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