|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link https://vistart.me/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
|
10
|
|
|
* @license https://vistart.me/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace rhosocial\user\console\controllers; |
|
14
|
|
|
|
|
15
|
|
|
use rhosocial\user\User; |
|
16
|
|
|
use rhosocial\user\Profile; |
|
17
|
|
|
use yii\console\Controller; |
|
18
|
|
|
use yii\console\Exception; |
|
19
|
|
|
use Yii; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The simple operations associated with User. |
|
23
|
|
|
* |
|
24
|
|
|
* @version 1.0 |
|
25
|
|
|
* @author vistart <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class UserController extends Controller |
|
28
|
|
|
{ |
|
29
|
|
|
public $userClass; |
|
30
|
|
|
|
|
31
|
|
|
public $defaultAction = 'show'; |
|
32
|
|
|
|
|
33
|
|
|
protected function checkUserClass() |
|
34
|
|
|
{ |
|
35
|
|
|
$userClass = $this->userClass; |
|
36
|
|
|
if (!class_exists($userClass)) { |
|
37
|
|
|
throw new Exception('User Class Invalid.'); |
|
38
|
|
|
} |
|
39
|
|
|
if (!((new $userClass()) instanceof User)) { |
|
40
|
|
|
throw new Exception('User Class(' . $userClass . ') does not inherited from `\rhosocial\user\User`.'); |
|
41
|
|
|
} |
|
42
|
|
|
return $userClass; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get user from database. |
|
47
|
|
|
* @param User|string|integer $user |
|
48
|
|
|
* @return User |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getUser($user) |
|
51
|
|
|
{ |
|
52
|
|
|
$userClass = $this->checkUserClass(); |
|
53
|
|
|
if (is_numeric($user)) { |
|
54
|
|
|
$user = $userClass::find()->id($user)->one(); |
|
55
|
|
|
} elseif (is_string($user) && strlen($user)) { |
|
56
|
|
|
$user = $userClass::find()->guid($user)->one(); |
|
57
|
|
|
} |
|
58
|
|
|
if (!$user || $user->getIsNewRecord()) { |
|
59
|
|
|
throw new Exception('User Not Registered.'); |
|
60
|
|
|
} |
|
61
|
|
|
return $user; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Register new User. |
|
66
|
|
|
* @param string $password Password. |
|
67
|
|
|
* @param string $nickname If profile contains this property, this parameter is required. |
|
68
|
|
|
* @param string $firstName If profile contains this property, this parameter is required. |
|
69
|
|
|
* @param string $lastName If profile contains this propery, this parameter is required. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function actionRegister($password, $nickname = null, $firstName = null, $lastName = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$userClass = $this->checkUserClass(); |
|
74
|
|
|
|
|
75
|
|
|
$user = new $userClass(['password' => $password]); |
|
76
|
|
|
/* @var $user User */ |
|
77
|
|
|
$profile = $user->createProfile([ |
|
78
|
|
|
'nickname' => $nickname, |
|
79
|
|
|
'first_name' => $firstName, |
|
80
|
|
|
'last_name' => $lastName, |
|
81
|
|
|
]); |
|
82
|
|
|
/* @var $profile Profile */ |
|
83
|
|
|
try { |
|
84
|
|
|
is_null($profile) ? $user->register(): $user->register([$profile]); |
|
85
|
|
|
} catch (\Exception $ex) { |
|
86
|
|
|
throw new Exception($ex->getMessage()); |
|
87
|
|
|
} |
|
88
|
|
|
echo "User Registered:\n"; |
|
89
|
|
|
return $this->actionShow($user); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Deregister user. |
|
94
|
|
|
* @param User|string|integer $user The user to be deregistered. |
|
95
|
|
|
* @return boolean |
|
96
|
|
|
*/ |
|
97
|
|
|
public function actionDeregister($user) |
|
98
|
|
|
{ |
|
99
|
|
|
$user = $this->getUser($user); |
|
100
|
|
|
if ($user->deregister()) { |
|
101
|
|
|
echo "User (" . $user->getID() . ") Deregistered.\n"; |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Show User Information. |
|
109
|
|
|
* @param User|string|integer $user |
|
110
|
|
|
* @param boolean $guid |
|
111
|
|
|
* @param boolean $passHash |
|
112
|
|
|
* @param boolean $accessToken |
|
113
|
|
|
* @param boolean $authKey |
|
114
|
|
|
*/ |
|
115
|
|
|
public function actionShow($user, $guid = false, $passHash = false, $accessToken = false, $authKey = false) |
|
116
|
|
|
{ |
|
117
|
|
|
$user = $this->getUser($user); |
|
118
|
|
|
echo Yii::t('app', 'User') . " (" . $user->getID() . "), " . Yii::t('app', 'registered at') . " (" . $user->getCreatedAt() . ")" |
|
119
|
|
|
. ($user->getCreatedAt() == $user->getUpdatedAt() ? "" : ", " . Yii::t('app', 'last updated at') . " (" . $user->getUpdatedAt() . ")") .".\n"; |
|
120
|
|
|
if ($guid) { |
|
121
|
|
|
echo "GUID: " . $user->getGUID() . "\n"; |
|
122
|
|
|
} |
|
123
|
|
|
if ($passHash) { |
|
124
|
|
|
echo "Password Hash: " . $user->{$user->passwordHashAttribute} . "\n"; |
|
125
|
|
|
} |
|
126
|
|
|
if ($accessToken) { |
|
127
|
|
|
echo "Access Token: " . $user->getAccessToken() . "\n"; |
|
128
|
|
|
} |
|
129
|
|
|
if ($authKey) { |
|
130
|
|
|
echo "Authentication Key: " . $user->getAuthKey() . "\n"; |
|
131
|
|
|
} |
|
132
|
|
|
return true; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Show statistics. |
|
137
|
|
|
* @param User|string|integer $user |
|
138
|
|
|
* @return boolean |
|
139
|
|
|
*/ |
|
140
|
|
|
public function actionStat($user = null) |
|
141
|
|
|
{ |
|
142
|
|
|
if ($user === null) { |
|
143
|
|
|
$count = User::find()->count(); |
|
144
|
|
|
echo "Total number of user(s): " . $count . "\n"; |
|
145
|
|
|
if ($count == 0) { |
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
$last = User::find()->orderByCreatedAt(SORT_DESC)->one(); |
|
149
|
|
|
/* @var $last User */ |
|
150
|
|
|
echo "Latest user (" . $last->getID() . ") registered at " . $last->getCreatedAt() . "\n"; |
|
151
|
|
|
return true; |
|
152
|
|
|
} |
|
153
|
|
|
$user = $this->getUser($user); |
|
|
|
|
|
|
154
|
|
|
return true; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Assign a role to user or revoke a role. |
|
159
|
|
|
* @param User|string|integer $user |
|
160
|
|
|
* @param string $operation Only `assign` and `revoke` are acceptable. |
|
161
|
|
|
* @param string $role |
|
162
|
|
|
*/ |
|
163
|
|
|
public function actionRole($user, $operation, $role) |
|
164
|
|
|
{ |
|
165
|
|
|
$user = $this->getUser($user); |
|
166
|
|
|
$role = Yii::$app->authManager->getRole($role); |
|
167
|
|
|
if ($operation == 'assign') { |
|
168
|
|
|
try { |
|
169
|
|
|
$assignment = Yii::$app->authManager->assign($role, $user); |
|
170
|
|
|
} catch (\yii\db\IntegrityException $ex) { |
|
171
|
|
|
echo "Failed to assign `" . $role->name . "`.\n"; |
|
172
|
|
|
echo "Maybe the role has been assigned.\n"; |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
if ($assignment) { |
|
176
|
|
|
echo "`$role->name`" . " assigned to User (" . $user->getID() . ") successfully.\n"; |
|
177
|
|
|
} else { |
|
178
|
|
|
echo "Failed to assign `" . $role->name . "`.\n"; |
|
179
|
|
|
} |
|
180
|
|
|
return true; |
|
181
|
|
|
} |
|
182
|
|
|
if ($operation == 'revoke') { |
|
183
|
|
|
$assignment = Yii::$app->authManager->revoke($role, $user); |
|
184
|
|
|
if ($assignment) { |
|
185
|
|
|
echo "`$role->name`" . " revoked from User (" . $user->getID() . ").\n"; |
|
186
|
|
|
} else { |
|
187
|
|
|
echo "Failed to revoke `" . $role->name . "`.\n"; |
|
188
|
|
|
echo "Maybe the role has not been assigned yet.\n"; |
|
189
|
|
|
} |
|
190
|
|
|
return true; |
|
191
|
|
|
} |
|
192
|
|
|
echo "Unrecognized operation: $operation.\n"; |
|
193
|
|
|
echo "The accepted operations are `assign` and `revoke`.\n"; |
|
194
|
|
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Assign a permission to user or revoke a permission. |
|
199
|
|
|
* @param User|string|integer $user |
|
200
|
|
|
* @param string $operation Only `assign` and `revoke` are acceptable. |
|
201
|
|
|
* @param string $permission |
|
202
|
|
|
*/ |
|
203
|
|
|
public function actionPermission($user, $operation, $permission) |
|
204
|
|
|
{ |
|
205
|
|
|
$user = $this->getUser($user); |
|
206
|
|
|
$permission = Yii::$app->authManager->getPermission($permission); |
|
207
|
|
|
if ($operation == 'assign') { |
|
208
|
|
|
try { |
|
209
|
|
|
$assignment = Yii::$app->authManager->assign($permission, $user); |
|
210
|
|
|
} catch (\yii\db\IntegrityException $ex) { |
|
211
|
|
|
echo "Failed to assign `" . $role->name . "`.\n"; |
|
|
|
|
|
|
212
|
|
|
echo "Maybe the permission has been assigned.\n"; |
|
213
|
|
|
return false; |
|
214
|
|
|
} |
|
215
|
|
|
if ($assignment) { |
|
216
|
|
|
echo "`$permission->name`" . " assigned to User (" . $user->getID() . ") successfully.\n"; |
|
217
|
|
|
} else { |
|
218
|
|
|
echo "Failed to assign `" . $permission->name . "`.\n"; |
|
219
|
|
|
} |
|
220
|
|
|
return true; |
|
221
|
|
|
} |
|
222
|
|
|
if ($operation == 'revoke') { |
|
223
|
|
|
$assignment = Yii::$app->authManager->revoke($permission, $user); |
|
224
|
|
|
if ($assignment) { |
|
225
|
|
|
echo "`$permission->name`" . " revoked from User (" . $user->getID() . ").\n"; |
|
226
|
|
|
} else { |
|
227
|
|
|
echo "Failed to revoke `" . $permission->name . "`.\n"; |
|
228
|
|
|
echo "Maybe the permission has not been assigned yet.\n"; |
|
229
|
|
|
} |
|
230
|
|
|
return true; |
|
231
|
|
|
} |
|
232
|
|
|
echo "Unrecognized operation: $operation.\n"; |
|
233
|
|
|
echo "The accepted operations are `assign` and `revoke`.\n"; |
|
234
|
|
|
return false; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Validate password. |
|
239
|
|
|
* @param User|string|integer $user |
|
240
|
|
|
* @param password $password |
|
241
|
|
|
*/ |
|
242
|
|
|
public function actionValidatePassword($user, $password) |
|
243
|
|
|
{ |
|
244
|
|
|
$user = $this->getUser($user); |
|
245
|
|
|
$result = $user->validatePassword($password); |
|
246
|
|
|
if ($result) { |
|
247
|
|
|
echo "Correct.\n"; |
|
248
|
|
|
} else { |
|
249
|
|
|
echo "Incorrect.\n"; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Change password directly. |
|
255
|
|
|
* @param User|string|integer $user |
|
256
|
|
|
* @param string $password |
|
257
|
|
|
*/ |
|
258
|
|
|
public function actionPassword($user, $password) |
|
259
|
|
|
{ |
|
260
|
|
|
$user = $this->getUser($user); |
|
261
|
|
|
$user->applyForNewPassword(); |
|
262
|
|
|
$result = $user->resetPassword($password, $user->getPasswordResetToken()); |
|
263
|
|
|
if ($result) { |
|
264
|
|
|
echo "Password changed.\n"; |
|
265
|
|
|
} else { |
|
266
|
|
|
echo "Password not changed.\n"; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.