Skip to content

Commit

Permalink
add before-after share link auth events
Browse files Browse the repository at this point in the history
  • Loading branch information
karakayasemi authored and gitmate-bot committed Jan 20, 2019
1 parent 3515439 commit 4fe6d6f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions apps/files_sharing/lib/Controllers/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}

Expand Down
30 changes: 30 additions & 0 deletions apps/files_sharing/tests/Controllers/ShareControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down

0 comments on commit 4fe6d6f

Please sign in to comment.