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

4
use Yii;
Qiang Xue committed
5
use yii\base\NotSupportedException;
6
use yii\behaviors\TimestampBehavior;
7
use yii\db\ActiveRecord;
8
use yii\web\IdentityInterface;
9 10

/**
11
 * User model
12 13 14 15
 *
 * @property integer $id
 * @property string $username
 * @property string $password_hash
16
 * @property string $password_reset_token
17 18 19
 * @property string $email
 * @property string $auth_key
 * @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
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;

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

37 38 39 40 41 42
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
43
            TimestampBehavior::className(),
44 45 46
        ];
    }

47
    /**
宝哥哥 committed
48 49
     * @inheritdoc
     */
resurtm committed
50 51 52 53 54 55 56
    public function rules()
    {
        return [
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
        ];
    }
57

58 59 60 61 62
    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
63
        return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
64 65 66 67 68
    }

    /**
     * @inheritdoc
     */
69
    public static function findIdentityByAccessToken($token, $type = null)
70 71 72 73 74 75 76
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * Finds user by username
     *
77
     * @param string $username
78 79 80 81
     * @return static|null
     */
    public static function findByUsername($username)
    {
Alexander Makarov committed
82
        return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
83 84 85 86 87
    }

    /**
     * Finds user by password reset token
     *
88
     * @param string $token password reset token
89 90 91 92
     * @return static|null
     */
    public static function findByPasswordResetToken($token)
    {
93
        if (!static::isPasswordResetTokenValid($token)) {
94 95 96
            return null;
        }

Alexander Makarov committed
97
        return static::findOne([
98 99 100 101 102
            'password_reset_token' => $token,
            'status' => self::STATUS_ACTIVE,
        ]);
    }

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    /**
     * Finds out if password reset token is valid
     *
     * @param string $token password reset token
     * @return boolean
     */
    public static function isPasswordResetTokenValid($token)
    {
        if (empty($token)) {
            return false;
        }
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
        $parts = explode('_', $token);
        $timestamp = (int) end($parts);
        return $timestamp + $expire >= time();
    }

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
    /**
     * @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
     *
147
     * @param string $password password to validate
148 149 150 151
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
152
        return Yii::$app->security->validatePassword($password, $this->password_hash);
153 154 155 156 157 158 159 160 161
    }

    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
162
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
163 164 165 166 167 168 169
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
170
        $this->auth_key = Yii::$app->security->generateRandomString();
171 172 173 174 175 176 177
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
178
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
179 180 181 182 183 184 185 186 187
    }

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