示例#1
0
 public function signInFormSubmitted(Form $form)
 {
     try {
         $user = $this->getUser();
         $values = $form->getValues();
         /*if ($values->persistent) {
               $user->setExpiration('+30 days', FALSE);
           }*/
         /** make login */
         $user->login($values->username, $values->password);
         /** get rights */
         $userId = $user->getIdentity()->id;
         $permissions = array();
         foreach ($this->permissionRepository->getLevels($userId)->fetchPairs('url') as $page => $level) {
             $permissions[$page] = $level->level;
         }
         /** test for admin */
         $permissions['admin'] = $this->userRepository->isAdmin($userId);
         /** set permissions */
         $user->getIdentity()->setRoles($permissions);
         $this->flashMessage('Přihlášení bylo úspěšné.', 'success');
         $this->redirect('Homepage:');
     } catch (Nette\Security\AuthenticationException $e) {
         $form->addError('Neplatné uživatelské jméno nebo heslo.');
     }
 }
示例#2
0
 /**
  * Performs an authentication.
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $row = $this->userRepository->findByName($username)->fetch();
     //dump(self::calculateHash($password, $row->password));
     if (!$row) {
         throw new Nette\Security\AuthenticationException("Uživatel '{$username}' nebyl nalezen.", self::IDENTITY_NOT_FOUND);
     }
     if ($row->password !== self::calculateHash($password, $row->password)) {
         throw new Nette\Security\AuthenticationException("Špatné heslo.", self::INVALID_CREDENTIAL);
     }
     unset($row->password);
     return new Nette\Security\Identity($row->id, NULL, $row->toArray());
 }