Advertisement
Guest User

user.php

a guest
Apr 19th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. class User extends \yii\base\Object implements \yii\web\IdentityInterface
  6. {
  7.     public $id;
  8.     public $username;
  9.     public $password;
  10.     public $authKey;
  11.     public $accessToken;
  12.  
  13.     private static $users = [
  14.         '100' => [
  15.             'id' => '100',
  16.             'username' => 'vadim',
  17.             'password' => '123456789',
  18.             'authKey' => 'test100key',
  19.             'accessToken' => '100-token',
  20.         ],
  21.         '101' => [
  22.             'id' => '101',
  23.             'username' => 'demo',
  24.             'password' => 'demo',
  25.             'authKey' => 'test101key',
  26.             'accessToken' => '101-token',
  27.         ],
  28.     ];
  29.  
  30.     /**
  31.      * @inheritdoc
  32.      */
  33.     public static function findIdentity($id)
  34.     {
  35.         return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
  36.     }
  37.  
  38.     /**
  39.      * @inheritdoc
  40.      */
  41.     public static function findIdentityByAccessToken($token, $type = null)
  42.     {
  43.         foreach (self::$users as $user) {
  44.             if ($user['accessToken'] === $token) {
  45.                 return new static($user);
  46.             }
  47.         }
  48.         return null;
  49.     }
  50.  
  51.     /**
  52.      * Finds user by username
  53.      *
  54.      * @param  string      $username
  55.      * @return static|null
  56.      */
  57.     public static function findByUsername($username)
  58.     {
  59.         foreach (self::$users as $user) {
  60.             if (strcasecmp($user['username'], $username) === 0) {
  61.                 return new static($user);
  62.             }
  63.         }
  64.  
  65.         return null;
  66.     }
  67.  
  68.     /**
  69.      * @inheritdoc
  70.      */
  71.     public function getId()
  72.     {
  73.         return $this->id;
  74.     }
  75.  
  76.     /**
  77.      * @inheritdoc
  78.      */
  79.     public function getAuthKey()
  80.     {
  81.         return $this->authKey;
  82.     }
  83.  
  84.     /**
  85.      * @inheritdoc
  86.      */
  87.     public function validateAuthKey($authKey)
  88.     {
  89.         return $this->authKey === $authKey;
  90.     }
  91.  
  92.     /**
  93.      * Validates password
  94.      *
  95.      * @param  string  $password password to validate
  96.      * @return boolean if password provided is valid for current user
  97.      */
  98.     public function validatePassword($password)
  99.     {
  100.         return $this->password === $password;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement