Skip to content

Commit

Permalink
Add logout prehooks
Browse files Browse the repository at this point in the history
This change will help to do actions
in logout prehooks. Any changes to
be made before logout action can be
done here.

Signed-off-by: Sujith H <sharidasan@owncloud.com>
  • Loading branch information
sharidas committed Oct 25, 2017
1 parent b08c9cd commit eb1384d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
use OC\Files\External\Service\GlobalStoragesService;
use OC\Files\External\Service\DBConfigService;
use OC\Http\Client\WebdavClientService;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class Server
Expand Down Expand Up @@ -322,6 +323,10 @@ public function __construct($webRoot, \OC\Config $config) {
/** @var $user \OC\User\User */
\OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'password' => $password]);
});
$userSession->listen('\OC\User', 'preLogout', function () {
$event = new GenericEvent(null, []);
\OC::$server->getEventDispatcher()->dispatch('\OC\User\Session::pre_logout', $event);
});
$userSession->listen('\OC\User', 'logout', function () {
\OC_Hook::emit('OC_User', 'logout', []);
});
Expand Down
14 changes: 14 additions & 0 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use OCP\IUserSession;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\Util;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
Expand Down Expand Up @@ -821,8 +822,21 @@ public function loginWithCookie($uid, $currentToken) {

/**
* logout the user from the session
*
* @return bool
*/
public function logout() {

$event = new GenericEvent(null, ['cancel' => false]);
$eventDispatcher = \OC::$server->getEventDispatcher();
$eventDispatcher->dispatch('\OC\User\Session::pre_logout', $event);

$this->manager->emit('\OC\User', 'preLogout');

if ($event['cancel'] === true) {
return true;
}

$this->manager->emit('\OC\User', 'logout');
$user = $this->getUser();
if (!is_null($user)) {
Expand Down
29 changes: 29 additions & 0 deletions tests/lib/User/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,4 +947,33 @@ public function testUpdateSessionTokenPasswordInvalidTokenException() {
$userSession->updateSessionTokenPassword($password);
}

public function testCancelLogout() {
/** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */
$session = $this->createMock(Memory::class);
$session->expects($this->once())
->method('set')
->with('user_id', 'foo');

/** @var Manager $manager */
$manager = $this->createMock(Manager::class);

/** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->will($this->returnValue('foo'));

$userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
$userSession->setUser($user);


$called['cancel'] = false;

\OC::$server->getEventDispatcher()->addListener('\OC\User\Session::pre_logout', function ($event) use (&$called) {
$called['cancel'] = true;
$event['cancel'] = $called['cancel'];
});

$this->assertEquals(true, $userSession->logout());
}
}

0 comments on commit eb1384d

Please sign in to comment.