use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
use OC\Hooks\Emitter;
+use OC\Hooks\PublicEmitter;
use OC_User;
use OC_Util;
use OCA\DAV\Connector\Sabre\Auth;
*/
class Session implements IUserSession, Emitter {
- /** @var IUserManager $manager */
+ /** @var IUserManager|PublicEmitter $manager */
private $manager;
/** @var ISession $session */
/**
* get the manager object
*
- * @return Manager
+ * @return Manager|PublicEmitter
*/
public function getManager() {
return $this->manager;
return $this->loginWithPassword($uid, $password);
}
+ /**
+ * @param IUser $user
+ * @param array $loginDetails
+ * @return bool
+ * @throws LoginException
+ */
+ public function completeLogin(IUser $user, array $loginDetails) {
+ if (!$user->isEnabled()) {
+ // disabled users can not log in
+ // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory
+ $message = \OC::$server->getL10N('lib')->t('User disabled');
+ throw new LoginException($message);
+ }
+
+ $this->setUser($user);
+ $this->setLoginName($loginDetails['loginName']);
+
+ if(isset($loginDetails['token']) && $loginDetails['token'] instanceof IToken) {
+ $this->setToken($loginDetails['token']->getId());
+ \OC::$server->getLockdownManager()->setToken($loginDetails['token']);
+ $firstTimeLogin = false;
+ } else {
+ $this->setToken(null);
+ $firstTimeLogin = $user->updateLastLoginTimestamp();
+ }
+ $this->manager->emit('\OC\User', 'postLogin', [$user, $loginDetails['password']]);
+ if($this->isLoggedIn()) {
+ $this->prepareUserLogin($firstTimeLogin);
+ return true;
+ } else {
+ $message = \OC::$server->getL10N('lib')->t('Login canceled by app');
+ throw new LoginException($message);
+ }
+ }
+
/**
* Tries to log in a client
*
return false;
}
- if ($user->isEnabled()) {
- $this->setUser($user);
- $this->setLoginName($uid);
- $this->setToken(null);
- $firstTimeLogin = $user->updateLastLoginTimestamp();
- $this->manager->emit('\OC\User', 'postLogin', [$user, $password]);
- if ($this->isLoggedIn()) {
- $this->prepareUserLogin($firstTimeLogin);
- return true;
- } else {
- // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory
- $message = \OC::$server->getL10N('lib')->t('Login canceled by app');
- throw new LoginException($message);
- }
- } else {
- // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory
- $message = \OC::$server->getL10N('lib')->t('User disabled');
- throw new LoginException($message);
- }
+ return $this->completeLogin($user, ['loginName' => $uid, 'password' => $password]);
}
/**
// user does not exist
return false;
}
- if (!$user->isEnabled()) {
- // disabled users can not log in
- // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory
- $message = \OC::$server->getL10N('lib')->t('User disabled');
- throw new LoginException($message);
- }
-
- //login
- $this->setUser($user);
- $this->setLoginName($dbToken->getLoginName());
- $this->setToken($dbToken->getId());
- $this->lockdownManager->setToken($dbToken);
- $this->manager->emit('\OC\User', 'postLogin', array($user, $password));
-
- if ($this->isLoggedIn()) {
- $this->prepareUserLogin(false); // token login cant be the first
- } else {
- // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory
- $message = \OC::$server->getL10N('lib')->t('Login canceled by app');
- throw new LoginException($message);
- }
- return true;
+ return $this->completeLogin($user, ['loginName' => $uid, 'password' => $password, 'token' => $dbToken]);
}
/**
->method('checkPassword')
->will($this->returnValue($user));
$this->userSession->expects($this->once())
- ->method('login')
- ->with($loginName, $password);
+ ->method('completeLogin')
+ ->with($user, ['loginName' => $loginName, 'password' => $password]);
$this->userSession->expects($this->once())
->method('createSessionToken')
->with($this->request, $user->getUID(), $loginName, $password, false);
->method('checkPassword')
->will($this->returnValue($user));
$this->userSession->expects($this->once())
- ->method('login')
- ->with($loginName, $password);
+ ->method('completeLogin')
+ ->with($user, ['loginName' => $loginName, 'password' => $password]);
$this->userSession->expects($this->once())
->method('createSessionToken')
->with($this->request, $user->getUID(), $loginName, $password, true);
->method('checkPassword')
->will($this->returnValue($user));
$this->userSession->expects($this->once())
- ->method('login')
- ->with('john@doe.com', $password);
+ ->method('completeLogin')
+ ->with($user, ['loginName' => 'john@doe.com', 'password' => $password]);
$this->userSession->expects($this->once())
->method('createSessionToken')
->with($this->request, $user->getUID(), 'john@doe.com', $password, false);
->method('checkPassword')
->will($this->returnValue($user));
$this->userSession->expects($this->once())
- ->method('login')
- ->with('john@doe.com', $password);
+ ->method('completeLogin')
+ ->with($user, ['loginName' => 'john@doe.com', 'password' => $password]);
$this->userSession->expects($this->once())
->method('createSessionToken')
->with($this->request, $user->getUID(), 'john@doe.com', $password, false);