Completed
Push — 2.1 ( 7c8525...0afc41 )
by Alexander
21:05 queued 16:02
created

BaseManager::getRole()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 3
eloc 3
nc 4
nop 1
crap 3
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\rbac;
9
10
use yii\base\Component;
11
use yii\base\InvalidConfigException;
12
use yii\base\InvalidArgumentException;
13
14
/**
15
 * BaseManager is a base class implementing [[ManagerInterface]] for RBAC management.
16
 *
17
 * For more details and usage information on DbManager, see the [guide article on security authorization](guide:security-authorization).
18
 *
19
 * @property Role[] $defaultRoleInstances Default roles. The array is indexed by the role names. This property
20
 * is read-only.
21
 *
22
 * @author Qiang Xue <[email protected]>
23
 * @since 2.0
24
 */
25
abstract class BaseManager extends Component implements ManagerInterface
26
{
27
    /**
28
     * @var array a list of role names that are assigned to every user automatically without calling [[assign()]].
29
     */
30
    public $defaultRoles = [];
31
32
33
    /**
34
     * Returns the named auth item.
35
     * @param string $name the auth item name.
36
     * @return Item the auth item corresponding to the specified name. Null is returned if no such item.
37
     */
38
    abstract protected function getItem($name);
39
40
    /**
41
     * Returns the items of the specified type.
42
     * @param int $type the auth item type (either [[Item::TYPE_ROLE]] or [[Item::TYPE_PERMISSION]]
43
     * @return Item[] the auth items of the specified type.
44
     */
45
    abstract protected function getItems($type);
46
47
    /**
48
     * Adds an auth item to the RBAC system.
49
     * @param Item $item the item to add
50
     * @return bool whether the auth item is successfully added to the system
51
     * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique)
52
     */
53
    abstract protected function addItem($item);
54
55
    /**
56
     * Adds a rule to the RBAC system.
57
     * @param Rule $rule the rule to add
58
     * @return bool whether the rule is successfully added to the system
59
     * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique)
60
     */
61
    abstract protected function addRule($rule);
62
63
    /**
64
     * Removes an auth item from the RBAC system.
65
     * @param Item $item the item to remove
66
     * @return bool whether the role or permission is successfully removed
67
     * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique)
68
     */
69
    abstract protected function removeItem($item);
70
71
    /**
72
     * Removes a rule from the RBAC system.
73
     * @param Rule $rule the rule to remove
74
     * @return bool whether the rule is successfully removed
75
     * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique)
76
     */
77
    abstract protected function removeRule($rule);
78
79
    /**
80
     * Updates an auth item in the RBAC system.
81
     * @param string $name the name of the item being updated
82
     * @param Item $item the updated item
83
     * @return bool whether the auth item is successfully updated
84
     * @throws \Exception if data validation or saving fails (such as the name of the role or permission is not unique)
85
     */
86
    abstract protected function updateItem($name, $item);
87
88
    /**
89
     * Updates a rule to the RBAC system.
90
     * @param string $name the name of the rule being updated
91
     * @param Rule $rule the updated rule
92
     * @return bool whether the rule is successfully updated
93
     * @throws \Exception if data validation or saving fails (such as the name of the rule is not unique)
94
     */
95
    abstract protected function updateRule($name, $rule);
96
97
    /**
98
     * @inheritdoc
99
     */
100 163
    public function createRole($name)
101
    {
102 163
        $role = new Role();
103 163
        $role->name = $name;
104 163
        return $role;
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110 156
    public function createPermission($name)
111
    {
112 156
        $permission = new Permission();
113 156
        $permission->name = $name;
114 156
        return $permission;
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120 163
    public function add($object)
121
    {
122 163
        if ($object instanceof Item) {
123 157
            if ($object->ruleName && $this->getRule($object->ruleName) === null) {
124 6
                $rule = \Yii::createObject($object->ruleName);
125 6
                $rule->name = $object->ruleName;
126 6
                $this->addRule($rule);
127
            }
128 157
            return $this->addItem($object);
129 156
        } elseif ($object instanceof Rule) {
130 156
            return $this->addRule($object);
131
        } else {
132
            throw new InvalidArgumentException('Adding unsupported object type.');
133
        }
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139 7
    public function remove($object)
140
    {
141 7
        if ($object instanceof Item) {
142 7
            return $this->removeItem($object);
143 6
        } elseif ($object instanceof Rule) {
144 6
            return $this->removeRule($object);
145
        } else {
146
            throw new InvalidArgumentException('Removing unsupported object type.');
147
        }
148
    }
149
150
    /**
151
     * @inheritdoc
152
     */
153 16
    public function update($name, $object)
154
    {
155 16
        if ($object instanceof Item) {
156 16
            if ($object->ruleName && $this->getRule($object->ruleName) === null) {
157
                $rule = \Yii::createObject($object->ruleName);
158
                $rule->name = $object->ruleName;
159
                $this->addRule($rule);
160
            }
161 16
            return $this->updateItem($name, $object);
162 6
        } elseif ($object instanceof Rule) {
163 6
            return $this->updateRule($name, $object);
164
        } else {
165
            throw new InvalidArgumentException('Updating unsupported object type.');
166
        }
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172 42
    public function getRole($name)
173
    {
174 42
        $item = $this->getItem($name);
175 42
        return $item instanceof Item && $item->type == Item::TYPE_ROLE ? $item : null;
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181 21
    public function getPermission($name)
182
    {
183 21
        $item = $this->getItem($name);
184 21
        return $item instanceof Item && $item->type == Item::TYPE_PERMISSION ? $item : null;
185
    }
186
187
    /**
188
     * @inheritdoc
189
     */
190 24
    public function getRoles()
191
    {
192 24
        return $this->getItems(Item::TYPE_ROLE);
193
    }
194
195
    /**
196
     * Returns defaultRoles as array of Role objects
197
     * @since 2.0.12
198
     * @return Role[] default roles. The array is indexed by the role names
199
     */
200 12
    public function getDefaultRoleInstances()
201
    {
202 12
        $result = [];
203 12
        foreach ($this->defaultRoles as $roleName) {
204 12
            $result[$roleName] = $this->createRole($roleName);
205
        }
206 12
        return $result;
207
    }
208
209
    /**
210
     * @inheritdoc
211
     */
212 18
    public function getPermissions()
213
    {
214 18
        return $this->getItems(Item::TYPE_PERMISSION);
215
    }
216
217
    /**
218
     * Executes the rule associated with the specified auth item.
219
     *
220
     * If the item does not specify a rule, this method will return true. Otherwise, it will
221
     * return the value of [[Rule::execute()]].
222
     *
223
     * @param string|int $user the user ID. This should be either an integer or a string representing
224
     * the unique identifier of a user. See [[\yii\web\User::id]].
225
     * @param Item $item the auth item that needs to execute its rule
226
     * @param array $params parameters passed to [[CheckAccessInterface::checkAccess()]] and will be passed to the rule
227
     * @return bool the return value of [[Rule::execute()]]. If the auth item does not specify a rule, true will be returned.
228
     * @throws InvalidConfigException if the auth item has an invalid rule.
229
     */
230 24
    protected function executeRule($user, $item, $params)
231
    {
232 24
        if ($item->ruleName === null) {
233 24
            return true;
234
        }
235 20
        $rule = $this->getRule($item->ruleName);
236 20
        if ($rule instanceof Rule) {
237 20
            return $rule->execute($user, $item, $params);
238
        } else {
239
            throw new InvalidConfigException("Rule not found: {$item->ruleName}");
240
        }
241
    }
242
243
    /**
244
     * Checks whether array of $assignments is empty and [[defaultRoles]] property is empty as well
245
     *
246
     * @param Assignment[] $assignments array of user's assignments
247
     * @return bool whether array of $assignments is empty and [[defaultRoles]] property is empty as well
248
     * @since 2.0.11
249
     */
250 32
    protected function hasNoAssignments(array $assignments)
251
    {
252 32
        return empty($assignments) && empty($this->defaultRoles);
253
    }
254
}
255