User.php 3.78 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 33 34
	public static function create($attributes)
	{
		/** @var User $user */
		$user = new static();
		$user->setAttributes($attributes);
35 36
		$user->setPassword($attributes['password']);
		$user->generateAuthKey();
37 38 39 40 41 42 43
		if ($user->save()) {
			return $user;
		} else {
			return null;
		}
	}

44 45 46
	/**
	 * @inheritdoc
	 */
47 48
	public function behaviors()
	{
49 50
		return [
			'timestamp' => [
51
				'class' => 'yii\behaviors\AutoTimestamp',
52
				'attributes' => [
53 54
					ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
					ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
55 56 57
				],
			],
		];
58
	}
59

60
	/**
61
	 * @inheritdoc
62
	 */
63 64
	public static function findIdentity($id)
	{
65
		return static::find($id);
66 67
	}

68 69 70 71
	/**
	 * Finds user by username
	 *
	 * @param string $username
72
	 * @return self
73
	 */
74 75
	public static function findByUsername($username)
	{
76
		return static::find(['username' => $username, 'status' => static::STATUS_ACTIVE]);
77 78
	}

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

		return User::find([
			'password_reset_token' => $token,
			'status' => User::STATUS_ACTIVE,
		]);
	}

101
	/**
102
	 * @inheritdoc
103
	 */
104 105
	public function getId()
	{
106
		return $this->getPrimaryKey();
107 108
	}

109
	/**
110
	 * @inheritdoc
111
	 */
112 113
	public function getAuthKey()
	{
114
		return $this->auth_key;
115 116
	}

117
	/**
118
	 * @inheritdoc
119
	 */
120 121
	public function validateAuthKey($authKey)
	{
122
		return $this->getAuthKey() === $authKey;
123 124
	}

125
	/**
126 127
	 * Validates password
	 *
128 129 130
	 * @param string $password password to validate
	 * @return bool if password provided is valid for current user
	 */
131 132
	public function validatePassword($password)
	{
133
		return Security::validatePassword($password, $this->password_hash);
134 135
	}

136 137 138 139 140 141 142 143 144 145
	/**
	 * Generates password hash from password and sets it to the model
	 *
	 * @param string $password
	 */
	public function setPassword($password)
	{
		$this->password_hash = Security::generatePasswordHash($password);
	}

146 147 148 149 150 151 152 153
	/**
	 * Generates "remember me" authentication key
	 */
	public function generateAuthKey()
	{
		$this->auth_key = Security::generateRandomKey();
	}

154 155 156 157 158
	/**
	 * Generates new password reset token
	 */
	public function generatePasswordResetToken()
	{
159
		$this->password_reset_token = Security::generateRandomKey() . '_' . time();
160 161 162 163 164 165 166
	}

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

170 171 172
	/**
	 * @inheritdoc
	 */
173 174
	public function rules()
	{
175
		return [
176
			['status', 'default', 'value' => self::STATUS_ACTIVE],
177
			['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
178 179

			['role', 'default', 'value' => self::ROLE_USER],
180 181
			['role', 'in', 'range' => [self::ROLE_USER]],

182 183 184 185 186 187 188
			['username', 'filter', 'filter' => 'trim'],
			['username', 'required'],
			['username', 'string', 'min' => 2, 'max' => 255],

			['email', 'filter', 'filter' => 'trim'],
			['email', 'required'],
			['email', 'email'],
189
			['email', 'unique'],
190
		];
191
	}
192
}