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