1
|
|
|
<?php |
2
|
|
|
namespace backend\controllers; |
3
|
|
|
|
4
|
|
|
use Yii; |
5
|
|
|
use yii\filters\VerbFilter; |
6
|
|
|
use common\models\LoginForm; |
7
|
|
|
use backend\components\Controller; |
8
|
|
|
use backend\models\Log; |
9
|
|
|
use backend\models\User; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Site controller |
13
|
|
|
*/ |
14
|
|
|
class SiteController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @inheritdoc |
18
|
|
|
*/ |
19
|
|
|
public function behaviors() |
20
|
|
|
{ |
21
|
|
|
$behaviors = parent::behaviors(); |
22
|
|
|
$behaviors['access']['rules'] = [ |
23
|
|
|
[ |
24
|
|
|
'actions' => ['login', 'error'], |
25
|
|
|
'allow' => true, |
26
|
|
|
], |
27
|
|
|
[ |
28
|
|
|
'actions' => ['logout', 'index'], |
29
|
|
|
'allow' => true, |
30
|
|
|
'roles' => ['@'], |
31
|
|
|
], |
32
|
|
|
]; |
33
|
|
|
$behaviors['verbs'] = [ |
34
|
|
|
'class' => VerbFilter::class, |
35
|
|
|
'actions' => [ |
36
|
|
|
'logout' => ['post'], |
37
|
|
|
], |
38
|
|
|
]; |
39
|
|
|
return $behaviors; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function actions() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'error' => [ |
49
|
|
|
'class' => 'yii\web\ErrorAction', |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Displays homepage. |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function actionIndex() |
59
|
|
|
{ |
60
|
|
|
return $this->render('index'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Login action. |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function actionLogin() |
68
|
|
|
{ |
69
|
|
|
if (!Yii::$app->user->isGuest) { |
70
|
|
|
return $this->goHome(); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$model = new LoginForm(); |
74
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->login()) { |
75
|
|
|
$this->logUserAction(Log::ACTION_LOGIN); |
76
|
|
|
return $this->goBack(); |
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
return $this->render('login', [ |
79
|
|
|
'model' => $model, |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Logout action. |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function actionLogout() |
89
|
|
|
{ |
90
|
|
|
$this->logUserAction(Log::ACTION_LOGOUT); |
91
|
|
|
Yii::$app->user->logout(); |
|
|
|
|
92
|
|
|
return $this->goHome(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $action |
97
|
|
|
* @param bool $saveOldData |
98
|
|
|
* @param bool $saveNewData |
99
|
|
|
*/ |
100
|
|
|
protected function logUserAction($action, $saveOldData = false, $saveNewData = false) |
101
|
|
|
{ |
102
|
|
|
$user = new User(Yii::$app->user->identity); |
|
|
|
|
103
|
|
|
Log::find() |
104
|
|
|
->createLogEntity($user, $action, $saveOldData, $saveNewData) |
105
|
|
|
->save(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|