Completed
Push — master ( e1c233...726cec )
by Igor
07:28
created

Module::getCurrentPermissionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace app\modules\admin;
4
5
use Yii;
6
use yii\helpers\Html;
7
8
class Module extends \yii\base\Module
9
{
10
    public $controllerNamespace = 'app\modules\admin\controllers';
11
    public $defaultRoute = 'index/index';
12
    public $layout = 'admin';
13
14
    public function init()
15
    {
16
        parent::init();
17
18
        Yii::$app->user->loginUrl = ['admin/index/login'];
19
        Yii::$app->timeZone = Yii::$app->params['mainTimeZone'];
20
21
        \Yii::$container->set('yii\widgets\LinkPager', [
22
            'maxButtonCount' => 5,
23
            'nextPageLabel'  => '&rarr;',
24
            'prevPageLabel'  => '&larr;',
25
            'firstPageLabel' => '&lArr;',
26
            'lastPageLabel'  => '&rArr;',
27
        ]);
28
    }
29
30
    public function beforeAction($action)
31
    {
32
        if (parent::beforeAction($action)) {
33
            $action->controller->cssBundle = 'admin.css';
34
            $action->controller->jsBundle = 'admin.js';
35
36
            return $this->checkAccess($action);
37
        } else {
38
            return false;
39
        }
40
    }
41
42
    public function checkAccess($action)
43
    {
44
        if ($action->controller->id == 'index') {
45
            return true;
46
        }
47
48
        if (!\Yii::$app->user->can('AdminModule') ||
49
            !\Yii::$app->user->can($this->getCurrentPermissionName($action))
50
        ) {
51
            Http::exception(403);
52
        }
53
54
        return true;
55
    }
56
57
    private function getCurrentPermissionName($action)
58
    {
59
        return 'ACTION_Admin' . ucfirst($action->controller->id);
60
    }
61
}
62