|
1
|
|
|
<?php |
|
2
|
|
|
namespace console\controllers; |
|
3
|
|
|
|
|
4
|
|
|
use Yii; |
|
5
|
|
|
use yii\console\Exception; |
|
6
|
|
|
use yii\db\ActiveRecord; |
|
7
|
|
|
use yii\console\ExitCode; |
|
8
|
|
|
use yii\console\Controller; |
|
9
|
|
|
use yii\console\widgets\Table; |
|
10
|
|
|
use common\models\User; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Manage system users |
|
14
|
|
|
*/ |
|
15
|
|
|
class UsersController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
public $defaultAction = 'list'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* List users |
|
21
|
|
|
*/ |
|
22
|
|
|
public function actionList() |
|
23
|
|
|
{ |
|
24
|
|
|
echo Table::widget([ |
|
25
|
|
|
'headers' => ['User', 'Status', 'Modified'], |
|
26
|
|
|
'rows' => array_map(function (User $user) { |
|
27
|
|
|
return [ |
|
28
|
|
|
$user->username, |
|
29
|
|
|
$user->status === User::STATUS_ACTIVE ? 'Active' : '', |
|
30
|
|
|
Yii::$app->formatter |
|
31
|
|
|
->asDatetime($user->updated_at, 'short'), |
|
32
|
|
|
]; |
|
33
|
|
|
}, User::find()->all()), |
|
34
|
|
|
]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Enable user |
|
39
|
|
|
* @param string $username User login |
|
40
|
|
|
* @return int |
|
41
|
|
|
*/ |
|
42
|
|
|
public function actionEnable($username) |
|
43
|
|
|
{ |
|
44
|
|
|
$user = $this->findModel($username); |
|
45
|
|
|
$user->status = User::STATUS_ACTIVE; |
|
46
|
|
|
$user->save(); |
|
47
|
|
|
$this->stdout("Successfully enabled\n"); |
|
48
|
|
|
return ExitCode::OK; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Disable user |
|
53
|
|
|
* @param string $username User login |
|
54
|
|
|
* @return int |
|
55
|
|
|
*/ |
|
56
|
|
|
public function actionDisable($username) |
|
57
|
|
|
{ |
|
58
|
|
|
$user = $this->findModel($username); |
|
59
|
|
|
$user->status = User::STATUS_DELETED; |
|
60
|
|
|
$user->save(); |
|
61
|
|
|
$this->stdout("Successfully disabled\n"); |
|
62
|
|
|
return ExitCode::OK; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Change password |
|
67
|
|
|
* @param string $username User login |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
|
|
public function actionPasswd($username) |
|
71
|
|
|
{ |
|
72
|
|
|
$user = $this->findModel($username); |
|
73
|
|
|
$password = $this->prompt('Please, input new password:', [ |
|
74
|
|
|
'required' => true, |
|
75
|
|
|
'validator' => function ($input, &$error) { |
|
76
|
|
|
if (strlen($input) < 6) { |
|
77
|
|
|
$error = 'Password must be at least 6 characters'; |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
return true; |
|
81
|
|
|
}, |
|
82
|
|
|
]); |
|
83
|
|
|
$user->setPassword($password); |
|
84
|
|
|
if (!$user->save()) { |
|
85
|
|
|
$this->stderr($user->getFirstError('password')); |
|
86
|
|
|
return ExitCode::DATAERR; |
|
87
|
|
|
} |
|
88
|
|
|
$this->stdout("Successfully changed\n"); |
|
89
|
|
|
return ExitCode::OK; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $username |
|
94
|
|
|
* @return User|null|ActiveRecord |
|
95
|
|
|
* @throws Exception |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function findModel($username) |
|
98
|
|
|
{ |
|
99
|
|
|
$user = User::find() |
|
100
|
|
|
->where([ 'username' => $username ]) |
|
101
|
|
|
->one(); |
|
102
|
|
|
if (!$user) { |
|
103
|
|
|
throw new Exception('User not found'); |
|
104
|
|
|
} |
|
105
|
|
|
return $user; |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|