User.php 4.21 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
	/**
	 * Creates a new user
	 *
34 35
	 * @param array $attributes the attributes given by field => value
	 * @return static|null the newly created model, or null on failure
36
	 */
37 38 39 40 41
	public static function create($attributes)
	{
		/** @var User $user */
		$user = new static();
		$user->setAttributes($attributes);
42 43
		$user->setPassword($attributes['password']);
		$user->generateAuthKey();
44 45 46 47 48 49 50
		if ($user->save()) {
			return $user;
		} else {
			return null;
		}
	}

51 52 53
	/**
	 * @inheritdoc
	 */
54 55
	public function behaviors()
	{
56 57
		return [
			'timestamp' => [
58
				'class' => 'yii\behaviors\TimestampBehavior',
59
				'attributes' => [
60
					ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
61
					ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
62 63 64
				],
			],
		];
65
	}
66

67
	/**
68
	 * @inheritdoc
69
	 */
70 71
	public static function findIdentity($id)
	{
72
		return static::find($id);
73 74
	}

Qiang Xue committed
75 76 77
	/**
	 * @inheritdoc
	 */
78
	public static function findIdentityByAccessToken($token)
Qiang Xue committed
79
	{
80
		throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
Qiang Xue committed
81 82
	}

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

94 95 96 97
	/**
	 * Finds user by password reset token
	 *
	 * @param string $token password reset token
98
	 * @return static|null
99 100 101
	 */
	public static function findByPasswordResetToken($token)
	{
102
		$expire = \Yii::$app->params['user.passwordResetTokenExpire'];
103 104 105 106 107 108 109
		$parts = explode('_', $token);
		$timestamp = (int)end($parts);
		if ($timestamp + $expire < time()) {
			// token expired
			return null;
		}

110
		return static::find([
111
			'password_reset_token' => $token,
112
			'status' => self::STATUS_ACTIVE,
113 114 115
		]);
	}

116
	/**
117
	 * @inheritdoc
118
	 */
119 120
	public function getId()
	{
121
		return $this->getPrimaryKey();
122 123
	}

124
	/**
125
	 * @inheritdoc
126
	 */
127 128
	public function getAuthKey()
	{
129
		return $this->auth_key;
130 131
	}

132
	/**
133
	 * @inheritdoc
134
	 */
135 136
	public function validateAuthKey($authKey)
	{
137
		return $this->getAuthKey() === $authKey;
138 139
	}

140
	/**
141 142
	 * Validates password
	 *
143
	 * @param string $password password to validate
144
	 * @return boolean if password provided is valid for current user
145
	 */
146 147
	public function validatePassword($password)
	{
148
		return Security::validatePassword($password, $this->password_hash);
149 150
	}

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

161 162 163 164 165 166 167 168
	/**
	 * Generates "remember me" authentication key
	 */
	public function generateAuthKey()
	{
		$this->auth_key = Security::generateRandomKey();
	}

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

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

185 186 187
	/**
	 * @inheritdoc
	 */
188 189
	public function rules()
	{
190
		return [
191
			['status', 'default', 'value' => self::STATUS_ACTIVE],
192
			['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
193 194

			['role', 'default', 'value' => self::ROLE_USER],
195 196
			['role', 'in', 'range' => [self::ROLE_USER]],

197 198
			['username', 'filter', 'filter' => 'trim'],
			['username', 'required'],
199
			['username', 'unique'],
200 201 202 203 204
			['username', 'string', 'min' => 2, 'max' => 255],

			['email', 'filter', 'filter' => 'trim'],
			['email', 'required'],
			['email', 'email'],
205
			['email', 'unique'],
206
		];
207
	}
208
}