diff --git a/apps/files_sharing/lib/Controllers/ShareController.php b/apps/files_sharing/lib/Controllers/ShareController.php index a253ae3b9b7b..3d32b4a93b0d 100644 --- a/apps/files_sharing/lib/Controllers/ShareController.php +++ b/apps/files_sharing/lib/Controllers/ShareController.php @@ -180,6 +180,8 @@ public function authenticate($token, $password = '') { * @return bool */ private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { + $beforeEvent = new GenericEvent(null, ['shareObject' => $share]); + $this->eventDispatcher->dispatch('share.beforelinkauth', $beforeEvent); if ($password !== null) { if ($this->shareManager->checkPassword($share, $password)) { $this->session->set('public_link_authenticated', (string)$share->getId()); @@ -194,6 +196,8 @@ private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { return false; } } + $afterEvent = new GenericEvent(null, ['shareObject' => $share]); + $this->eventDispatcher->dispatch('share.afterlinkauth', $afterEvent); return true; } diff --git a/apps/files_sharing/tests/Controllers/ShareControllerTest.php b/apps/files_sharing/tests/Controllers/ShareControllerTest.php index 5018b82ae5c2..c78b0aa048fc 100644 --- a/apps/files_sharing/tests/Controllers/ShareControllerTest.php +++ b/apps/files_sharing/tests/Controllers/ShareControllerTest.php @@ -223,9 +223,24 @@ public function testAuthenticateValidPassword() { ->with('files_sharing.sharecontroller.showShare', ['token'=>'token']) ->willReturn('redirect'); + $beforeLinkAuthCalled = false; + $this->eventDispatcher->addListener( + 'share.beforelinkauth', function () use (&$beforeLinkAuthCalled) { + $beforeLinkAuthCalled = true; + } + ); + $afterLinkAuthCalled = false; + $this->eventDispatcher->addListener( + 'share.afterlinkauth', function () use (&$afterLinkAuthCalled) { + $afterLinkAuthCalled = true; + } + ); + $response = $this->shareController->authenticate('token', 'validpassword'); $expectedResponse = new RedirectResponse('redirect'); $this->assertEquals($expectedResponse, $response); + $this->assertEquals(true, $beforeLinkAuthCalled); + $this->assertEquals(true, $afterLinkAuthCalled); } public function testAuthenticateInvalidPassword() { @@ -262,6 +277,19 @@ function (GenericEvent $event) use (&$calledShareLinkAccess) { $calledShareLinkAccess[] = $event; }); + $beforeLinkAuthCalled = false; + $this->eventDispatcher->addListener( + 'share.beforelinkauth', function () use (&$beforeLinkAuthCalled) { + $beforeLinkAuthCalled = true; + } + ); + $afterLinkAuthCalled = false; + $this->eventDispatcher->addListener( + 'share.afterlinkauth', function () use (&$afterLinkAuthCalled) { + $afterLinkAuthCalled = true; + } + ); + $hookListner->expects($this->once()) ->method('access') ->with($this->callback(function (array $data) { @@ -286,6 +314,8 @@ function (GenericEvent $event) use (&$calledShareLinkAccess) { $this->assertEquals('token', $calledShareLinkAccess[1]->getArgument('shareObject')->getToken()); $this->assertEquals(403, $calledShareLinkAccess[1]->getArgument('errorCode')); $this->assertEquals('Wrong password', $calledShareLinkAccess[1]->getArgument('errorMessage')); + $this->assertEquals(true, $beforeLinkAuthCalled); + $this->assertEquals(false, $afterLinkAuthCalled); } public function testShowShareInvalidToken() {