Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First login event #26206

Merged
merged 6 commits into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 @@ -58,6 +59,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 @@ -70,7 +81,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 @@ -84,10 +84,6 @@ public function setup() {
'changeUser',
$this,
'changeUser');
Util::connectHook('OC_User',
'post_login',
$this,
'postLogin');
}

public function postCreateUser($params) {
Expand Down Expand Up @@ -123,8 +119,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();
$calendars = $this->calDav->getCalendarsForUser($principal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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 @@ -60,7 +60,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 Down Expand Up @@ -90,7 +89,7 @@ public function test() {
'contacts', ['{DAV:}displayname' => $this->l10n->t('Contacts')]);

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

public function testWithExisting() {
Expand All @@ -103,7 +102,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 @@ -129,7 +127,7 @@ public function testWithExisting() {
$card->expects($this->never())->method('createAddressBook');

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

public function testWithBirthdayCalendar() {
Expand All @@ -142,7 +140,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 Down Expand Up @@ -174,7 +171,7 @@ public function testWithBirthdayCalendar() {
'contacts', ['{DAV:}displayname' => $this->l10n->t('Contacts')]);

$hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n);
$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 @@ -23,8 +23,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 @@ -33,7 +35,7 @@
*
* @package OCA\Files\Tests\Command
*/
class DeleteOrphanedFilesTest extends \Test\TestCase {
class DeleteOrphanedFilesTest extends TestCase {

/**
* @var DeleteOrphanedFiles
Expand Down Expand Up @@ -93,7 +95,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 @@ -114,7 +116,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 @@ -349,7 +349,6 @@ public function getUserFolder($userId) {
$folder = $folder->get($dir);
} catch (NotFoundException $e) {
$folder = $folder->newFolder($dir);
\OC_Util::copySkeleton($userId, $folder);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


return $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 @@ -366,15 +366,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 @@ -395,6 +386,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 @@ -78,14 +78,6 @@ public function __construct(IConfig $config = null) {
/** @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
21 changes: 17 additions & 4 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OCP\IUserSession;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\Util;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class Session
Expand Down Expand Up @@ -371,15 +372,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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we can also leverage this to avoid syncing the whole LDAP profile every time a LDAP user logs in ? (something to discuss separately)

CC @butonic

// 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 @@ -431,9 +442,10 @@ private function loginWithPassword($uid, $password) {
if ($user->isEnabled()) {
$this->setUser($user);
$this->setLoginName($uid);
$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 @@ -688,6 +700,7 @@ public function loginWithCookie($uid, $currentToken) {

//login
$this->setUser($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 @@ -333,6 +333,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