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