Skip to content

Commit

Permalink
Introduce an event for first time login based on the last login time …
Browse files Browse the repository at this point in the history
…stamp

Use firstLogin event to trigger creation of default calendar and default address book

Delay login of admin user after setup so that firstLogin event can properly be processed for the admin

Fixing tests ...

Skeleton files are not copied over -> only 3 cache entries are remaining

Use updateLastLoginTimestamp to properly setup lastLogin value for a test user
  • Loading branch information
DeepDiver1975 authored and MorrisJobke committed Nov 14, 2016
1 parent cebb689 commit 506ccdb
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 41 deletions.
12 changes: 11 additions & 1 deletion apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\DAV\HookManager;
use \OCP\AppFramework\App;
use OCP\Contacts\IManager;
use OCP\IUser;
use Symfony\Component\EventDispatcher\GenericEvent;

class Application extends App {
Expand Down Expand Up @@ -65,6 +66,16 @@ public function registerHooks() {
$hm = $this->getContainer()->query(HookManager::class);
$hm->setup();

$dispatcher = $this->getContainer()->getServer()->getEventDispatcher();

// first time login event setup
$dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm) {
if ($event instanceof GenericEvent) {
$hm->firstLogin($event->getSubject());
}
});

// carddav/caldav sync event setup
$listener = function($event) {
if ($event instanceof GenericEvent) {
/** @var BirthdayService $b */
Expand All @@ -77,7 +88,6 @@ public function registerHooks() {
}
};

$dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
$dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', $listener);
$dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $listener);
$dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', function($event) {
Expand Down
7 changes: 1 addition & 6 deletions apps/dav/lib/HookManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ public function setup() {
'changeUser',
$this,
'changeUser');
Util::connectHook('OC_User',
'post_login',
$this,
'postLogin');
}

