ProfileController.php
4.96 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
<?php
namespace backend\controllers;
use artbox\core\behaviors\LinkBehavior;
use artbox\core\models\ChangePassword;
use common\models\User;
use common\models\UserData;
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' ],
],
],
];
}
/**
* User profile page, that allows to change UserData and password.
*
* @param \artbox\core\models\ChangePassword|null $changePasswordModel
* @param \common\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;
$userData = new UserData();
if (empty( $changePasswordModel )) {
/**
* @var ChangePassword $changePasswordModel
*/
$changePasswordModel = \Yii::createObject(ChangePassword::className());
}
return $this->render(
'index',
[
'user' => $user,
'changePasswordModel' => $changePasswordModel,
'userData' => $userData
]
);
}
/**
* 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
*/
$data = new UserData();
$changePassword = \Yii::createObject(ChangePassword::className());
$successData = true;
if ($data->load(\Yii::$app->request->post()) && $data->validate()) {
$successData = $data->saveData();
}
$successPassword = true;
if ($changePassword->load(\Yii::$app->request->post()) && $changePassword->validatePassword()){
$successPassword = $changePassword->saveChanges();
}
if ($successPassword && $successData){
\Yii::$app->session->setFlash('success', \Yii::t('core', 'You data changed.'));
} else {
\Yii::$app->session->setFlash('error', \Yii::t('core', 'Change failed.'));
}
return $this->actionIndex($changePassword, $data);
}
}