1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\users\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\ArrayHelper; |
7
|
|
|
use yii\helpers\Html; |
8
|
|
|
use modules\users\traits\ModuleTrait; |
9
|
|
|
use modules\users\Module; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class User |
13
|
|
|
* @package modules\users\models |
14
|
|
|
*/ |
15
|
|
|
class User extends BaseUser |
16
|
|
|
{ |
17
|
|
|
use ModuleTrait; |
18
|
|
|
|
19
|
|
|
const LENGTH_STRING_PASSWORD_MIN = 6; |
20
|
|
|
const LENGTH_STRING_PASSWORD_MAX = 16; |
21
|
|
|
|
22
|
|
|
const SCENARIO_ADMIN_CREATE = 'adminCreate'; |
23
|
|
|
const SCENARIO_ADMIN_UPDATE = 'adminUpdate'; |
24
|
|
|
const SCENARIO_ADMIN_PASSWORD_UPDATE = 'adminPasswordUpdate'; |
25
|
|
|
const SCENARIO_PROFILE_UPDATE = 'profileUpdate'; |
26
|
|
|
const SCENARIO_PASSWORD_UPDATE = 'passwordUpdate'; |
27
|
|
|
const SCENARIO_PROFILE_DELETE = 'profileDelete'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $currentPassword; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $newPassword; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $newPasswordRepeat; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function rules() |
48
|
|
|
{ |
49
|
|
|
return ArrayHelper::merge(parent::rules(), [ |
50
|
|
|
[['newPassword', 'newPasswordRepeat'], 'required', 'on' => [self::SCENARIO_ADMIN_CREATE, self::SCENARIO_PASSWORD_UPDATE, self::SCENARIO_ADMIN_PASSWORD_UPDATE]], |
51
|
|
|
['newPassword', 'string', 'min' => self::LENGTH_STRING_PASSWORD_MIN], |
52
|
|
|
['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword'], |
53
|
|
|
['currentPassword', 'validateCurrentPassword', 'skipOnEmpty' => false, 'skipOnError' => false], |
54
|
|
|
]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $attribute |
59
|
|
|
*/ |
60
|
|
|
public function validateCurrentPassword($attribute) |
61
|
|
|
{ |
62
|
|
|
if (!empty($this->newPassword) && !empty($this->newPasswordRepeat) && !$this->hasErrors()) { |
63
|
|
|
if ($this->$attribute) { |
64
|
|
|
if (!$this->validatePassword($this->$attribute)) |
65
|
|
|
$this->addError($attribute, Module::t('module', 'Incorrect current password.')); |
66
|
|
|
} else { |
67
|
|
|
$this->addError($attribute, Module::t('module', 'Enter your current password.')); |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
$this->addError($attribute, Module::t('module', 'Not all fields are filled in correctly.')); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function scenarios() |
78
|
|
|
{ |
79
|
|
|
$scenarios = parent::scenarios(); |
80
|
|
|
$scenarios[self::SCENARIO_ADMIN_CREATE] = ['avatar', 'username', 'email', 'status', 'newPassword', 'newPasswordRepeat', 'registration_type', 'first_name', 'last_name']; |
81
|
|
|
$scenarios[self::SCENARIO_ADMIN_UPDATE] = ['username', 'email', 'status', 'first_name', 'last_name']; |
82
|
|
|
$scenarios[self::SCENARIO_ADMIN_PASSWORD_UPDATE] = ['newPassword', 'newPasswordRepeat']; |
83
|
|
|
$scenarios[self::SCENARIO_PASSWORD_UPDATE] = ['currentPassword', 'newPassword', 'newPasswordRepeat']; |
84
|
|
|
$scenarios[self::SCENARIO_PROFILE_UPDATE] = ['email', 'first_name', 'last_name']; |
85
|
|
|
$scenarios[self::SCENARIO_PROFILE_DELETE] = ['status']; |
86
|
|
|
$scenarios['default'] = ['username', 'email', 'password_hash', 'status', 'auth_key', 'email_confirm_token']; |
87
|
|
|
return $scenarios; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function attributeLabels() |
94
|
|
|
{ |
95
|
|
|
return ArrayHelper::merge(parent::attributeLabels(), [ |
96
|
|
|
'userRoleName' => Module::t('module', 'Role'), |
97
|
|
|
'currentPassword' => Module::t('module', 'Current Password'), |
98
|
|
|
'newPassword' => Module::t('module', 'New Password'), |
99
|
|
|
'newPasswordRepeat' => Module::t('module', 'Repeat Password'), |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Actions before saving |
105
|
|
|
* |
106
|
|
|
* @param bool $insert |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function beforeSave($insert) |
110
|
|
|
{ |
111
|
|
|
if (parent::beforeSave($insert)) { |
112
|
|
|
if (!empty($this->newPassword)) { |
113
|
|
|
$this->setPassword($this->newPassword); |
114
|
|
|
Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.')); |
115
|
|
|
} |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set Status |
123
|
|
|
*/ |
124
|
|
|
public function setStatus() |
125
|
|
|
{ |
126
|
|
|
if ($this->status == self::STATUS_ACTIVE) { |
127
|
|
|
$this->status = self::STATUS_BLOCKED; |
128
|
|
|
} else if ($this->status == self::STATUS_BLOCKED) { |
129
|
|
|
$this->status = self::STATUS_ACTIVE; |
130
|
|
|
} else if ($this->status == self::STATUS_WAIT) { |
131
|
|
|
$this->status = self::STATUS_ACTIVE; |
132
|
|
|
} else if ($this->status == self::STATUS_DELETED) { |
133
|
|
|
$this->status = self::STATUS_WAIT; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getUserFullName() |
141
|
|
|
{ |
142
|
|
|
$fullName = ''; |
143
|
|
|
if (Yii::$app->user->identity) { |
144
|
|
|
if ($this->first_name && $this->last_name) { |
145
|
|
|
$fullName = $this->first_name . ' ' . $this->last_name; |
146
|
|
|
} else if ($this->first_name) { |
147
|
|
|
$fullName = $this->first_name; |
148
|
|
|
} else if ($this->last_name) { |
149
|
|
|
$fullName = $this->last_name; |
150
|
|
|
} else { |
151
|
|
|
$fullName = $this->username; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
return Html::encode($fullName); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param integer|string $id |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function isSuperAdmin($id) |
162
|
|
|
{ |
163
|
|
|
$id = $id ? $id : $this->id; |
164
|
|
|
$authManager = Yii::$app->authManager; |
165
|
|
|
$roles = $authManager->getRolesByUser($id); |
166
|
|
|
foreach ($roles as $role) { |
167
|
|
|
if ($role->name == \modules\rbac\models\Role::ROLE_SUPER_ADMIN) |
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|