Overview

Namespaces

  • App
    • Components
    • Interfaces
    • Lib
      • Files
      • Forms
        • Controls
      • Html
      • Repair
      • Statics
    • Model
    • Modules
      • BaseModule
        • Presenters
      • PartnersModule
        • Presenters
      • ProductModule
        • Presenters
      • SaleModule
        • Model
        • Presenters
      • SettingsModule
        • Model
        • Presenters
      • StoreModule
        • Model
        • Presenters
    • Presenters
    • Router
  • PHP

Classes

  • DateTime
  • SplFileInfo

Interfaces

  • ArrayAccess
  • Countable
  • Exception
  • Iterator
  • Traversable
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Download
  1: <?php
  2: 
  3: /**
  4:  * Project sberp
  5:  *
  6:  * @file Authenticator 
  7:  * @author Jaromír Polášek
  8:  * @version 0.7.6 RELATION
  9:  * Encoding UTF-8
 10:  */
 11: 
 12: namespace App\Model;
 13: 
 14: use Nette\Object,
 15:     Nette\Security as NS,
 16:     Nette\Security\Passwords,
 17:     App\Model\Database,
 18:     App\Lib\Table,
 19:     App\Lib\Statics\Cons,
 20:     App\Router\RouterFactory;
 21: 
 22: /**
 23:  * Zajištuje Autentizaci uživatele
 24:  */
 25: class Authenticator extends Object implements NS\IAuthenticator {
 26:  
 27:     /** @var \App\Model\Database */
 28:     private $database;
 29:     
 30:     /**
 31:      * @param \App\Model\Database $database
 32:      */
 33:     public function __construct(Database $database) {
 34:     $this->database = $database;
 35:     }
 36: 
 37:     /**
 38:      * Provede přihlášení
 39:      * @return Nette\Security\Identity
 40:      * @throws Nette\Security\AuthenticationException
 41:      */
 42:     public function authenticate(array $credentials) {
 43:     list($username, $password) = $credentials;
 44:     $row = (new Table(Cons::TABLE_USERS, $this->database))->data->where(Cons::COLUMN_USERNAME, $username)->fetch();
 45:     if (!$row) {
 46:         throw new NS\AuthenticationException('Špatné jméno nebo heslo.', self::IDENTITY_NOT_FOUND);
 47:     } else if ($this->password($row, $password)) {
 48:         return new NS\Identity($row->id, $this->getRoles($row->id), $this->getData($row));
 49:     }
 50:     }
 51: 
 52:     /**
 53:      * Vrací pole rolí uživatele 
 54:      * @param integer $id 
 55:      * @return array 
 56:      */
 57:     public function getRoles($id) {
 58:     $roles = array();
 59:     $selection = (new Table(Cons::TABLE_USERS.'_'.Cons::TABLE_ROLES, $this->database))
 60:         ->data->where(Cons::TABLE_USERS.'_'.Cons::COLUMN_ID, $id);
 61:     foreach ($selection as $row) {
 62:         $roles[] = $row[Cons::TABLE_ACL.'_'.Cons::TABLE_ROLES][Cons::COLUMN_PATH];
 63:     }
 64:     return $roles;
 65:     }
 66: 
 67:     /**
 68:      * Ověření Hesla uživatele
 69:      * @param \Nette\Database\Table\ActiveRow $user
 70:      * @param string $password
 71:      * @return boolean
 72:      * @throws NS\AuthenticationException
 73:      */
 74:     private function password($user, $password) {
 75:     if (!Passwords::verify($password, $user[Cons::COLUMN_PASSWORD])) {
 76:         throw new NS\AuthenticationException('Špatné jméno nebo heslo.', self::INVALID_CREDENTIAL);
 77:     } elseif (Passwords::needsRehash($user[Cons::COLUMN_PASSWORD])) {
 78:         $user->update(array(Cons::COLUMN_PASSWORD => Passwords::hash($password)));
 79:     } elseif ($user[Cons::COLUMN_STATE] === 'Blocked') {
 80:         throw new NS\AuthenticationException('Uživatel byl zablokován', self::INVALID_CREDENTIAL);
 81:     } elseif($user[Cons::COLUMN_STATE] === 'Inactive'){
 82:         throw new NS\AuthenticationException('Uživatel není aktivní', self::INVALID_CREDENTIAL);
 83:     } else {
 84:         return true;
 85:     }
 86:     }
 87: 
 88:     /**
 89:      * Vrací pole s daty uživatele
 90:      * @param \Nette\Database\TAble\ActiveRow $userRow
 91:      * @return array
 92:      */
 93:     private function getData($userRow) {
 94:     $homepageRow = $userRow->ref(Cons::TABLE_SOURCE);   
 95:     return array(
 96:         Cons::USER_LOGIN => $userRow[Cons::COLUMN_USERNAME],
 97:         Cons::USER_NAME => $userRow[Cons::COLUMN_FULLNAME],
 98:         Cons::USER_EMAIL => $userRow[Cons::COLUMN_EMAIL],
 99:         Cons::USER_HOME => RouterFactory::getRouteString($homepageRow),
100:         Cons::USER_SOURCE => $userRow[Cons::TABLE_SOURCE.'_'.Cons::COLUMN_ID])
101:     ;
102:     }
103: }
104: 
sberp API API documentation generated by ApiGen