ProfileController.php
5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace artbox\core\controllers;
use artbox\core\behaviors\LinkBehavior;
use artbox\core\models\ChangePassword;
use artbox\core\models\User;
use artbox\core\models\UserData;
use yii\base\InvalidConfigException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
/**
* Class ProfileController
*
* @package artbox\core\controllers
*/
class ProfileController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => [
'login',
'error',
],
'allow' => true,
],
[
'allow' => true,
'roles' => [ '@' ],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [// 'change-password' => [ 'post' ],
],
],
];
}
/**
* @return bool|string
*/
public function getViewPath()
{
return \Yii::getAlias('@artbox/core/views/profile');
}
/**
* User profile page, that allows to change UserData and password.
*
* @param \artbox\core\models\ChangePassword|null $changePasswordModel
* @param \artbox\core\models\UserData|null $userData
*
* @return string
* @throws \yii\base\InvalidConfigException
*/
public function actionIndex(ChangePassword $changePasswordModel = null, UserData $userData = null)
{
/**
* @var User $user
*/
$user = \Yii::$app->user->identity;
/**
* @var LinkBehavior $userDataBehavior
*/
if (empty( $userData )) {
if (( $userDataBehavior = $user->getBehavior('userData') ) === null) {
throw new InvalidConfigException(
\Yii::t('core', '$app->user->identity class must have LinkBehavior to UserData model')
);
}
$userDataBehavior->ensureExistance();
$userData = $userDataBehavior->getLinkObject();
}
if (empty( $changePasswordModel )) {
/**
* @var ChangePassword $changePasswordModel
*/
$changePasswordModel = \Yii::createObject(ChangePassword::className());
}
return $this->render(
'index',
[
'user' => $user,
'userData' => $userData,
'changePasswordModel' => $changePasswordModel,
]
);
}
/**
* Change User password
*
* @return string|\yii\web\Response
*/
public function actionChangePassword()
{
if (empty( \Yii::$app->request->post() )) {
return $this->redirect([ 'index' ]);
}
/**
* @var ChangePassword $model
*/
$model = \Yii::createObject(ChangePassword::className());
$success = false;
if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
if ($model->validatePassword()) {
if (( $success = $model->saveChanges() ) === true) {
\Yii::$app->session->setFlash('success', \Yii::t('core', 'Password was successfully changed.'));
}
}
}
if (!$success) {
\Yii::$app->session->setFlash('error', \Yii::t('core', 'Password change failed.'));
}
$model->oldPassword = $model->newPassword = $model->newPasswordRepeat = null;
return $this->actionIndex($model);
}
/**
* Update UserData
*
* @return string|\yii\web\Response
* @throws \yii\base\InvalidConfigException
*/
public function actionUpdate()
{
if (empty( \Yii::$app->request->post() )) {
return $this->redirect([ 'index' ]);
}
/**
* @var User $user
*/
$user = \Yii::$app->user->identity;
/**
* @var LinkBehavior $userDataBehavior
*/
if (( $userDataBehavior = $user->getBehavior('userData') ) === null) {
throw new InvalidConfigException(
\Yii::t('core', '$app->user->identity class must have LinkBehavior to UserData model')
);
}
$userDataBehavior->ensureExistance();
$model = $userDataBehavior->getLinkObject();
$success = false;
if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
if (( $success = $model->save() ) === true) {
\Yii::$app->session->setFlash(
'success',
\Yii::t('core', 'Personal information was successfully changed.')
);
}
}
if (!$success) {
\Yii::$app->session->setFlash('error', \Yii::t('core', 'Personal information change failed.'));
}
return $this->actionIndex(null, $model);
}
}