Completed
Push — master ( c9a211...be2c58 )
by Misbahul D
03:20
created

Module.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace mdm\admin;
4
5
use Yii;
6
use yii\helpers\Inflector;
7
use yii\helpers\ArrayHelper;
8
9
/**
10
 * GUI manager for RBAC.
11
 * 
12
 * Use [[\yii\base\Module::$controllerMap]] to change property of controller. 
13
 * To change listed menu, use property [[$menus]].
14
 * 
15
 * ```
16
 * 'layout' => 'left-menu', // default to null mean use application layout.
17
 * 'controllerMap' => [
18
 *     'assignment' => [
19
 *         'class' => 'mdm\admin\controllers\AssignmentController',
20
 *         'userClassName' => 'app\models\User',
21
 *         'idField' => 'id'
22
 *     ]
23
 * ],
24
 * 'menus' => [
25
 *     'assignment' => [
26
 *         'label' => 'Grand Access' // change label
27
 *     ],
28
 *     'route' => null, // disable menu
29
 * ],
30
 * ```
31
 * 
32
 * @property string $mainLayout Main layout using for module. Default to layout of parent module.
33
 * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
34
 * @property array $menus List avalible menu of module.
35
 * It generated by module items .
36
 * 
37
 * @author Misbahul D Munir <[email protected]>
38
 * @since 1.0
39
 */
40
class Module extends \yii\base\Module
41
{
42
    /**
43
     * @var array Nav bar items.
44
     */
45
    public $navbar;
46
    /**
47
     * @var string Main layout using for module. Default to layout of parent module.
48
     * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
49
     */
50
    public $mainLayout = '@mdm/admin/views/layouts/main.php';
51
    /**
52
     * @var array 
53
     * @see [[menus]]
54
     */
55
    private $_menus = [];
56
    /**
57
     * @var array 
58
     * @see [[menus]]
59
     */
60
    private $_coreItems = [
61
        'user' => 'Users',
62
        'assignment' => 'Assignments',
63
        'role' => 'Roles',
64
        'permission' => 'Permissions',
65
        'route' => 'Routes',
66
        'rule' => 'Rules',
67
        'menu' => 'Menus',
68
    ];
69
    /**
70
     * @var array 
71
     * @see [[items]]
72
     */
73
    private $_normalizeMenus;
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function init()
79
    {
80
        parent::init();
81
        if (!isset(Yii::$app->i18n->translations['rbac-admin'])) {
82
            Yii::$app->i18n->translations['rbac-admin'] = [
83
                'class' => 'yii\i18n\PhpMessageSource',
84
                'sourceLanguage' => 'en',
85
                'basePath' => '@mdm/admin/messages'
86
            ];
87
        }
88
        $userClass = ArrayHelper::getValue(Yii::$app->components, 'user.identityClass');
89
        if ($this->defaultRoute == 'default' && $userClass && is_subclass_of($userClass, 'yii\db\BaseActiveRecord')) {
90
            $this->defaultRoute = 'assignment';
91
        }
92
        //user did not define the Navbar?
93
        if ($this->navbar === null && Yii::$app instanceof \yii\web\Application) {
94
            $this->navbar = [
95
                ['label' => Yii::t('rbac-admin', 'Help'), 'url' => ['default/index']],
96
                ['label' => Yii::t('rbac-admin', 'Application'), 'url' => Yii::$app->homeUrl]
97
            ];
98
        }
99
        if (class_exists('yii\jui\JuiAsset')) {
100
            Yii::$container->set('mdm\admin\AutocompleteAsset', 'yii\jui\JuiAsset');
101
        }
102
    }
103
104
    /**
105
     * Get avalible menu.
106
     * @return array
107
     */
108
    public function getMenus()
109
    {
110
        if ($this->_normalizeMenus === null) {
111
            $mid = '/' . $this->getUniqueId() . '/';
112
            // resolve core menus
113
            $this->_normalizeMenus = [];
114
115
            $config = components\Configs::instance();
116
            $conditions = [
117
                'user' => $config->db && $config->db->schema->getTableSchema($config->userTable),
118
                'assignment' => ($userClass = Yii::$app->getUser()->identityClass) && is_subclass_of($userClass, 'yii\db\BaseActiveRecord'),
0 ignored issues
show
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
119
                'menu' => $config->db && $config->db->schema->getTableSchema($config->menuTable),
120
            ];
121
            foreach ($this->_coreItems as $id => $lable) {
122
                if (!isset($conditions[$id]) || $conditions[$id]) {
123
                    $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]];
124
                }
125
            }
126
            foreach (array_keys($this->controllerMap) as $id) {
127
                $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', Inflector::humanize($id)), 'url' => [$mid . $id]];
128
            }
129
130
            // user configure menus
131
            foreach ($this->_menus as $id => $value) {
132
                if (empty($value)) {
133
                    unset($this->_normalizeMenus[$id]);
134
                    continue;
135
                }
136
                if (is_string($value)) {
137
                    $value = ['label' => $value];
138
                }
139
                $this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value)
140
                        : $value;
141
                if (!isset($this->_normalizeMenus[$id]['url'])) {
142
                    $this->_normalizeMenus[$id]['url'] = [$mid . $id];
143
                }
144
            }
145
        }
146
        return $this->_normalizeMenus;
147
    }
148
149
    /**
150
     * Set or add avalible menu.
151
     * @param array $menus
152
     */
153
    public function setMenus($menus)
154
    {
155
        $this->_menus = array_merge($this->_menus, $menus);
156
        $this->_normalizeMenus = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $_normalizeMenus.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
157
    }
158
159
    /**
160
     * @inheritdoc
161
     */
162
    public function beforeAction($action)
163
    {
164
        if (parent::beforeAction($action)) {
165
            /* @var $action \yii\base\Action */
166
            $view = $action->controller->getView();
167
168
            $view->params['breadcrumbs'][] = [
169
                'label' => Yii::t('rbac-admin', 'Admin'),
170
                'url' => ['/' . $this->uniqueId]
171
            ];
172
            return true;
173
        }
174
        return false;
175
    }
176
}
177