User.php 2.1 KB
Newer Older
Qiang Xue committed
1 2 3 4
<?php

namespace app\models;

5
class User extends \yii\base\Object implements \yii\web\IdentityInterface
Qiang Xue committed
6
{
7 8 9 10 11
    public $id;
    public $username;
    public $password;
    public $authKey;
    public $accessToken;
Qiang Xue committed
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    private static $users = [
        '100' => [
            'id' => '100',
            'username' => 'admin',
            'password' => 'admin',
            'authKey' => 'test100key',
            'accessToken' => '100-token',
        ],
        '101' => [
            'id' => '101',
            'username' => 'demo',
            'password' => 'demo',
            'authKey' => 'test101key',
            'accessToken' => '101-token',
        ],
    ];
Qiang Xue committed
29

30 31 32 33 34 35 36
    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
    }
Qiang Xue committed
37

38 39 40
    /**
     * @inheritdoc
     */
41
    public static function findIdentityByAccessToken($token, $type = null)
42 43 44 45 46 47
    {
        foreach (self::$users as $user) {
            if ($user['accessToken'] === $token) {
                return new static($user);
            }
        }
Qiang Xue committed
48

49 50
        return null;
    }
Qiang Xue committed
51

52 53 54 55 56 57 58 59 60 61 62 63 64
    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        foreach (self::$users as $user) {
            if (strcasecmp($user['username'], $username) === 0) {
                return new static($user);
            }
        }
Qiang Xue committed
65

66 67
        return null;
    }
Qiang Xue committed
68

69 70 71 72 73 74 75
    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->id;
    }
Qiang Xue committed
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return $this->password === $password;
    }
Zander Baldwin committed
103
}