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(); |
||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||||
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(); |
||||||
0 ignored issues
–
show
|
|||||||
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(); |
||||||
0 ignored issues
–
show
The method
logout() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
92 | return $this->goHome(); |
||||||
0 ignored issues
–
show
|
|||||||
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); |
||||||
0 ignored issues
–
show
It seems like
Yii::app->user->identity can also be of type yii\web\IdentityInterface ; however, parameter $config of backend\models\User::__construct() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
103 | Log::find() |
||||||
104 | ->createLogEntity($user, $action, $saveOldData, $saveNewData) |
||||||
105 | ->save(); |
||||||
106 | } |
||||||
107 | } |
||||||
108 |