Completed
Push — master ( 2e8683...7e20fc )
by Igor
07:20
created

IndexController::behaviors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 37
ccs 2
cts 2
cp 1
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
crap 1
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\filters\AccessControl;
7
use yii\filters\VerbFilter;
8
use yii\web\ForbiddenHttpException;
9
use app\modules\admin\models\forms\LoginForm;
10
11
class IndexController extends \yii\web\Controller
12
{
13 14
    public function behaviors()
14
    {
15
        return [
16 14
            'access' => [
17
                'class' => AccessControl::class,
18
                'rules' => [
19
                    [
20
                        'allow'   => true,
21
                        'actions' => ['index'],
22
                        'roles'   => ['@', 'AdminModule'],
23
                    ],
24
                    [
25
                        'allow'   => true,
26
                        'actions' => ['login'],
27
                        'roles'   => ['?'],
28
                    ],
29
                    [
30
                        'allow'   => true,
31
                        'actions' => ['logout'],
32
                        'roles'   => ['@'],
33
                    ],
34
                    [
35
                        'allow'   => true,
36
                        'actions' => ['error'],
37
                        'roles'   => ['*'],
38
                    ],
39
                    // everything else is denied
40
                ],
41
            ],
42
            'verbs' => [
43
                'class' => VerbFilter::class,
44
                'actions' => [
45
                    'logout' => ['post'],
46
                ],
47
            ],
48
        ];
49
    }
50
51
    public function actionIndex()
52
    {
53
        if (!Yii::$app->user->can('AdminModule')) {
54
            throw new ForbiddenHttpException(Yii::t('app', 'Access Denied'));
55
        } // @codeCoverageIgnore
56
57
        return $this->render('index');
58
    }
59
60 14
    public function actionLogin()
61
    {
62 14
        $model = new LoginForm();
63
64 14
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
65 2
            return $this->goBack();
66
        }
67 14
        return $this->render('login', [
68 14
            'model' => $model,
69
        ]);
70
    }
71
72
    public function actionLogout()
73
    {
74
        Yii::$app->user->logout();
75
        return $this->goHome();
76
    }
77
}
78