From e1a5ccaad4b9f4a670eb3ac9795960e634026668 Mon Sep 17 00:00:00 2001 From: Semih Serhat Karakaya Date: Fri, 22 May 2020 07:42:29 -0700 Subject: [PATCH] emit events for before-after-failed link password checks --- changelog/unreleased/37438 | 6 ++++++ lib/private/Share20/Manager.php | 8 ++++++-- tests/lib/Share20/ManagerTest.php | 33 +++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 changelog/unreleased/37438 diff --git a/changelog/unreleased/37438 b/changelog/unreleased/37438 new file mode 100644 index 000000000000..6c0bae4fa04e --- /dev/null +++ b/changelog/unreleased/37438 @@ -0,0 +1,6 @@ +Enhancement: Add 3 new events (before-fail-after) for link password validations. + +'share.beforelinkpasswordcheck', 'share.afterlinkpasswordcheck' and 'share.failedlinkpasswordcheck' +events has been added for link password validations. + +https://github.com/owncloud/core/pull/37438 diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index 34f58733ce72..a25749468ab2 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -1488,12 +1488,16 @@ public function checkPassword(\OCP\Share\IShare $share, $password) { if ($password === null || $share->getPassword() === null) { return false; } - + $beforeEvent = new GenericEvent(null, ['shareObject' => $share]); + $this->eventDispatcher->dispatch('share.beforelinkpasswordcheck', $beforeEvent); $newHash = ''; if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { + $failEvent = new GenericEvent(null, ['shareObject' => $share]); + $this->eventDispatcher->dispatch('share.failedlinkpasswordcheck', $failEvent); return false; } - + $afterEvent = new GenericEvent(null, ['shareObject' => $share]); + $this->eventDispatcher->dispatch('share.afterlinkpasswordcheck', $afterEvent); if (!empty($newHash)) { $share->setPassword($newHash); $provider = $this->factory->getProviderForType($share->getShareType()); diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index 4389517d0305..a72cbf4a2956 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -3151,7 +3151,23 @@ public function testCheckPasswordInvalidPassword() { $this->hasher->method('verify')->with('invalidpassword', 'password', '')->willReturn(false); + $calledBeforeEvent = []; + $this->eventDispatcher->addListener('share.beforelinkpasswordcheck', + function (GenericEvent $event) use (&$calledBeforeEvent) { + $calledBeforeEvent[] = 'share.beforelinkpasswordcheck'; + $calledBeforeEvent[] = $event; + }); + $calledFailEvent = []; + $this->eventDispatcher->addListener('share.failedlinkpasswordcheck', + function (GenericEvent $event) use (&$calledFailEvent) { + $calledFailEvent[] = 'share.failedlinkpasswordcheck'; + $calledFailEvent[] = $event; + }); $this->assertFalse($this->manager->checkPassword($share, 'invalidpassword')); + $this->assertEquals('share.beforelinkpasswordcheck', $calledBeforeEvent[0]); + $this->assertEquals('share.failedlinkpasswordcheck', $calledFailEvent[0]); + $this->assertInstanceOf(GenericEvent::class, $calledBeforeEvent[1]); + $this->assertInstanceOf(GenericEvent::class, $calledFailEvent[1]); } public function testCheckPasswordValidPassword() { @@ -3159,9 +3175,26 @@ public function testCheckPasswordValidPassword() { $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK); $share->method('getPassword')->willReturn('passwordHash'); + $calledBeforeEvent = []; + $this->eventDispatcher->addListener('share.beforelinkpasswordcheck', + function (GenericEvent $event) use (&$calledBeforeEvent) { + $calledBeforeEvent[] = 'share.beforelinkpasswordcheck'; + $calledBeforeEvent[] = $event; + }); + $calledAfterEvent = []; + $this->eventDispatcher->addListener('share.afterlinkpasswordcheck', + function (GenericEvent $event) use (&$calledAfterEvent) { + $calledAfterEvent[] = 'share.afterlinkpasswordcheck'; + $calledAfterEvent[] = $event; + }); + $this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true); $this->assertTrue($this->manager->checkPassword($share, 'password')); + $this->assertEquals('share.beforelinkpasswordcheck', $calledBeforeEvent[0]); + $this->assertEquals('share.afterlinkpasswordcheck', $calledAfterEvent[0]); + $this->assertInstanceOf(GenericEvent::class, $calledBeforeEvent[1]); + $this->assertInstanceOf(GenericEvent::class, $calledAfterEvent[1]); } public function testCheckPasswordUpdateShare() {