Passed
Pull Request — master (#400)
by
unknown
02:08
created

Assignment::revoke()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 10
nop 1
1
<?php
2
3
namespace mdm\admin\models;
4
5
use mdm\admin\components\Configs;
6
use mdm\admin\components\Helper;
7
use Yii;
8
9
/**
10
 * Description of Assignment
11
 *
12
 * @author Misbahul D Munir <[email protected]>
13
 * @since 2.5
14
 */
15
class Assignment extends \mdm\admin\BaseObject
16
{
17
    /**
18
     * @var integer User id
19
     */
20
    public $id;
21
    /**
22
     * @var \yii\web\IdentityInterface User
23
     */
24
    public $user;
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function __construct($id, $user = null, $config = array())
30
    {
31
        $this->id = $id;
32
        $this->user = $user;
33
        parent::__construct($config);
34
    }
35
36
    /**
37
     * Grands a roles from a user.
38
     * @param array $items
39
     * @return integer number of successful grand
40
     */
41 View Code Duplication
    public function assign($items)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $manager = Configs::authManager();
44
        $success = 0;
45
46
        $current_user_id = Yii::$app->getUser()->getId();
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...
47
48
        foreach ($items as $name) {
49
            try {
50
                $verify_result = $manager->checkAccess($current_user_id, $name);
51
52
                Yii::debug("verify role|permission: $name, result: "
0 ignored issues
show
Bug introduced by
The method debug() does not seem to exist on object<Yii>.

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.

Loading history...
53
                    . ($verify_result ? "Y" : "N"));
54
55
                if ($verify_result) {
56
                    $item = $manager->getRole($name);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $item is correct as $manager->getRole($name) (which targets yii\rbac\ManagerInterface::getRole()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
57
                    $item = $item ?: $manager->getPermission($name);
58
                    $manager->assign($item, $this->id);
0 ignored issues
show
Documentation introduced by
$item is of type null|object<yii\rbac\Permission>, but the function expects a object<yii\rbac\Role>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
                    $success++;
60
                }
61
            } catch (\Exception $exc) {
62
                Yii::error($exc->getMessage(), __METHOD__);
63
            }
64
        }
65
        Helper::invalidate();
66
        return $success;
67
    }
68
69
    /**
70
     * Revokes a roles from a user.
71
     * @param array $items
72
     * @return integer number of successful revoke
73
     */
74 View Code Duplication
    public function revoke($items)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $current_user_id = Yii::$app->getUser()->getId();
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...
77
        $manager = Configs::authManager();
78
        $success = 0;
79
        foreach ($items as $name) {
80
            try {
81
                $verify_result = $manager->checkAccess($current_user_id, $name);
82
83
                Yii::debug("verify role|permission: $name, result: "
0 ignored issues
show
Bug introduced by
The method debug() does not seem to exist on object<Yii>.

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.

Loading history...
84
                    . ($verify_result ? "Y" : "N"));
85
86
                if ($verify_result) {
87
                    $item = $manager->getRole($name);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $item is correct as $manager->getRole($name) (which targets yii\rbac\ManagerInterface::getRole()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
                    $item = $item ?: $manager->getPermission($name);
89
                    $manager->revoke($item, $this->id);
0 ignored issues
show
Documentation introduced by
$item is of type null|object<yii\rbac\Permission>, but the function expects a object<yii\rbac\Role>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
                    $success++;
91
                }
92
            } catch (\Exception $exc) {
93
                Yii::error($exc->getMessage(), __METHOD__);
94
            }
95
        }
96
        Helper::invalidate();
97
        return $success;
98
    }
99
100
    /**
101
     * Get all available and assigned roles/permission
102
     * @return array
103
     */
104
    public function getItems()
105
    {
106
        $current_user_id = Yii::$app->getUser()->getId();
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...
107
        $manager = Configs::authManager();
108
        $available = [];
109
110
        $roles = $manager->getRolesByUser($current_user_id);
111
112
        foreach ($roles as $role) {
113
            $name = $role->name;
114
            $available[$name][0] = 'role';
115
            $available[$name][1] = $role->description;
116
117
            $child_roles = $manager->getChildRoles($name);
0 ignored issues
show
Bug introduced by
The method getChildRoles() does not seem to exist on object<yii\rbac\ManagerInterface>.

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.

Loading history...
118
            foreach ($child_roles as $childRole)
119
            {
120
                $name = $childRole->name;
121
                $available[$name][0] = 'role';
122
                $available[$name][1] = $childRole->description;
123
            }
124
        }
125
126
127
        $permissions = $manager->getPermissionsByUser($current_user_id);
128
129
        foreach ($permissions as $permission) {
130
            $name = $permission->name;
131
            if ($name[0] != '/') {
132
                $available[$name][0] = 'permission';
133
                $available[$name][1] = $permission->description;
134
            }
135
        }
136
137
        $assigned = [];
138
        foreach ($manager->getAssignments($this->id) as $item) {
139
            if(isset($available[$item->roleName])) {
140
                $assigned[$item->roleName] = $available[$item->roleName];
141
                unset($available[$item->roleName]);
142
            }
143
        }
144
145
        ksort($available);
146
        ksort($assigned);
147
        return [
148
            'available' => $available,
149
            'assigned'  => $assigned,
150
        ];
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156
    public function __get($name)
157
    {
158
        if ($this->user) {
159
            return $this->user->$name;
160
        }
161
    }
162
}
163