|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
namespace modules\users\commands;
|
|
4
|
|
|
|
|
5
|
|
|
use Yii;
|
|
6
|
|
|
use yii\console\Controller;
|
|
7
|
|
|
use yii\console\Exception;
|
|
8
|
|
|
use console\components\helpers\Console;
|
|
9
|
|
|
use modules\users\models\User;
|
|
10
|
|
|
use modules\users\Module;
|
|
11
|
|
|
|
|
12
|
|
|
/**
|
|
13
|
|
|
* Console user actions
|
|
14
|
|
|
* @package app\modules\user\commands
|
|
15
|
|
|
*/
|
|
16
|
|
|
class UserController extends Controller
|
|
17
|
|
|
{
|
|
18
|
|
|
/**
|
|
19
|
|
|
* Color
|
|
20
|
|
|
* @var bool
|
|
21
|
|
|
*/
|
|
22
|
|
|
public $color = true;
|
|
23
|
|
|
|
|
24
|
|
|
/**
|
|
25
|
|
|
* Console user actions
|
|
26
|
|
|
* @inheritdoc
|
|
27
|
|
|
*/
|
|
28
|
|
|
public function actionIndex()
|
|
29
|
|
|
{
|
|
30
|
|
|
echo 'yii users/user/create' . PHP_EOL;
|
|
31
|
|
|
echo 'yii users/user/remove' . PHP_EOL;
|
|
32
|
|
|
echo 'yii users/user/activate' . PHP_EOL;
|
|
33
|
|
|
echo 'yii users/user/set-status' . PHP_EOL;
|
|
34
|
|
|
echo 'yii users/user/get-status' . PHP_EOL;
|
|
35
|
|
|
echo 'yii users/user/change-password' . PHP_EOL;
|
|
36
|
|
|
}
|
|
37
|
|
|
|
|
38
|
|
|
/**
|
|
39
|
|
|
* Create new user
|
|
40
|
|
|
* @inheritdoc
|
|
41
|
|
|
*/
|
|
42
|
|
|
public function actionCreate()
|
|
43
|
|
|
{
|
|
44
|
|
|
$model = new User();
|
|
45
|
|
|
$this->readValue($model, 'username');
|
|
46
|
|
|
$this->readValue($model, 'email');
|
|
47
|
|
|
$model->setPassword($this->prompt(Module::t('module', 'Password:'), [
|
|
48
|
|
|
'required' => true,
|
|
49
|
|
|
'pattern' => '#^.{6,255}$#i',
|
|
50
|
|
|
'error' => Module::t('module', 'More than 6 symbols'),
|
|
51
|
|
|
]));
|
|
52
|
|
|
$model->generateAuthKey();
|
|
53
|
|
|
if (($select = User::getStatusesArray()) && is_array($select)) {
|
|
54
|
|
|
$model->status = $this->select(Module::t('module', 'Status:'), $select);
|
|
|
|
|
|
|
55
|
|
|
$this->log($model->save());
|
|
56
|
|
|
} else {
|
|
57
|
|
|
$this->log();
|
|
58
|
|
|
}
|
|
59
|
|
|
}
|
|
60
|
|
|
|
|
61
|
|
|
/**
|
|
62
|
|
|
* Remove user
|
|
63
|
|
|
* @throws Exception
|
|
64
|
|
|
* @throws \Exception
|
|
65
|
|
|
* @throws \Throwable
|
|
66
|
|
|
*/
|
|
67
|
|
|
public function actionRemove()
|
|
68
|
|
|
{
|
|
69
|
|
|
$username = $this->prompt(Module::t('module', 'Username') . ':', ['required' => true]);
|
|
70
|
|
|
$model = $this->findModel($username);
|
|
71
|
|
|
if ($model->delete() !== false) {
|
|
72
|
|
|
$this->log(true);
|
|
73
|
|
|
} else {
|
|
74
|
|
|
$this->log(false);
|
|
75
|
|
|
}
|
|
76
|
|
|
}
|
|
77
|
|
|
|
|
78
|
|
|
/**
|
|
79
|
|
|
* Change status user to active
|
|
80
|
|
|
* @throws Exception
|
|
81
|
|
|
*/
|
|
82
|
|
|
public function actionActivate()
|
|
83
|
|
|
{
|
|
84
|
|
|
$username = $this->prompt(Module::t('module', 'Username:'), ['required' => true]);
|
|
85
|
|
|
$model = $this->findModel($username);
|
|
86
|
|
|
$model->status = User::STATUS_ACTIVE;
|
|
87
|
|
|
$model->removeEmailConfirmToken();
|
|
88
|
|
|
$this->log($model->save());
|
|
89
|
|
|
}
|
|
90
|
|
|
|
|
91
|
|
|
/**
|
|
92
|
|
|
* Get status name to user
|
|
93
|
|
|
* @throws Exception
|
|
94
|
|
|
*/
|
|
95
|
|
|
public function actionGetStatus()
|
|
96
|
|
|
{
|
|
97
|
|
|
$username = $this->prompt(Module::t('module', 'Username') . ':', ['required' => true]);
|
|
98
|
|
|
$model = $this->findModel($username);
|
|
99
|
|
|
$this->stdout($model->statusName, Console::FG_GREEN, Console::BOLD);
|
|
100
|
|
|
}
|
|
101
|
|
|
|
|
102
|
|
|
/**
|
|
103
|
|
|
* Change select status user
|
|
104
|
|
|
* @throws Exception
|
|
105
|
|
|
*/
|
|
106
|
|
|
public function actionSetStatus()
|
|
107
|
|
|
{
|
|
108
|
|
|
$username = $this->prompt(Module::t('module', 'Username') . ':', ['required' => true]);
|
|
109
|
|
|
$model = $this->findModel($username);
|
|
110
|
|
|
if (($select = User::getStatusesArray()) && is_array($select)) {
|
|
111
|
|
|
$model->status = $this->select(Module::t('module', 'Status') . ':', $select);
|
|
|
|
|
|
|
112
|
|
|
$this->log($model->save());
|
|
113
|
|
|
}
|
|
114
|
|
|
}
|
|
115
|
|
|
|
|
116
|
|
|
/**
|
|
117
|
|
|
* Change password
|
|
118
|
|
|
* @throws Exception
|
|
119
|
|
|
* @throws \yii\base\Exception
|
|
120
|
|
|
*/
|
|
121
|
|
|
public function actionChangePassword()
|
|
122
|
|
|
{
|
|
123
|
|
|
$username = $this->prompt(Module::t('module', 'Username:'), ['required' => true]);
|
|
124
|
|
|
$model = $this->findModel($username);
|
|
125
|
|
|
$model->setPassword($this->prompt(Module::t('module', 'New password:'), [
|
|
126
|
|
|
'required' => true,
|
|
127
|
|
|
'pattern' => '#^.{6,255}$#i',
|
|
128
|
|
|
'error' => Module::t('module', 'More than 6 symbols'),
|
|
129
|
|
|
]));
|
|
130
|
|
|
$this->log($model->save());
|
|
131
|
|
|
}
|
|
132
|
|
|
|
|
133
|
|
|
/**
|
|
134
|
|
|
* Find model user
|
|
135
|
|
|
* @param string $username
|
|
136
|
|
|
* @throws \yii\console\Exception
|
|
137
|
|
|
* @return User the loaded model
|
|
138
|
|
|
*/
|
|
139
|
|
|
private function findModel($username)
|
|
140
|
|
|
{
|
|
141
|
|
|
if (!$model = User::findOne(['username' => $username])) {
|
|
142
|
|
|
throw new Exception(
|
|
143
|
|
|
Module::t('module', 'User "{:Username}" not found', [':Username' => $username])
|
|
144
|
|
|
);
|
|
145
|
|
|
}
|
|
146
|
|
|
return $model;
|
|
147
|
|
|
}
|
|
148
|
|
|
|
|
149
|
|
|
/**
|
|
150
|
|
|
* @param \yii\base\Model $model
|
|
151
|
|
|
* @param string $attribute
|
|
152
|
|
|
*/
|
|
153
|
|
|
private function readValue($model = null, $attribute = '')
|
|
154
|
|
|
{
|
|
155
|
|
|
$model->$attribute = $this->prompt(Module::t('module', mb_convert_case($attribute, MB_CASE_TITLE, 'UTF-8')) . ':', [
|
|
156
|
|
|
'validator' => function ($input, &$error) use ($model, $attribute) {
|
|
157
|
|
|
/** @var string $input */
|
|
158
|
|
|
$model->$attribute = $input;
|
|
159
|
|
|
/** @var \yii\base\Model $model */
|
|
160
|
|
|
if ($model->validate([$attribute])) {
|
|
161
|
|
|
return true;
|
|
162
|
|
|
} else {
|
|
163
|
|
|
$error = implode(',', $model->getErrors($attribute));
|
|
164
|
|
|
return false;
|
|
165
|
|
|
}
|
|
166
|
|
|
},
|
|
167
|
|
|
]);
|
|
168
|
|
|
}
|
|
169
|
|
|
|
|
170
|
|
|
/**
|
|
171
|
|
|
* @param bool|int $success
|
|
172
|
|
|
*/
|
|
173
|
|
|
private function log($success = false)
|
|
174
|
|
|
{
|
|
175
|
|
|
if ($success === true || $success !== 0) {
|
|
176
|
|
|
$this->stdout(Module::t('module', 'Success!'), Console::FG_GREEN, Console::BOLD);
|
|
177
|
|
|
} else {
|
|
178
|
|
|
$this->stderr(Module::t('module', 'Error!'), Console::FG_RED, Console::BOLD);
|
|
179
|
|
|
}
|
|
180
|
|
|
echo PHP_EOL;
|
|
181
|
|
|
}
|
|
182
|
|
|
}
|
|
183
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.