/
BaseUser.php
executable file
·152 lines (132 loc) · 3.29 KB
/
BaseUser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace vtvz\basics;
use vtvz\extrelations\Relations;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\web\IdentityInterface;
/**
* BaseUser class
*
* @property integer $id
* @property string $passwordHash
* @property timestamp $createdAt
* @property timestamp $updatedAt
* @property integer $authKey
* @property integer $status
*/
abstract class BaseUser extends ActiveRecord implements IdentityInterface
{
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;
public $statuses = [
'inactive' => self::STATUS_INACTIVE,
'active' => self::STATUS_ACTIVE,
];
public static $queryClass = 'vtvz\basics\UserQuery';
public static function find()
{
return Yii::createObject(static::$queryClass, [get_called_class()]);
}
public function behaviors()
{
return ArrayHelper::merge(
parent::behaviors(),
[
Relations::className(),
[
'class' => TimestampBehavior::className(),
]
]
);
}
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => $this->statuses],
['login', 'unique'],
];
}
public function makeActive()
{
$this->status = self::STATUS_ACTIVE;
$this->save();
}
public function isActive()
{
return $this->status == self::STATUS_ACTIVE;
}
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey()
{
$this->authKey = Yii::$app->security->generateRandomString() . '_' . time();
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password)
{
$this->passwordHash = Yii::$app->security->generatePasswordHash($password);
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->passwordHash);
}
/**
* Finds active user by login
*
* @param string $login
* @return static|null
*/
public static function findByLogin($login)
{
return static::findOne(['login' => $login, 'status' => self::STATUS_ACTIVE]);
}
/*
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->authKey;
}
}