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\web\admin\controllers; |
14
|
|
|
|
15
|
|
|
use rhosocial\user\User; |
16
|
|
|
use rhosocial\user\Profile; |
17
|
|
|
use rhosocial\user\forms\ChangePasswordForm; |
18
|
|
|
use rhosocial\user\forms\RegisterForm; |
19
|
|
|
use rhosocial\user\web\admin\Module; |
20
|
|
|
use Yii; |
21
|
|
|
use yii\bootstrap\ActiveForm; |
22
|
|
|
use yii\filters\AccessControl; |
23
|
|
|
use yii\filters\VerbFilter; |
24
|
|
|
use yii\web\Controller; |
25
|
|
|
use yii\web\BadRequestHttpException; |
26
|
|
|
use yii\web\ForbiddenHttpException; |
27
|
|
|
use yii\web\MethodNotAllowedHttpException; |
28
|
|
|
use yii\web\Response; |
29
|
|
|
use yii\web\ServerErrorHttpException; |
30
|
|
|
use yii\web\UnauthorizedHttpException; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @version 1.0 |
34
|
|
|
* @author vistart <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class UserController extends Controller |
37
|
|
|
{ |
38
|
|
|
public $layout = 'user'; |
39
|
|
|
const RESULT_SUCCESS = 'success'; |
40
|
|
|
const RESULT_FAILED = 'failed'; |
41
|
|
|
const SESSION_KEY_MESSAGE = 'session_key_message'; |
42
|
|
|
const SESSION_KEY_RESULT = 'session_key_result'; |
43
|
|
|
|
44
|
|
|
public $registerSuccessMessage; |
45
|
|
|
public $registerFailedMessage; |
46
|
|
|
|
47
|
|
|
public $deregisterSuccessMessage; |
48
|
|
|
public $deregisterFailedMessage; |
49
|
|
|
|
50
|
|
|
public $updateSuccessMessage; |
51
|
|
|
public $updateFailedMessage; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initialize messages. |
55
|
|
|
*/ |
56
|
|
|
protected function initMessages() |
57
|
|
|
{ |
58
|
|
|
if (!is_string($this->registerSuccessMessage)) { |
59
|
|
|
$this->registerSuccessMessage = Yii::t('user' ,'User Registered.'); |
60
|
|
|
} |
61
|
|
|
if (!is_string($this->registerFailedMessage)) { |
62
|
|
|
$this->registerFailedMessage = Yii::t('user', 'Register Failed.'); |
63
|
|
|
} |
64
|
|
|
if (!is_string($this->deregisterSuccessMessage)) { |
65
|
|
|
$this->deregisterSuccessMessage = Yii::t('user', 'User Deregistered.'); |
66
|
|
|
} |
67
|
|
|
if (!is_string($this->deregisterFailedMessage)) { |
68
|
|
|
$this->deregisterFailedMessage = Yii::t('user', 'Failed to Deregister User.'); |
69
|
|
|
} |
70
|
|
|
if (!is_string($this->updateSuccessMessage)) { |
71
|
|
|
$this->updateSuccessMessage = Yii::t('user', 'Updated.'); |
72
|
|
|
} |
73
|
|
|
if (!is_string($this->updateFailedMessage)) { |
74
|
|
|
$this->updateFailedMessage = Yii::t('user', 'Failed to Update.'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function init() |
82
|
|
|
{ |
83
|
|
|
$this->initMessages(); |
84
|
|
|
parent::init(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function behaviors() { |
91
|
|
|
return [ |
92
|
|
|
'access' => [ |
93
|
|
|
'class' => AccessControl::class, |
94
|
|
|
'rules' => [ |
95
|
|
|
[ // Disallow all unauthorized users to access this controller. |
96
|
|
|
'allow' => false, |
97
|
|
|
'roles' => ['?'], |
98
|
|
|
], |
99
|
|
|
[ // Allow the user who has the `viewUser` permission to access the `index` action. |
100
|
|
|
'actions' => ['index'], |
101
|
|
|
'allow' => true, |
102
|
|
|
'roles' => ['viewUser'], |
103
|
|
|
], |
104
|
|
|
[ // Disallow other non-admin users to access this controller. |
105
|
|
|
'allow' => false, |
106
|
|
|
'matchCallback' => function ($rule, $action) { |
|
|
|
|
107
|
|
|
return !Yii::$app->authManager->checkAccess(Yii::$app->user->identity, 'admin'); |
108
|
|
|
}, |
109
|
|
|
'denyCallback' => function ($rule, $action) { |
|
|
|
|
110
|
|
|
throw new UnauthorizedHttpException(Yii::t('user', 'You are not an administrator and have no access to this page.')); |
111
|
|
|
}, |
112
|
|
|
], |
113
|
|
|
[ // Disallow admin users to access deregister action directly, only `POST` accepted. |
114
|
|
|
'actions' => ['deregister'], |
115
|
|
|
'allow' => false, |
116
|
|
|
'matchCallback' => function ($rule, $action) { |
|
|
|
|
117
|
|
|
return strtoupper(Yii::$app->request->getMethod()) != 'POST'; |
118
|
|
|
}, |
119
|
|
|
'denyCallback' => function ($rule, $action) { |
|
|
|
|
120
|
|
|
throw new MethodNotAllowedHttpException(Yii::t('user', 'You cannot access this page directly.')); |
121
|
|
|
}, |
122
|
|
|
], |
123
|
|
|
[ // Allow admin user to access other views. |
124
|
|
|
// This is a final rule, if you want to add other rules, please put it before this rule. |
125
|
|
|
'allow' => true, |
126
|
|
|
'roles' => ['admin'], // Administrator can access this controller. |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
], |
130
|
|
|
'verbs' => [ |
131
|
|
|
'class' => VerbFilter::class, |
132
|
|
|
'actions' => [ |
133
|
|
|
'deregister' => ['post'], |
134
|
|
|
], |
135
|
|
|
], |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function actionIndex() |
143
|
|
|
{ |
144
|
|
|
$searchModel = Yii::$app->user->identity->getSearchModel(); |
145
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->get()); |
146
|
|
|
|
147
|
|
|
return $this->render('index', ['dataProvider' => $dataProvider, 'searchModel' => $searchModel]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Register new user. |
152
|
|
|
* @return string|\yii\web\Response |
153
|
|
|
*/ |
154
|
|
|
public function actionRegisterNewUser() |
155
|
|
|
{ |
156
|
|
|
$model = new RegisterForm(); |
157
|
|
|
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
158
|
|
|
Yii::$app->response = Response::FORMAT_JSON; |
159
|
|
|
return ActiveForm::validate($model); |
160
|
|
|
} |
161
|
|
|
if ($model->load(Yii::$app->request->post())) { |
162
|
|
|
try { |
163
|
|
|
if (($result = $model->register()) === true) { |
164
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS); |
165
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $model->model->getID() . ') ' . $this->registerSuccessMessage); |
166
|
|
|
return $this->redirect($model->continue ? '' : ['index']); |
167
|
|
|
} |
168
|
|
|
if ($result instanceof \Exception) { |
169
|
|
|
throw $result; |
170
|
|
|
} |
171
|
|
|
} catch (\Exception $ex) { |
172
|
|
|
Yii::error($ex->getMessage(), __METHOD__); |
173
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED); |
174
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $ex->getMessage()); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
return $this->render('register-new-user', ['model' => $model]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get user by ID. |
182
|
|
|
* @param string $id User ID. |
183
|
|
|
* @return User |
184
|
|
|
* @throws BadRequestHttpException throw if user not found. |
185
|
|
|
*/ |
186
|
|
|
protected function getUser($id) |
187
|
|
|
{ |
188
|
|
|
$class = Yii::$app->user->identityClass; |
189
|
|
|
if (!class_exists($class)) { |
190
|
|
|
return null; |
191
|
|
|
} |
192
|
|
|
$user = $class::find()->id($id)->one(); |
193
|
|
|
if (empty($user) || !($user instanceof User)) { |
194
|
|
|
throw new BadRequestHttpException(Yii::t('user', 'User Not Found.')); |
195
|
|
|
} |
196
|
|
|
return $user; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Deregister User. |
201
|
|
|
* @param string $id User ID. |
202
|
|
|
* @return string |
203
|
|
|
* @throws ServerErrorHttpException |
204
|
|
|
* @throws ForbiddenHttpException |
205
|
|
|
*/ |
206
|
|
|
public function actionDeregister($id) |
207
|
|
|
{ |
208
|
|
|
$id = (int)$id; |
209
|
|
|
if (Yii::$app->user->identity->getID() == $id) { |
210
|
|
|
throw new ForbiddenHttpException(Yii::t('user', 'You cannot deregister yourself.')); |
211
|
|
|
} |
212
|
|
|
$user = $this->getUser($id); |
213
|
|
|
try { |
214
|
|
|
$result = $user->deregister(); |
215
|
|
|
if ($result instanceof \Exception) { |
216
|
|
|
throw $result; |
217
|
|
|
} |
218
|
|
|
} catch (\Exception $ex) { |
219
|
|
|
throw new ServerErrorHttpException($ex->getMessage()); |
220
|
|
|
} |
221
|
|
|
if ($result !== true) { |
222
|
|
|
throw new ServerErrorHttpException(Yii::t('user', 'Failed to deregister user.')); |
223
|
|
|
} |
224
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS); |
225
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $user->getID() . ') ' . $this->deregisterSuccessMessage); |
226
|
|
|
return $this->redirect(['index']); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function actionView($id) |
230
|
|
|
{ |
231
|
|
|
$user = $this->getUser($id); |
232
|
|
|
return $this->render('view', ['user' => $user]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $id User ID. |
237
|
|
|
* @return string|\yii\web\Response |
238
|
|
|
* @throws BadRequestHttpException |
239
|
|
|
*/ |
240
|
|
|
public function actionUpdate($id) |
241
|
|
|
{ |
242
|
|
|
$user = $this->getUser($id); |
243
|
|
|
$model = $user->profile; |
244
|
|
|
if (empty($model)) { |
245
|
|
|
$model = $user->createProfile(); |
246
|
|
|
} |
247
|
|
|
$model->scenario = Profile::SCENARIO_UPDATE; |
248
|
|
|
if ($model->load(Yii::$app->request->post())) { |
249
|
|
|
if ($model->getGUID() != $user->getGUID()) { |
250
|
|
|
throw new BadRequestHttpException(Yii::t('user', 'Please do not forge parameters.')); |
251
|
|
|
} |
252
|
|
|
if ($model->save()) { |
253
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS); |
254
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $user->getID() . ') ' . $this->updateSuccessMessage); |
255
|
|
|
return $this->redirect(['update', 'id' => $id]); |
256
|
|
|
} |
257
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED); |
258
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $user->getID() . ') ' . $this->updateFailedMessage); |
259
|
|
|
} |
260
|
|
|
return $this->render('update', ['user' => $user, 'model' => $model]); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param string $id User ID. |
265
|
|
|
* @return string|\yii\web\Response |
266
|
|
|
*/ |
267
|
|
|
public function actionChangePassword($id) |
268
|
|
|
{ |
269
|
|
|
$user = $this->getUser($id); |
270
|
|
|
$model = new ChangePasswordForm(['user' => $user, 'scenario' => ChangePasswordForm::SCENARIO_ADMIN]); |
271
|
|
|
if ($model->load(Yii::$app->request->post())){ |
272
|
|
|
if ($model->changePassword()) { |
273
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS); |
274
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $user->getID() . ') ' . $this->updateSuccessMessage); |
275
|
|
|
return $this->redirect(['index', 'id' => $id]); |
276
|
|
|
} else { |
277
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED); |
278
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $user->getID() . ') ' . $this->updateFailedMessage); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
return $this->render('change-password', ['model' => $model]); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.