AuthHelper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 19.23%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 91
ccs 5
cts 26
cp 0.1923
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasRole() 0 10 2
A isAdmin() 0 10 4
A getPermission() 0 4 1
A getRole() 0 4 1
A remove() 0 4 1
A getUnassignedItems() 0 14 4
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Helper;
13
14
use Da\User\Model\AbstractAuthItem;
15
use Da\User\Module;
16
use Da\User\Traits\AuthManagerAwareTrait;
17
use Yii;
18
use yii\helpers\ArrayHelper;
19
use yii\rbac\Permission;
20
use yii\rbac\Role;
21
use yii\rbac\Rule;
22
23
class AuthHelper
24
{
25
    use AuthManagerAwareTrait;
26
27
    /**
28
     * Checks whether a user has certain role.
29
     *
30
     * @param $userId
31
     * @param $role
32
     *
33
     * @return bool
34
     */
35
    public function hasRole($userId, $role)
36
    {
37
        if ($this->getAuthManager()) {
38
            $roles = array_keys($this->getAuthManager()->getRolesByUser($userId));
39
40
            return in_array($role, $roles, true);
41
        }
42
43
        return false;
44
    }
45
46
    /**
47
     * @param $username
48
     *
49
     * @return bool
50
     */
51 2
    public function isAdmin($username)
52
    {
53
        /** @var Module $module */
54 2
        $module = Yii::$app->getModule('user');
55 2
        $hasAdministratorPermissionName = $this->getAuthManager() && $module->administratorPermissionName
56
            ? Yii::$app->getUser()->can($module->administratorPermissionName)
0 ignored issues
show
Bug introduced by
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...
57 2
            : false;
58
59 2
        return $hasAdministratorPermissionName || in_array($username, $module->administrators, false);
60
    }
61
62
    /**
63
     * @param $name
64
     *
65
     * @return null|\yii\rbac\Item|Permission
66
     */
67
    public function getPermission($name)
68
    {
69
        return $this->getAuthManager()->getPermission($name);
70
    }
71
72
    /**
73
     * @param $name
74
     *
75
     * @return null|\yii\rbac\Item|Role
76
     */
77
    public function getRole($name)
78
    {
79
        return $this->getAuthManager()->getRole($name);
80
    }
81
82
    /**
83
     * Removes a role, permission or rule from the RBAC system.
84
     *
85
     * @param Role|Permission|Rule $object
86
     *
87
     * @return bool whether the role, permission or rule is successfully removed
88
     */
89
    public function remove($object)
90
    {
91
        return $this->getAuthManager()->remove($object);
92
    }
93
94
    /**
95
     * @param AbstractAuthItem $model
96
     *
97
     * @return array
98
     */
99
    public function getUnassignedItems(AbstractAuthItem $model)
100
    {
101
        $excludeItems = $model->item !== null ? [$model->item->name] : [];
102
        $type = $model->getType() === Permission::TYPE_PERMISSION ? Permission::TYPE_PERMISSION : null;
103
        $items = $this->getAuthManager()->getItems($type, $excludeItems);
104
105
        return ArrayHelper::map(
106
            $items,
107
            'name',
108
            function ($item) {
109
                return empty($item->description) ? $item->name : "{$item->name} ({$item->description})";
110
            }
111
        );
112
    }
113
}
114