User.php 3.22 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 72
	/**
	 * @return int|string current user ID
	 */
73 74 75 76 77
	public function getId()
	{
		return $this->id;
	}

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 107 108 109 110 111 112 113 114 115 116 117 118 119
		return [
			['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],
		];
120 121 122 123
	}

	public function scenarios()
	{
124 125 126 127 128
		return [
			'signup' => ['username', 'email', 'password'],
			'resetPassword' => ['password'],
			'requestPasswordResetToken' => ['email'],
		];
129 130 131 132
	}

	public function beforeSave($insert)
	{
resurtm committed
133
		if (parent::beforeSave($insert)) {
134
			if (($this->isNewRecord || $this->getScenario() === 'resetPassword') && !empty($this->password)) {
135
				$this->password_hash = Security::generatePasswordHash($this->password);
136
			}
resurtm committed
137
			if ($this->isNewRecord) {
138
				$this->auth_key = Security::generateRandomKey();
139 140 141 142
			}
			return true;
		}
		return false;
143 144
	}
}