Completed
Push — master ( 0ca8c7...32ef7a )
by Andrii
04:22
created

AuthManager::saveAssignments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * RBAC implementation for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-rbac
7
 * @package   hipanel-rbac
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\rbac;
13
14
use yii\base\InvalidParamException;
15
use yii\rbac\Item;
16
17
/**
18
 * HiPanel AuthManager.
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class AuthManager extends \yii\rbac\PhpManager
23
{
24
    public $itemFile       = '@hipanel/rbac/files/items.php';
25
    public $ruleFile       = '@hipanel/rbac/files/rules.php';
26
    public $assignmentFile = '@hipanel/rbac/files/assignments.php';
27
28
    /**
29
     * Set permission.
30
     * @param string $name
31
     * @param string $description
32
     * @return Item
33
     */
34 3
    public function setPermission($name, $description = null)
35
    {
36 3
        return $this->setItem('permission', $name, $description);
37
    }
38
39
    /**
40
     * Set role.
41
     * @param string $name
42
     * @param string $description
43
     * @return Item
44
     */
45 3
    public function setRole($name, $description = null)
46
    {
47 3
        return $this->setItem('role', $name, $description);
48
    }
49
50
    /**
51
     * Set item by type and name.
52
     * Created if not exists else updates.
53
     * @param string $type
54
     * @param string $name
55
     * @param string $description
56
     * @return Item
57
     */
58 3
    public function setItem($type, $name, $description = null)
59
    {
60 3
        $item = $this->getItem($name) ?: $this->createItem($type, $name);
61 3
        if ($description) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $description of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
62
            $item->description = $description;
63
        }
64 3
        $this->add($item);
0 ignored issues
show
Documentation introduced by
$item is of type object<yii\rbac\Item>, but the function expects a object<yii\rbac\Role>|ob...>|object<yii\rbac\Rule>.

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...
65
66 3
        return $item;
67
    }
68
69
    /**
70
     * Create item by type and name.
71
     * @param string $type
72
     * @param string $name
73
     * @throws InvalidParamException
74
     * @return Item
75
     */
76 3
    public function createItem($type, $name)
77
    {
78 3
        if ('role' === $type) {
79 3
            return $this->createRole($name);
80 3
        } elseif ('permission' === $type) {
81 3
            return $this->createPermission($name);
82
        } else {
83
            throw new InvalidParamException('Creating unsupported item type: ' . $type);
84
        }
85
    }
86
87
    /**
88
     * Set child.
89
     * @param string|Item $parent
90
     * @param string|Item $child
91
     * @return bool
92
     */
93 3
    public function setChild($parent, $child)
94
    {
95 3 View Code Duplication
        if (is_string($parent)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96 3
            $name   = $parent;
97 3
            $parent = $this->getItem($parent);
98 3
            if (is_null($parent)) {
99
                throw new InvalidParamException("Unknown parent:$name at setChild");
100
            }
101 3
        }
102 3 View Code Duplication
        if (is_string($child)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
103 3
            $name  = $child;
104 3
            $child = $this->getItem($child);
105 3
            if (is_null($child)) {
106
                throw new InvalidParamException("Unknown child:$name at setChild");
107
            }
108 3
        }
109 3
        if (isset($this->children[$parent->name][$child->name])) {
110
            return false;
111
        }
112
113 3
        return $this->addChild($parent, $child);
114
    }
115
116 6
    public function setAssignment($role, $userId)
117
    {
118 6 View Code Duplication
        if (is_string($role)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
119 6
            $name = $role;
120 6
            $role = $this->getRole($role);
121 6
            if (is_null($role)) {
122
                throw new InvalidParamException("Unknown role:$name at setAssignment");
123
            }
124 6
        }
125 6
        if (isset($this->assignments[$userId][$role->name])) {
126
            return false;
127
        }
128
129 6
        return $this->assign($role, $userId);
130
    }
131
132 6
    public function getRoles()
133
    {
134 6
        return $this->getItems(Item::TYPE_ROLE);
135
    }
136
137
    public function getPermissions()
138
    {
139
        return $this->getItems(Item::TYPE_PERMISSION);
140
    }
141
142
    public function getAllAssignments()
143
    {
144
        return $this->assignments;
145
    }
146
147
    /**
148
     * We don't keep all the assignments, only basic.
149
     * @see forceSaveAssignments
150
     */
151 6
    protected function saveAssignments()
152
    {
153 6
    }
154
155
    /**
156
     * Create only basic assignments before saving.
157
     */
158 3
    public function saveBasicAssignments()
159
    {
160 3
        parent::saveAssignments();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (saveAssignments() instead of saveBasicAssignments()). Are you sure this is correct? If so, you might want to change this to $this->saveAssignments().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
161 3
    }
162
}
163