1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\backend\controllers; |
4
|
|
|
|
5
|
|
|
use yii; |
6
|
|
|
use yii\web\Controller; |
7
|
|
|
use yii\filters\AccessControl; |
8
|
|
|
use zacksleo\yii2\backend\models\forms\LoginForm; |
9
|
|
|
use zacksleo\yii2\backend\models\forms\PasswordResetRequestForm; |
10
|
|
|
use zacksleo\yii2\backend\models\forms\ResetPasswordForm; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Default controller for the `backend` module |
14
|
|
|
*/ |
15
|
|
|
class SiteController extends Controller |
16
|
|
|
{ |
17
|
|
|
public $layout = '@vendor/zacksleo/yii2-backend/src/views/layouts/layout'; |
18
|
|
|
public $_viewPath = '@vendor/zacksleo/yii2-backend/src/views/site/'; |
19
|
|
|
|
20
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
'access' => [ |
24
|
|
|
'class' => AccessControl::className(), |
|
|
|
|
25
|
|
|
'only' => ['login', 'logout', 'index'], |
26
|
|
|
'rules' => [ |
27
|
|
|
[ |
28
|
|
|
'allow' => true, |
29
|
|
|
'actions' => ['login'], |
30
|
|
|
'roles' => ['?'], |
31
|
|
|
], |
32
|
|
|
[ |
33
|
|
|
'allow' => true, |
34
|
|
|
'actions' => ['index', 'logout'], |
35
|
|
|
'roles' => ['@'], |
36
|
|
|
] |
37
|
|
|
], |
38
|
|
|
] |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function actions() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'error' => [ |
49
|
|
|
'class' => 'yii\web\ErrorAction', |
50
|
|
|
'view' => '@vendor/zacksleo/yii2-backend/src/views/site/error' |
51
|
|
|
], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Renders the index view for the module |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function actionIndex() |
60
|
|
|
{ |
61
|
|
|
return $this->render($this->_viewPath . 'index'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function actionLogin() |
65
|
|
|
{ |
66
|
|
|
if (!\Yii::$app->user->isGuest) { |
67
|
|
|
return $this->goHome(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$model = new LoginForm(); |
71
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->login()) { |
72
|
|
|
return $this->goBack(); |
73
|
|
|
} else { |
74
|
|
|
$this->layout = 'page'; |
75
|
|
|
Yii::$app->params['bodyClass'] = 'login'; |
76
|
|
|
return $this->render($this->_viewPath . 'login', [ |
77
|
|
|
'model' => $model, |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* User logout |
84
|
|
|
*/ |
85
|
|
|
public function actionLogout() |
86
|
|
|
{ |
87
|
|
|
Yii::$app->user->logout(); |
88
|
|
|
|
89
|
|
|
return $this->goHome(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Requests password reset. |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function actionRequestPasswordReset() |
98
|
|
|
{ |
99
|
|
|
$model = new PasswordResetRequestForm(); |
100
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
101
|
|
|
if ($model->sendEmail()) { |
102
|
|
|
Yii::$app->session->setFlash('success', 'Check your email for further instructions.'); |
103
|
|
|
|
104
|
|
|
return $this->goHome(); |
105
|
|
|
} else { |
106
|
|
|
Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->render($this->_viewPath . 'requestPasswordReset', [ |
111
|
|
|
'model' => $model, |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Resets password. |
117
|
|
|
* |
118
|
|
|
* @param string $token |
119
|
|
|
* @return mixed |
120
|
|
|
* @throws BadRequestHttpException |
121
|
|
|
*/ |
122
|
|
|
public function actionResetPassword($token) |
123
|
|
|
{ |
124
|
|
|
try { |
125
|
|
|
$model = new ResetPasswordForm($token); |
126
|
|
|
} catch (InvalidParamException $e) { |
|
|
|
|
127
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) { |
131
|
|
|
Yii::$app->session->setFlash('success', 'New password saved.'); |
132
|
|
|
|
133
|
|
|
return $this->goHome(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $this->render($this->_viewPath . 'resetPassword', [ |
137
|
|
|
'model' => $model, |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function actionEditor() |
142
|
|
|
{ |
143
|
|
|
return $this->render($this->_viewPath . 'editor'); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.