-
Notifications
You must be signed in to change notification settings - Fork 3
/
User.php
executable file
·143 lines (122 loc) · 3.19 KB
/
User.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
<?php
namespace common\modules\users\models;
use yii\db\ActiveRecord;
use Yii;
/**
* @inheritdoc
*/
class User extends \nepster\users\models\User
{
/**
* @var string
*/
public $repassword;
/**
* @inheritdoc
*/
public function scenarios()
{
return [
'signup' => ['phone', 'email', 'password', 'repassword'],
];
}
/**
* @inheritdoc
*/
public function transactions()
{
return [
'signup' => self::OP_ALL
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
$labels = parent::attributeLabels();
return array_merge($labels, [
'repassword' => Yii::t('users', 'REPASSWORD'),
]);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['phone', 'email', 'password', 'repassword'], 'required'],
[['phone', 'email', 'password', 'repassword'], 'trim'],
[['phone', 'email'], 'unique'],
['email', 'email'],
['password', '\nepster\users\validators\PasswordValidator'],
['repassword', 'compare', 'compareAttribute' => 'password'],
];
}
/**
* @inheritdoc
*/
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if ($this->isNewRecord) {
// Хешируем пароль
$this->setPassword($this->password);
// IP пользователя
if (!Yii::$app instanceof \yii\console\Application) {
$this->ip_register = Yii::$app->request->userIP;
}
}
return true;
}
return false;
}
/**
* @inheritdoc
*/
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
if ($insert) {
// Сохраняем профиль
$this->profile->user_id = $this->id;
$this->profile->save(false);
// Сохраняем данные юридического лица
if ($this->person) {
$this->person->user_id = $this->id;
$this->person->save(false);
}
}
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPerson()
{
return $this->hasOne(LegalPerson::className(), ['user_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProfile()
{
return $this->hasOne(Profile::className(), ['user_id' => 'id']);
}
/**
* Данные статуса
* @param null $status
* @return array|mixed
*/
public static function getStatusArray($status = null)
{
$statuses = [
self::STATUS_ACTIVE => Yii::t('users', 'STATUS_ACTIVE'),
self::STATUS_INACTIVE => Yii::t('users', 'STATUS_INACTIVE'),
self::STATUS_DELETED => Yii::t('users', 'STATUS_DELETED'),
];
if ($status !== null) {
return \yii\helpers\ArrayHelper::getValue($statuses, $status);
}
return $statuses;
}
}