PasswordController.php
5 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace backend\modules\user\controllers;
use Yii;
use yii\base\InvalidParamException;
use yii\filters\AccessControl;
use yii\web\{
BadRequestHttpException, NotFoundHttpException
};
//
use thread\app\base\controllers\BackendController;
use thread\modules\user\models\form\{
ChangePassword, PasswordResetRequestForm, ResetPasswordForm
};
//
use backend\modules\user\models\{
User
};
/**
* Class PasswordController
*
* @package admin\modules\user\controllers
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c), Thread
*/
class PasswordController extends BackendController
{
public $name = 'password';
public $title = "User Password";
public $label = "User Password";
protected $model = User::class;
public $actionListLinkStatus = ['/user/user/list'];
/**
* @return array
*/
public function behaviors()
{
return [
'AccessControl' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'actions' => ['request-reset', 'reset'],
'roles' => ['?'],
],
[
'allow' => true,
'actions' => ['validation', 'change'],
'roles' => ['admin']
],
[
'allow' => false,
],
],
],
];
}
/**
* @param \yii\base\Action $action
* @return bool
*/
public function beforeAction($action)
{
return true;
}
/**
*
* @return array
*/
public function actions()
{
return [];
}
/**
* @param $id
* @return string
* @throws NotFoundHttpException
* @throws \Exception
*/
public function actionChange($id)
{
$user = User::findIdentity($id);
if ($user === null) {
throw new NotFoundHttpException;
}
$this->label = Yii::t('user', 'Password change') . ' : ' . $user['username'];
$model = new ChangePassword();
$model->setScenario('adminPasswordChange');
$model['username'] = $user['username'];
$model['email'] = $user['email'];
if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) {
if ($user !== null) {
$user->setScenario('passwordChange');
$user->setPassword($model['password']);
$transaction = $user::getDb()->beginTransaction();
try {
$save = $user->save();
if ($save) {
$transaction->commit();
if (Yii::$app->getRequest()->post('save_and_exit')) {
return $this->redirect($this->actionListLinkStatus);
} else {
$model->addFlash(Yii::t('user', 'Password changed'));
}
} else {
$transaction->rollBack();
};
} catch (\Exception $e) {
$transaction->rollBack();
throw new \Exception($e);
}
}
}
return $this->render('change', [
'model' => $model,
'user' => $user
]);
}
/**
* Requests password reset.
*
* @return mixed
*/
public function actionRequestReset()
{
$model = new PasswordResetRequestForm();
$model->setScenario('remind');
if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) {
if ($model->sendEmail()) {
Yii::$app->session->setFlash('success', Yii::t('user', 'Check your email for further instructions.'));
return $this->goHome();
} else {
Yii::$app->session->setFlash('error', Yii::t('user', 'Sorry, we are unable to reset password for email provided.'));
}
}
$this->layout = '@app/layouts/nologin';
return $this->render('requestResetToken', [
'model' => $model,
]);
}
/**
* Resets password.
*
* @param string $token
* @return mixed
* @throws BadRequestHttpException
*/
public function actionReset($token)
{
try {
$model = new ResetPasswordForm($token);
} catch (InvalidParamException $e) {
throw new BadRequestHttpException($e->getMessage());
}
$model->setScenario('setPassword');
if ($model->load(Yii::$app->getRequest()->post()) && $model->validate() && $model->setPassword()) {
Yii::$app->session->setFlash('success', Yii::t('user', 'New password was saved.'));
return $this->goHome();
}
$this->layout = '@app/layouts/nologin';
return $this->render('reset', [
'model' => $model,
]);
}
}