1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\web\Response; |
7
|
|
|
use yii\filters\AccessControl; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
use app\models\{LoginForm, RegForm}; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class SiteController |
13
|
|
|
* |
14
|
|
|
* @package app\controllers |
15
|
|
|
*/ |
16
|
|
|
class SiteController extends BaseController |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritdoc |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function behaviors() |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
return ArrayHelper::merge(parent::behaviors(), [ |
24
|
|
|
'access' => [ |
25
|
|
|
'class' => AccessControl::class, |
26
|
|
|
'rules' => [ |
27
|
|
|
[ |
28
|
|
|
'allow' => true, |
29
|
|
|
'actions' => ['reg', 'login', 'captcha'], |
30
|
|
|
'roles' => ['?'], |
31
|
|
|
], |
32
|
|
|
[ |
33
|
|
|
'allow' => true, |
34
|
|
|
'actions' => ['logout'], |
35
|
|
|
'roles' => ['@'], |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
], |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function actions() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
return ArrayHelper::merge(parent::actions(), [ |
48
|
|
|
'captcha' => [ |
49
|
|
|
'class' => 'yii\captcha\CaptchaAction', |
50
|
|
|
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, |
51
|
|
|
], |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Login action. |
57
|
|
|
* |
58
|
|
|
* @return Response|string |
59
|
|
|
*/ |
60
|
|
|
public function actionLogin() |
61
|
|
|
{ |
62
|
|
|
if (!Yii::$app->user->isGuest) { |
63
|
|
|
return $this->goHome(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$model = new LoginForm(); |
67
|
|
|
|
68
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->login()) { |
69
|
|
|
return $this->goBack(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->render('login', [ |
73
|
|
|
'model' => $model, |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Register action. |
79
|
|
|
* |
80
|
|
|
* @return Response|string |
81
|
|
|
*/ |
82
|
|
|
public function actionReg() |
83
|
|
|
{ |
84
|
|
|
if (!Yii::$app->user->isGuest) { |
85
|
|
|
return $this->goHome(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/* @var $settingModel \app\models\Setting */ |
89
|
|
|
$settingModel = Yii::$app->get('settings') |
90
|
|
|
->setModel() |
91
|
|
|
->getSettings(); |
92
|
|
|
|
93
|
|
|
if ($settingModel->regBlock == 1) { |
94
|
|
|
return $this->render('regBlock'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$model = new RegForm(); |
98
|
|
|
|
99
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->reg()) { |
100
|
|
|
|
101
|
|
|
if (Yii::$app->getUser()->login($model->getUser())) { |
|
|
|
|
102
|
|
|
return $this->goHome(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->render('reg', [ |
107
|
|
|
'model' => $model, |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Logout action. |
113
|
|
|
* |
114
|
|
|
* @return Response |
115
|
|
|
*/ |
116
|
|
|
public function actionLogout() |
117
|
|
|
{ |
118
|
|
|
Yii::$app->user->logout(); |
119
|
|
|
|
120
|
|
|
return $this->goHome(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.