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
|
|
|
|
102
|
|
|
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
103
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
104
|
|
|
return ActiveForm::validate($model); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
108
|
|
|
Yii::$app->session->setFlash('success', Module::t('module', 'Password changed successfully.')); |
109
|
|
|
} else { |
110
|
|
|
Yii::$app->session->setFlash('error', Module::t('module', 'Error! Password changed not successfully.')); |
111
|
|
|
} |
112
|
|
|
return $this->redirect(['update', 'tab' => 'password']); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Deletes an existing User model. |
117
|
|
|
* This delete set status blocked, is successful, logout and the browser will be redirected to the 'home' page. |
118
|
|
|
* @return Response |
119
|
|
|
* @throws NotFoundHttpException |
120
|
|
|
*/ |
121
|
|
|
public function actionDelete() |
122
|
|
|
{ |
123
|
|
|
$model = $this->findModel(); |
124
|
|
|
$model->scenario = $model::SCENARIO_PROFILE_DELETE; |
125
|
|
|
$model->status = $model::STATUS_DELETED; |
126
|
|
|
if ($model->save()) |
127
|
|
|
Yii::$app->user->logout(); |
128
|
|
|
return $this->goHome(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Generate new auth key |
133
|
|
|
* @throws NotFoundHttpException |
134
|
|
|
*/ |
135
|
|
|
public function actionGenerateAuthKey() |
136
|
|
|
{ |
137
|
|
|
$model = $this->findModel(); |
138
|
|
|
$model->generateAuthKey(); |
139
|
|
|
$model->save(); |
140
|
|
|
$this->redirect(['index']); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Finds the User model based on its primary key value. |
145
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
146
|
|
|
* @return null|User the loaded model |
147
|
|
|
* @throws NotFoundHttpException if the model cannot be found |
148
|
|
|
*/ |
149
|
|
|
private function findModel() |
150
|
|
|
{ |
151
|
|
|
if (!Yii::$app->user->isGuest) { |
152
|
|
|
/** @var object $identity */ |
153
|
|
|
$identity = Yii::$app->user->identity; |
154
|
|
|
if (($model = User::findOne($identity->id)) !== null) { |
155
|
|
|
return $model; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
throw new NotFoundHttpException(Module::t('module', 'The requested page does not exist.')); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|