User.php 4.04 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 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
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
99 100 101 102 103 104 105
        $parts = explode('_', $token);
        $timestamp = (int) end($parts);
        if ($timestamp + $expire < time()) {
            // token expired
            return null;
        }

Alexander Makarov committed
106
        return static::findOne([
107 108 109 110 111 112 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
            '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
     *
139
     * @param string $password password to validate
140 141 142 143
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
144
        return Yii::$app->security->validatePassword($password, $this->password_hash);
145 146 147 148 149 150 151 152 153
    }

    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
154
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
155 156 157 158 159 160 161
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
162
        $this->auth_key = Yii::$app->security->generateRandomString();
163 164 165 166 167 168 169
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
170
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
171 172 173 174 175 176 177 178 179
    }

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