User.php 4.35 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 20
 * @property string $email
 * @property string $auth_key
 * @property integer $role
 * @property integer $status
21 22
 * @property integer $created_at
 * @property integer $updated_at
Alexander Makarov committed
23
 * @property string $password write-only password
24
 */
25
class User extends ActiveRecord implements IdentityInterface
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
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
45
            TimestampBehavior::className(),
46 47 48
        ];
    }

49
    /**
宝哥哥 committed
50 51
     * @inheritdoc
     */
resurtm committed
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

resurtm committed
58 59 60 61
            ['role', 'default', 'value' => self::ROLE_USER],
            ['role', 'in', 'range' => [self::ROLE_USER]],
        ];
    }
62

63 64 65 66 67
    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
68
        return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
69 70 71 72 73
    }

    /**
     * @inheritdoc
     */
74
    public static function findIdentityByAccessToken($token, $type = null)
75 76 77 78 79 80 81
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

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

    /**
     * Finds user by password reset token
     *
93
     * @param string $token password reset token
94 95 96 97
     * @return static|null
     */
    public static function findByPasswordResetToken($token)
    {
98
        if (!static::isPasswordResetTokenValid($token)) {
99 100 101
            return null;
        }

Alexander Makarov committed
102
        return static::findOne([
103 104 105 106 107
            'password_reset_token' => $token,
            'status' => self::STATUS_ACTIVE,
        ]);
    }

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    /**
     * 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();
    }

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

    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
167
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
168 169 170 171 172 173 174
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
175
        $this->auth_key = Yii::$app->security->generateRandomString();
176 177 178 179 180 181 182
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
183
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
184 185 186 187 188 189 190 191 192
    }

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