User.php 4.22 KB
Newer Older
1
<?php
2
namespace common\models;
3

Qiang Xue committed
4
use yii\base\NotSupportedException;
5
use yii\db\ActiveRecord;
6
use yii\helpers\Security;
7
use yii\web\IdentityInterface;
8 9

/**
10
 * User model
11 12 13 14
 *
 * @property integer $id
 * @property string $username
 * @property string $password_hash
15
 * @property string $password_reset_token
16 17 18 19
 * @property string $email
 * @property string $auth_key
 * @property integer $role
 * @property integer $status
20 21
 * @property integer $created_at
 * @property integer $updated_at
Alexander Makarov committed
22
 * @property string $password write-only password
23
 */
24
class User extends ActiveRecord implements IdentityInterface
25
{
26 27 28 29 30
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;

    const ROLE_USER = 10;

31 32 33 34 35 36 37 38
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => 'yii\behaviors\TimestampBehavior',
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ];
    }

55 56 57 58 59 60 61 62 63 64 65 66 67 68
    /**
      * @inheritdoc
      */
     public function rules()
     {
         return [
             ['status', 'default', 'value' => self::STATUS_ACTIVE],
             ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],

             ['role', 'default', 'value' => self::ROLE_USER],
             ['role', 'in', 'range' => [self::ROLE_USER]],
         ];
     }

69 70 71 72 73
    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
Alexander Makarov committed
74
        return static::findOne($id);
75 76 77 78 79
    }

    /**
     * @inheritdoc
     */
80
    public static function findIdentityByAccessToken($token, $type = null)
81 82 83 84 85 86 87 88 89 90 91 92
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
Alexander Makarov committed
93
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    }

    /**
     * Finds user by password reset token
     *
     * @param  string      $token password reset token
     * @return static|null
     */
    public static function findByPasswordResetToken($token)
    {
        $expire = \Yii::$app->params['user.passwordResetTokenExpire'];
        $parts = explode('_', $token);
        $timestamp = (int) end($parts);
        if ($timestamp + $expire < time()) {
            // token expired
            return null;
        }

Alexander Makarov committed
112
        return static::findOne([
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
            'password_reset_token' => $token,
            'status' => self::STATUS_ACTIVE,
        ]);
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

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

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return Security::validatePassword($password, $this->password_hash);
    }

    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password_hash = Security::generatePasswordHash($password);
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
        $this->auth_key = Security::generateRandomKey();
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
        $this->password_reset_token = Security::generateRandomKey() . '_' . time();
    }

    /**
     * Removes password reset token
     */
    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }
186
}