1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\backend\tests\controllers; |
4
|
|
|
|
5
|
|
|
use yii; |
6
|
|
|
use yii\base\InvalidParamException; |
7
|
|
|
use yii\web\BadRequestHttpException; |
8
|
|
|
use yii\web\Controller; |
9
|
|
|
use yii\filters\AccessControl; |
10
|
|
|
use zacksleo\yii2\backend\models\forms\LoginForm; |
11
|
|
|
use zacksleo\yii2\backend\models\forms\PasswordResetRequestForm; |
12
|
|
|
use zacksleo\yii2\backend\models\forms\ResetPasswordForm; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default controller for the `backend` module |
16
|
|
|
*/ |
17
|
|
|
class SiteController extends Controller |
18
|
|
|
{ |
19
|
|
|
public $layout = '@vendor/zacksleo/yii2-backend/src/views/layouts/layout'; |
20
|
|
|
public $_viewPath = '@vendor/zacksleo/yii2-backend/src/views/site/'; |
21
|
|
|
|
22
|
4 |
View Code Duplication |
public function behaviors() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
4 |
|
'access' => [ |
26
|
4 |
|
'class' => AccessControl::className(), |
27
|
|
|
'only' => ['login', 'logout', 'index'], |
28
|
|
|
'rules' => [ |
29
|
|
|
[ |
30
|
|
|
'allow' => true, |
31
|
|
|
'actions' => ['login'], |
32
|
|
|
'roles' => ['?'], |
33
|
|
|
], |
34
|
|
|
[ |
35
|
|
|
'allow' => true, |
36
|
|
|
'actions' => ['index', 'logout'], |
37
|
|
|
'roles' => ['@'], |
38
|
|
|
] |
39
|
|
|
], |
40
|
|
|
] |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
4 |
|
public function actions() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
4 |
|
'error' => [ |
51
|
|
|
'class' => 'yii\web\ErrorAction', |
52
|
|
|
'view' => '@vendor/zacksleo/yii2-backend/src/views/site/error' |
53
|
|
|
], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
public function actionLogin() |
58
|
|
|
{ |
59
|
2 |
|
$model = new LoginForm(); |
60
|
2 |
|
return $model->load(Yii::$app->request->post()) && $model->login(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* User logout |
65
|
|
|
*/ |
66
|
|
|
public function actionLogout() |
67
|
|
|
{ |
68
|
|
|
return Yii::$app->user->logout(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Requests password reset. |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
1 |
|
public function actionRequestpasswordreset() |
77
|
|
|
{ |
78
|
1 |
|
$model = new PasswordResetRequestForm(); |
79
|
1 |
|
return $model->load(Yii::$app->request->bodyParams) && $model->validate() && $model->sendEmail(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Resets password. |
84
|
|
|
* |
85
|
|
|
* @param string $token |
86
|
|
|
* @return mixed |
87
|
|
|
* @throws BadRequestHttpException |
88
|
|
|
*/ |
89
|
1 |
|
public function actionResetpassword($token) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
1 |
|
$model = new ResetPasswordForm($token); |
93
|
|
|
} catch (InvalidParamException $e) { |
94
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
95
|
|
|
} |
96
|
1 |
|
$model->load(Yii::$app->request->bodyParams); |
97
|
1 |
|
return $model->load(Yii::$app->request->bodyParams) && $model->validate() && $model->resetPassword(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.