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

4
use yii\db\ActiveRecord;
5
use yii\helpers\Security;
6 7 8 9 10 11 12 13 14
use yii\web\Identity;

/**
 * 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 23
 * @property string $email
 * @property string $auth_key
 * @property integer $role
 * @property integer $status
 * @property integer $create_time
 * @property integer $update_time
 */
class User extends ActiveRecord implements Identity
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 37 38 39 40

	const STATUS_DELETED = 0;
	const STATUS_ACTIVE = 10;

	const ROLE_USER = 10;

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

48 49 50 51 52 53
	/**
	 * Finds an identity by the given ID.
	 *
	 * @param string|integer $id the ID to be looked for
	 * @return Identity|null the identity object that matches the given ID.
	 */
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(array('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 106 107 108
	}

	public function rules()
	{
		return array(
			array('username', 'filter', 'filter' => 'trim'),
			array('username', 'required'),
109
			array('username', 'string', 'min' => 2, 'max' => 255),
110 111 112 113

			array('email', 'filter', 'filter' => 'trim'),
			array('email', 'required'),
			array('email', 'email'),
114
			array('email', 'unique', 'message' => 'This email address has already been taken.', 'on' => 'signup'),
115
			array('email', 'exist', 'message' => 'There is no user with such email.', 'on' => 'requestPasswordResetToken'),
116 117

			array('password', 'required'),
118
			array('password', 'string', 'min' => 6),
119 120 121 122 123 124 125 126
		);
	}

	public function scenarios()
	{
		return array(
			'signup' => array('username', 'email', 'password'),
			'login' => array('username', 'password'),
127
			'resetPassword' => array('password'),
128
			'requestPasswordResetToken' => array('email'),
129 130 131 132 133
		);
	}

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