User.php 3.47 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 10 11 12 13 14

/**
 * Class User
 * @package common\models
 *
 * @property integer $id
 * @property string $username
 * @property string $password_hash
15
 * @property string $password_reset_token
16 17 18 19 20 21 22
 * @property string $email
 * @property string $auth_key
 * @property integer $role
 * @property integer $status
 * @property integer $create_time
 * @property integer $update_time
 */
23
class User extends ActiveRecord implements IdentityInterface
24
{
25 26 27
	/**
	 * @var string the raw password. Used to collect password input and isn't saved in database
	 */
28
	public $password;
29 30 31 32 33 34 35 36

	const STATUS_DELETED = 0;
	const STATUS_ACTIVE = 10;

	const ROLE_USER = 10;

	public function behaviors()
	{
37 38
		return [
			'timestamp' => [
39
				'class' => 'yii\behaviors\AutoTimestamp',
40 41
				'attributes' => [
					ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
42
					ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
43 44 45
				],
			],
		];
46
	}
47

48 49 50 51
	/**
	 * Finds an identity by the given ID.
	 *
	 * @param string|integer $id the ID to be looked for
52
	 * @return IdentityInterface|null the identity object that matches the given ID.
53
	 */
54 55
	public static function findIdentity($id)
	{
56
		return static::find($id);
57 58
	}

59 60 61 62 63 64
	/**
	 * Finds user by username
	 *
	 * @param string $username
	 * @return null|User
	 */
65 66
	public static function findByUsername($username)
	{
67
		return static::find(['username' => $username, 'status' => static::STATUS_ACTIVE]);
68 69
	}

70
	/**
71
	 * @return int|string|array current user ID
72
	 */
73 74
	public function getId()
	{
75
		return $this->getPrimaryKey();
76 77
	}

78 79 80
	/**
	 * @return string current user auth key
	 */
81 82
	public function getAuthKey()
	{
83
		return $this->auth_key;
84 85
	}

86 87 88 89
	/**
	 * @param string $authKey
	 * @return boolean if auth key is valid for current user
	 */
90 91
	public function validateAuthKey($authKey)
	{
92
		return $this->getAuthKey() === $authKey;
93 94
	}

95 96 97 98
	/**
	 * @param string $password password to validate
	 * @return bool if password provided is valid for current user
	 */
99 100
	public function validatePassword($password)
	{
101
		return Security::validatePassword($password, $this->password_hash);
102 103 104 105
	}

	public function rules()
	{
106
		return [
107 108 109 110 111 112
			['status', 'default', 'value' => self::STATUS_ACTIVE],
			['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],

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

113 114 115 116 117 118 119 120 121 122 123 124 125
			['username', 'filter', 'filter' => 'trim'],
			['username', 'required'],
			['username', 'string', 'min' => 2, 'max' => 255],

			['email', 'filter', 'filter' => 'trim'],
			['email', 'required'],
			['email', 'email'],
			['email', 'unique', 'message' => 'This email address has already been taken.', 'on' => 'signup'],
			['email', 'exist', 'message' => 'There is no user with such email.', 'on' => 'requestPasswordResetToken'],

			['password', 'required'],
			['password', 'string', 'min' => 6],
		];
126 127 128 129
	}

	public function scenarios()
	{
130 131 132 133 134
		return [
			'signup' => ['username', 'email', 'password'],
			'resetPassword' => ['password'],
			'requestPasswordResetToken' => ['email'],
		];
135 136 137 138
	}

	public function beforeSave($insert)
	{
resurtm committed
139
		if (parent::beforeSave($insert)) {
140
			if (($this->isNewRecord || $this->getScenario() === 'resetPassword') && !empty($this->password)) {
141
				$this->password_hash = Security::generatePasswordHash($this->password);
142
			}
resurtm committed
143
			if ($this->isNewRecord) {
144
				$this->auth_key = Security::generateRandomKey();
145 146 147 148
			}
			return true;
		}
		return false;
149 150
	}
}