public function postCreateUser($params) {
Expand Down Expand Up @@ -117,8 +113,7 @@ public function changeUser($params) {
$this->syncService->updateUser($user);
}

public function postLogin($params) {
$user = $this->userManager->get($params['uid']);
public function firstLogin(IUser $user = null) {
if (!is_null($user)) {
$principal = 'principals/users/' . $user->getUID();
if ($this->calDav->getCalendarsForUserCount($principal) === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function testUploadOverWriteReadLocked() {
public function testUploadOverWriteWriteLocked() {
$user = $this->getUniqueID();
$view = $this->setupUser($user, 'pass');
$this->loginAsUser($user);

$view->file_put_contents('foo.txt', 'bar');

Expand Down
9 changes: 3 additions & 6 deletions apps/dav/tests/unit/DAV/HookManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function test() {
$userManager = $this->getMockBuilder(IUserManager::class)
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())->method('get')->willReturn($user);

/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
$syncService = $this->getMockBuilder(SyncService::class)
Expand All @@ -84,7 +83,7 @@ public function test() {
'contacts', ['{DAV:}displayname' => 'Contacts']);

$hm = new HookManager($userManager, $syncService, $cal, $card);
$hm->postLogin(['uid' => 'newUser']);
$hm->firstLogin($user);
}

public function testWithExisting() {
Expand All @@ -97,7 +96,6 @@ public function testWithExisting() {
$userManager = $this->getMockBuilder(IUserManager::class)
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())->method('get')->willReturn($user);

/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
$syncService = $this->getMockBuilder(SyncService::class)
Expand All @@ -119,7 +117,7 @@ public function testWithExisting() {
$card->expects($this->never())->method('createAddressBook');

$hm = new HookManager($userManager, $syncService, $cal, $card);
$hm->postLogin(['uid' => 'newUser']);
$hm->firstLogin($user);
}

public function testWithBirthdayCalendar() {
Expand All @@ -132,7 +130,6 @@ public function testWithBirthdayCalendar() {
$userManager = $this->getMockBuilder(IUserManager::class)
->disableOriginalConstructor()
->getMock();
$userManager->expects($this->once())->method('get')->willReturn($user);

/** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $syncService */
$syncService = $this->getMockBuilder(SyncService::class)
Expand All @@ -158,7 +155,7 @@ public function testWithBirthdayCalendar() {
'contacts', ['{DAV:}displayname' => 'Contacts']);

$hm = new HookManager($userManager, $syncService, $cal, $card);
$hm->postLogin(['uid' => 'newUser']);
$hm->firstLogin($user);
}

public function testDeleteCalendar() {
Expand Down
8 changes: 5 additions & 3 deletions apps/files/tests/Command/DeleteOrphanedFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

namespace OCA\Files\Tests\Command;

use OC\Files\View;
use OCA\Files\Command\DeleteOrphanedFiles;
use OCP\Files\StorageNotAvailableException;
use Test\TestCase;

/**
* Class DeleteOrphanedFilesTest
Expand All @@ -34,7 +36,7 @@
*
* @package OCA\Files\Tests\Command
*/
class DeleteOrphanedFilesTest extends \Test\TestCase {
class DeleteOrphanedFilesTest extends TestCase {

/**
* @var DeleteOrphanedFiles
Expand Down Expand Up @@ -94,7 +96,7 @@ public function testClearFiles() {

$this->loginAsUser($this->user1);

$view = new \OC\Files\View('/' . $this->user1 . '/');
$view = new View('/' . $this->user1 . '/');
$view->mkdir('files/test');

$fileInfo = $view->getFileInfo('files/test');
Expand All @@ -115,7 +117,7 @@ public function testClearFiles() {
$output
->expects($this->once())
->method('writeln')
->with('4 orphaned file cache entries deleted');
->with('3 orphaned file cache entries deleted');

$this->command->execute($input, $output);

Expand Down
1 change: 0 additions & 1 deletion lib/private/Files/Node/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ public function getUserFolder($userId) {
$this->newFolder('/' . $userId);
}
$folder = $this->newFolder('/' . $userId . '/files');
\OC_Util::copySkeleton($userId, $folder);
}

$this->userFolderCache->set($userId, $folder);
Expand Down
18 changes: 9 additions & 9 deletions lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,6 @@ public function install($options) {
$group =\OC::$server->getGroupManager()->createGroup('admin');
$group->addUser($user);

// Create a session token for the newly created user
// The token provider requires a working db, so it's not injected on setup
/* @var $userSession User\Session */
$userSession = \OC::$server->getUserSession();
$defaultTokenProvider = \OC::$server->query('OC\Authentication\Token\DefaultTokenProvider');
$userSession->setTokenProvider($defaultTokenProvider);
$userSession->login($username, $password);
$userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password);

//guess what this does
Installer::installShippedApps();

Expand All @@ -392,6 +383,15 @@ public function install($options) {

//and we are done
$config->setSystemValue('installed', true);

// Create a session token for the newly created user
// The token provider requires a working db, so it's not injected on setup
/* @var $userSession User\Session */
$userSession = \OC::$server->getUserSession();
$defaultTokenProvider = \OC::$server->query('OC\Authentication\Token\DefaultTokenProvider');
$userSession->setTokenProvider($defaultTokenProvider);
$userSession->login($username, $password);
$userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password);
}

return $error;
Expand Down
8 changes: 0 additions & 8 deletions lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ public function __construct(IConfig $config) {
/** @var \OC\User\User $user */
unset($cachedUsers[$user->getUID()]);
});
$this->listen('\OC\User', 'postLogin', function ($user) {
/** @var \OC\User\User $user */
$user->updateLastLoginTimestamp();
});
$this->listen('\OC\User', 'postRememberedLogin', function ($user) {
/** @var \OC\User\User $user */
$user->updateLastLoginTimestamp();
});
}

/**
Expand Down
25 changes: 19 additions & 6 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use OCP\Security\ISecureRandom;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\Util;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class Session
Expand Down Expand Up @@ -396,15 +397,25 @@ public function isTokenPassword($password) {
}
}

protected function prepareUserLogin() {
protected function prepareUserLogin($firstTimeLogin) {
// TODO: mock/inject/use non-static
// Refresh the token
\OC::$server->getCsrfTokenManager()->refreshToken();
//we need to pass the user name, which may differ from login name
$user = $this->getUser()->getUID();
OC_Util::setupFS($user);
//trigger creation of user home and /files folder
\OC::$server->getUserFolder($user);

if ($firstTimeLogin) {
// TODO: lock necessary?
//trigger creation of user home and /files folder
$userFolder = \OC::$server->getUserFolder($user);

// copy skeleton
\OC_Util::copySkeleton($user, $userFolder);

// trigger any other initialization
\OC::$server->getEventDispatcher()->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser()));
}
}

/**
Expand Down Expand Up @@ -457,9 +468,10 @@ private function loginWithPassword($uid, $password) {
if ($user->isEnabled()) {
$this->setUser($user);
$this->setLoginName($uid);
$this->manager->emit('\OC\User', 'postLogin', array($user, $password));
$firstTimeLogin = $user->updateLastLoginTimestamp();
$this->manager->emit('\OC\User', 'postLogin', [$user, $password]);
if ($this->isLoggedIn()) {
$this->prepareUserLogin();
$this->prepareUserLogin($firstTimeLogin);
return true;
} else {
// injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory
Expand Down Expand Up @@ -725,7 +737,8 @@ public function loginWithCookie($uid, $currentToken, $oldSessionId) {

//login
$this->setUser($user);
$this->manager->emit('\OC\User', 'postRememberedLogin', array($user));
$user->updateLastLoginTimestamp();
$this->manager->emit('\OC\User', 'postRememberedLogin', [$user]);
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,12 @@ public function getLastLogin() {
* updates the timestamp of the most recent login of this user
*/
public function updateLastLoginTimestamp() {
$firstTimeLogin = ($this->lastLogin === 0);
$this->lastLogin = time();
\OC::$server->getConfig()->setUserValue(
$this->config->setUserValue(
$this->uid, 'login', 'lastLogin', $this->lastLogin);

return $firstTimeLogin;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ static protected function loginAsUser($user = '') {
self::logout();
\OC\Files\Filesystem::tearDown();
\OC_User::setUserId($user);
$userObject = \OC::$server->getUserManager()->get($user);
if (!is_null($userObject)) {
$userObject->updateLastLoginTimestamp();
}
\OC_Util::setupFS($user);
if (\OC_User::userExists($user)) {
\OC::$server->getUserFolder($user);
Expand Down

0 comments on commit 506ccdb

Please sign in to comment.