1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace modules\users\controllers\common; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\web\Controller; |
7
|
|
|
use modules\users\models\User; |
8
|
|
|
use modules\rbac\models\Assignment; |
9
|
|
|
use yii\filters\AccessControl; |
10
|
|
|
use yii\filters\VerbFilter; |
11
|
|
|
use yii\web\NotFoundHttpException; |
12
|
|
|
use yii\bootstrap\ActiveForm; |
13
|
|
|
use yii\web\Response; |
14
|
|
|
use modules\users\Module; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ProfileController |
18
|
|
|
* @package modules\users\controllers\common |
19
|
|
|
*/ |
20
|
|
|
class ProfileController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function behaviors() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
'verbs' => [ |
30
|
|
|
'class' => VerbFilter::className(), |
|
|
|
|
31
|
|
|
'actions' => [ |
32
|
|
|
'delete' => ['post'], |
33
|
|
|
], |
34
|
|
|
], |
35
|
|
|
'access' => [ |
36
|
|
|
'class' => AccessControl::className(), |
|
|
|
|
37
|
|
|
'rules' => [ |
38
|
|
|
[ |
39
|
|
|
'allow' => true, |
40
|
|
|
'roles' => ['@'] |
41
|
|
|
], |
42
|
|
|
], |
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
* @throws NotFoundHttpException |
50
|
|
|
*/ |
51
|
|
|
public function actionIndex() |
52
|
|
|
{ |
53
|
|
|
$model = $this->findModel(); |
54
|
|
|
|
55
|
|
|
$assignModel = new Assignment(); |
56
|
|
|
$assignModel->user = $model; |
57
|
|
|
|
58
|
|
|
return $this->render('index', [ |
59
|
|
|
'model' => $model, |
60
|
|
|
'assignModel' => $assignModel, |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
* @throws NotFoundHttpException |
67
|
|
|
*/ |
68
|
|
|
public function actionUpdate() |
69
|
|
|
{ |
70
|
|
|
$model = $this->findModel(); |
71
|
|
|
return $this->render('update', [ |
72
|
|
|
'model' => $model, |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Response |
78
|
|
|
* @throws NotFoundHttpException |
79
|
|
|
*/ |
80
|
|
|
public function actionUpdateProfile() |
81
|
|
|
{ |
82
|
|
|
$model = $this->findModel(); |
83
|
|
|
$model->scenario = $model::SCENARIO_PROFILE_UPDATE; |
84
|
|
|
|
85
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
86
|
|
|
Yii::$app->session->setFlash('success', Module::t('module', 'Profile successfully changed.')); |
|
|
|
|
87
|
|
|
} else { |
88
|
|
|
Yii::$app->session->setFlash('error', Module::t('module', 'Error! Profile not changed.')); |
89
|
|
|
} |
90
|
|
|
return $this->redirect(['update', 'tab' => 'profile']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array|Response |
95
|
|
|
* @throws NotFoundHttpException |
96
|
|
|
*/ |
97
|
|
|
public function actionUpdatePassword() |
98
|
|
|
{ |
99
|
|
|
$model = $this->findModel(); |
100
|
|
|
$model->scenario = $model::SCENARIO_PASSWORD_UPDATE; |
101
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
102
|
|
|
Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.')); |
103
|
|
|
} else { |
104
|
|
|
Yii::$app->session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.')); |
105
|
|
|
} |
106
|
|
|
return $this->redirect(['update', 'tab' => 'password']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array|Response |
111
|
|
|
* @throws NotFoundHttpException |
112
|
|
|
*/ |
113
|
|
|
public function actionAjaxValidatePasswordForm() |
114
|
|
|
{ |
115
|
|
|
$model = $this->findModel(); |
116
|
|
|
$model->scenario = $model::SCENARIO_PASSWORD_UPDATE; |
117
|
|
|
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
118
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
119
|
|
|
return ActiveForm::validate($model); |
120
|
|
|
} |
121
|
|
|
return $this->redirect(['index']); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Deletes an existing User model. |
126
|
|
|
* This delete set status blocked, is successful, logout and the browser will be redirected to the 'home' page. |
127
|
|
|
* @return Response |
128
|
|
|
* @throws NotFoundHttpException |
129
|
|
|
*/ |
130
|
|
|
public function actionDelete() |
131
|
|
|
{ |
132
|
|
|
$model = $this->findModel(); |
133
|
|
|
$model->scenario = $model::SCENARIO_PROFILE_DELETE; |
134
|
|
|
$model->status = $model::STATUS_DELETED; |
135
|
|
|
if ($model->save()) |
136
|
|
|
Yii::$app->user->logout(); |
|
|
|
|
137
|
|
|
return $this->goHome(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Action Generate new auth key |
142
|
|
|
* @throws NotFoundHttpException |
143
|
|
|
*/ |
144
|
|
|
public function actionGenerateAuthKey() |
145
|
|
|
{ |
146
|
|
|
$model = $this->processGenerateAuthKey(); |
147
|
|
|
if (Yii::$app->request->isAjax) { |
148
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
149
|
|
|
return [ |
150
|
|
|
'success' => $model->auth_key, |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
return $this->redirect(['index']); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Generate new auth key |
158
|
|
|
* @return User|null |
159
|
|
|
* @throws NotFoundHttpException |
160
|
|
|
*/ |
161
|
|
|
private function processGenerateAuthKey() |
162
|
|
|
{ |
163
|
|
|
$model = $this->findModel(); |
164
|
|
|
$model->generateAuthKey(); |
165
|
|
|
$model->save(); |
166
|
|
|
return $model; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Finds the User model based on its primary key value. |
171
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
172
|
|
|
* @return null|User the loaded model |
173
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
174
|
|
|
*/ |
175
|
|
|
private function findModel() |
176
|
|
|
{ |
177
|
|
|
if (!Yii::$app->user->isGuest) { |
178
|
|
|
/** @var object $identity */ |
179
|
|
|
$identity = Yii::$app->user->identity; |
180
|
|
|
if (($model = User::findOne($identity->id)) !== null) { |
181
|
|
|
return $model; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.')); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.