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

4
use yii\db\ActiveRecord;
5
use yii\helpers\Security;
6
use yii\web\IdentityInterface;
7 8

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

	const ROLE_USER = 10;

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

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

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

74 75 76 77
	/**
	 * Finds user by username
	 *
	 * @param string $username
78
	 * @return static|null
79
	 */
80 81
	public static function findByUsername($username)
	{
82
		return static::find(['username' => $username, 'status' => self::STATUS_ACTIVE]);
83 84
	}

85 86 87 88
	/**
	 * Finds user by password reset token
	 *
	 * @param string $token password reset token
89
	 * @return static|null
90 91 92
	 */
	public static function findByPasswordResetToken($token)
	{
93
		$expire = \Yii::$app->params['user.passwordResetTokenExpire'];
94 95 96 97 98 99 100
		$parts = explode('_', $token);
		$timestamp = (int)end($parts);
		if ($timestamp + $expire < time()) {
			// token expired
			return null;
		}

101
		return static::find([
102
			'password_reset_token' => $token,
103
			'status' => self::STATUS_ACTIVE,
104 105 106
		]);
	}

107
	/**
108
	 * @inheritdoc
109
	 */
110 111
	public function getId()
	{
112
		return $this->getPrimaryKey();
113 114
	}

115
	/**
116
	 * @inheritdoc
117
	 */
118 119
	public function getAuthKey()
	{
120
		return $this->auth_key;
121 122
	}

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

131
	/**
132 133
	 * Validates password
	 *
134 135 136
	 * @param string $password password to validate
	 * @return bool if password provided is valid for current user
	 */
137 138
	public function validatePassword($password)
	{
139
		return Security::validatePassword($password, $this->password_hash);
140 141
	}

142 143 144 145 146 147 148 149 150 151
	/**
	 * Generates password hash from password and sets it to the model
	 *
	 * @param string $password
	 */
	public function setPassword($password)
	{
		$this->password_hash = Security::generatePasswordHash($password);
	}

152 153 154 155 156 157 158 159
	/**
	 * Generates "remember me" authentication key
	 */
	public function generateAuthKey()
	{
		$this->auth_key = Security::generateRandomKey();
	}

160 161 162 163 164
	/**
	 * Generates new password reset token
	 */
	public function generatePasswordResetToken()
	{
165
		$this->password_reset_token = Security::generateRandomKey() . '_' . time();
166 167 168 169 170 171 172
	}

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

176 177 178
	/**
	 * @inheritdoc
	 */
179 180
	public function rules()
	{
181
		return [
182
			['status', 'default', 'value' => self::STATUS_ACTIVE],
183
			['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
184 185

			['role', 'default', 'value' => self::ROLE_USER],
186 187
			['role', 'in', 'range' => [self::ROLE_USER]],

188 189 190 191 192 193 194
			['username', 'filter', 'filter' => 'trim'],
			['username', 'required'],
			['username', 'string', 'min' => 2, 'max' => 255],

			['email', 'filter', 'filter' => 'trim'],
			['email', 'required'],
			['email', 'email'],
195
			['email', 'unique'],
196
		];
197
	}
198
}