Ejemplo n.º 1
0
 /**
  * @param array $credentials
  * @return Identity|\Nette\Security\IIdentity
  * @throws \movi\InvalidStateException
  * @throws \Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     if ($this->users === NULL) {
         throw new InvalidStateException('Service IUsers is not registered.');
     }
     $user = $this->users->login($credentials);
     if (!$user) {
         throw new AuthenticationException(self::INVALID_CREDENTIAL);
     }
     $token = $this->users->generateToken($user);
     return new Identity($user->id, $user->role, $token);
 }
Ejemplo n.º 2
0
 /**
  * @return bool
  * @throws InvalidStateException
  */
 public function isAuthenticated()
 {
     $authenticated = parent::isAuthenticated();
     if ($this->users === NULL) {
         throw new InvalidStateException('Service IUsers is not registered.');
     }
     if ($this->authenticated === NULL || $this->authenticated !== $authenticated) {
         if ($authenticated === true) {
             $identity = $this->getIdentity();
             if (!$this->users->validateToken($identity->getToken(), $identity->getUser())) {
                 $this->getSessionSection(true)->remove();
                 // Logout
                 $authenticated = false;
             }
         }
         $this->authenticated = $authenticated;
     }
     return $this->authenticated;
 }