Exemple #1
0
 /**
  * Creates the session for the user with the passed credentials.
  *
  * @param string $email
  * @param string $pw
  * @return Result
  */
 public function login($email, $pw)
 {
     $users = $this->userManager->getBy(array('email' => $email));
     if (count($users) == 0) {
         return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, $email);
     }
     if (count($users) >= 2) {
         return new Result(Result::FAILURE_IDENTITY_AMBIGUOUS, $email);
     }
     $user = current($users);
     /* Check for current login detent */
     $currentDateTime = new DateTime();
     if ($user->get('login_detent')) {
         $loginDetent = new DateTime($user->get('login_detent'));
         if ($loginDetent > $currentDateTime) {
             $result = new Result(Result::FAILURE_TOO_MANY_TRIES, $user);
             $result->setExtra('login_detent', $loginDetent);
             return $result;
         }
     }
     $bcrypt = new Bcrypt();
     $bcrypt->setCost(6);
     /* If legacy password is detected, use it for login and then delete it */
     if ($user->getMeta('legacy-pw')) {
         $legacyPw = $user->getMeta('legacy-pw');
         if ($legacyPw == md5($pw)) {
             $user->set('pw', $bcrypt->create($pw));
             $user->setMeta('legacy-pw', null);
         }
     }
     /* Check original credentials */
     if ($bcrypt->verify($pw, $user->need('pw'))) {
         /* Check user status */
         switch ($user->need('status')) {
             case 'placeholder':
             case 'deleted':
             case 'blocked':
             case 'disabled':
                 return new Result(Result::FAILURE_USER_STATUS, $user);
         }
         /* Create the session */
         $container = $this->getSessionContainer();
         $container->uid = $user->need('uid');
         /* Update last activity and ip */
         $user->set('login_attempts', null);
         $user->set('login_detent', null);
         $user->set('last_activity', date('Y-m-d H:i:s'));
         $user->set('last_ip', $_SERVER['REMOTE_ADDR']);
         $this->userManager->save($user);
         /* Inform anyone interested in this */
         $this->getEventManager()->trigger('login', $user);
         return new Result(Result::SUCCESS, $user);
     }
     /* Invalid password passed, prepare detent */
     $loginAttempts = $user->get('login_attempts');
     if (!$loginAttempts) {
         $loginAttempts = 0;
     }
     $loginAttempts++;
     if ($loginAttempts >= $this->attemptsAllowed) {
         $loginAttempts = null;
         $loginDetent = clone $currentDateTime;
         $loginDetent->modify(sprintf('+%u sec', $this->detentDuration));
     } else {
         $loginDetent = null;
     }
     $user->set('login_attempts', $loginAttempts);
     $user->set('login_detent', $loginDetent ? $loginDetent->format('Y-m-d H:i:s') : null);
     $this->userManager->save($user);
     return new Result(Result::FAILURE_CREDENTIAL_INVALID, $user);
 }
Exemple #2
0
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testExtraDataInvalidNullKey()
 {
     $result = new Result(Result::SUCCESS, 'Someone');
     $result->setExtra(null, 'Something');
 }