Completed
Push — master ( 127f02...ecc5eb )
by Lee
06:27 queued 02:34
created

Policy::addPolicies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 3
crap 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
declare(strict_types=1);
4
5
namespace Casbin\Model;
6
7
use Casbin\Exceptions\BatchOperationException;
8
use Casbin\Exceptions\CasbinException;
9
use Casbin\Log\Log;
10
use Casbin\Rbac\RoleManager;
11
use Casbin\Util\Util;
12
13
/**
14
 * Trait Policy.
15
 *
16
 * @author [email protected]
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
17
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
18
trait Policy
19
{
20
    /**
21
     * initializes the roles in RBAC.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
22
     *
23
     * @param RoleManager $rm
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
24
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
25 174
    public function buildRoleLinks(RoleManager $rm): void
26
    {
27 174
        if (!isset($this->items['g'])) {
28 81
            return;
29
        }
30 93
        foreach ($this->items['g'] as $ast) {
31 93
            $ast->buildRoleLinks($rm);
32
        }
33 93
    }
34
35
    /**
36
     * prints the policy to log.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
37
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
38 174
    public function printPolicy(): void
39
    {
40 174
        Log::logPrint('Policy:');
41 174
        foreach (['p', 'g'] as $sec) {
42 174
            if (!isset($this->items[$sec])) {
43 81
                return;
44
            }
45 174
            foreach ($this->items[$sec] as $key => $ast) {
46 174
                Log::logPrint($key, ': ', $ast->value, ': ', $ast->policy);
47
            }
48
        }
49 93
    }
50
51
    /**
52
     * clears all current policy.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
53
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
54 177
    public function clearPolicy(): void
55
    {
56 177
        foreach (['p', 'g'] as $sec) {
57 177
            if (!isset($this->items[$sec])) {
58 81
                return;
59
            }
60 177
            foreach ($this->items[$sec] as $key => $ast) {
61 177
                $this->items[$sec][$key]->policy = [];
62
            }
63
        }
64 96
    }
65
66
    /**
67
     * gets all rules in a policy.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
68
     *
69
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
70
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
71
     *
72
     * @return array
73
     */
74 9
    public function getPolicy(string $sec, string $ptype): array
75
    {
76 9
        return $this->items[$sec][$ptype]->policy;
77
    }
78
79
    /**
80
     * gets rules based on field filters from a policy.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
81
     *
82
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
83
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     * @param int    $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     * @param string ...$fieldValues
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
86
     *
87
     * @return array
88
     */
89 15
    public function getFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): array
90
    {
91 15
        $res = [];
92
93 15
        foreach ($this->items[$sec][$ptype]->policy as $rule) {
94 15
            $matched = true;
95 15
            foreach ($fieldValues as $i => $fieldValue) {
96 15
                if ('' != $fieldValue && $rule[$fieldIndex + $i] != $fieldValue) {
97 15
                    $matched = false;
98
99 15
                    break;
100
                }
101
            }
102
103 15
            if ($matched) {
104 15
                $res[] = $rule;
105
            }
106
        }
107
108 15
        return $res;
109
    }
110
111
    /**
112
     * determines whether a model has the specified policy rule.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
113
     *
114
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
115
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
116
     * @param array  $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
117
     *
118
     * @return bool
119
     */
120 48
    public function hasPolicy(string $sec, string $ptype, array $rule): bool
121
    {
122 48
        if (!isset($this->items[$sec][$ptype])) {
123
            return false;
124
        }
125
126 48
        return in_array($rule, $this->items[$sec][$ptype]->policy, true);
127
    }
128
129
    /**
130
     * determines whether a model has any of the specified policies. If one is found we return false.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
131
     *
132
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
133
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
134
     * @param array  $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
135
     *
136
     * @return bool
137
     */
138 6
    public function hasPolicies(string $sec, string $ptype, array $rules): bool
139
    {
140 6
        if (!isset($this->items[$sec][$ptype])) {
141
            return false;
142
        }
143
144 6
        foreach ($rules as $rule) {
145 6
            if (!in_array($rule, $this->items[$sec][$ptype]->policy, true)) {
146 6
                return false;
147
            }
148
        }
149
150 6
        return true;
151
    }
152
153
    /**
154
     * adds a policy rule to the model.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
155
     *
156
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
157
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
158
     * @param array  $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
159
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
160 54
    public function addPolicy(string $sec, string $ptype, array $rule): void
161
    {
162 54
        $this->items[$sec][$ptype]->policy[] = $rule;
163 54
    }
164
165
    /**
166
     * adds a policy rules to the model.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
167
     *
168
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
169
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
170
     * @param array  $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
171
     *
172
     * @throws CasbinException
173
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
174 6
    public function addPolicies(string $sec, string $ptype, array $rules): void
175
    {
176 6
        if (count(array_unique($rules, SORT_REGULAR), COUNT_RECURSIVE) !== count($rules, COUNT_RECURSIVE)) {
177 3
            throw new BatchOperationException('addPolicies error: $rules elements can not be duplicated.');
178
        }
179
180 6
        $this->items[$sec][$ptype]->policy = array_merge($this->items[$sec][$ptype]->policy, $rules);
181 6
    }
182
183
    /**
184
     * removes a policy rule from the model.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
185
     *
186
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
187
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
188
     * @param array  $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
189
     *
190
     * @return bool
191
     */
192 30
    public function removePolicy(string $sec, string $ptype, array $rule): bool
193
    {
194 30
        if (!isset($this->items[$sec][$ptype])) {
195
            return false;
196
        }
197
198 30
        $offset = array_search($rule, $this->items[$sec][$ptype]->policy, true);
199
200 30
        if (false === $offset) {
201 6
            return false;
202
        }
203
204 30
        array_splice($this->items[$sec][$ptype]->policy, $offset, 1);
0 ignored issues
show
Bug introduced by
It seems like $offset can also be of type string; however, parameter $offset of array_splice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

204
        array_splice($this->items[$sec][$ptype]->policy, /** @scrutinizer ignore-type */ $offset, 1);
Loading history...
205
206 30
        return true;
207
    }
208
209
    /**
210
     * removes a policy rules from the model.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
211
     *
212
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
213
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
214
     * @param array  $rules
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
215
     *
216
     * @return bool
217
     */
218 6
    public function removePolicies(string $sec, string $ptype, array $rules): bool
219
    {
220 6
        if (!isset($this->items[$sec][$ptype])) {
221
            return false;
222
        }
223
224 6
        $policy = $this->items[$sec][$ptype]->policy;
225
226 6
        foreach ($rules as $rule) {
227 6
            $offset = array_search($rule, $policy, true);
228
229 6
            if (false === $offset) {
230 6
                return false;
231
            }
232
233 6
            array_splice($policy, $offset, 1);
0 ignored issues
show
Bug introduced by
It seems like $offset can also be of type string; however, parameter $offset of array_splice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

233
            array_splice($policy, /** @scrutinizer ignore-type */ $offset, 1);
Loading history...
234
        }
235
236 6
        $this->items[$sec][$ptype]->policy = $policy;
237
238 6
        return true;
239
    }
240
241
    /**
242
     * removes policy rules based on field filters from the model.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
243
     *
244
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
245
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
246
     * @param int    $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
247
     * @param string ...$fieldValues
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
248
     *
249
     * @return bool
250
     */
251 27
    public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): bool
252
    {
253 27
        $tmp = [];
254 27
        $res = false;
255
256 27
        if (!isset($this->items[$sec][$ptype])) {
257
            return $res;
258
        }
259
260 27
        foreach ($this->items[$sec][$ptype]->policy as $rule) {
261 27
            $matched = true;
262 27
            foreach ($fieldValues as $i => $fieldValue) {
263 27
                if ('' != $fieldValue && $rule[$fieldIndex + $i] != $fieldValue) {
264 18
                    $matched = false;
265
266 24
                    break;
267
                }
268
            }
269
270 27
            if ($matched) {
271 27
                $res = true;
272
            } else {
273 24
                $tmp[] = $rule;
274
            }
275
        }
276
277 27
        $this->items[$sec][$ptype]->policy = $tmp;
278
279 27
        return $res;
280
    }
281
282
    /**
283
     * gets all values for a field for all rules in a policy, duplicated values are removed.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
284
     *
285
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
286
     * @param string $ptype
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
287
     * @param int    $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
288
     *
289
     * @return array
290
     */
291 9
    public function getValuesForFieldInPolicy(string $sec, string $ptype, int $fieldIndex): array
292
    {
293 9
        $values = [];
294
295 9
        if (!isset($this->items[$sec][$ptype])) {
296
            return $values;
297
        }
298
299 9
        foreach ($this->items[$sec][$ptype]->policy as $rule) {
300 9
            $values[] = $rule[$fieldIndex];
301
        }
302
303 9
        Util::arrayRemoveDuplicates($values);
304
305 9
        return $values;
306
    }
307
308
    /**
309
     * gets all values for a field for all rules in a policy of all ptypes, duplicated values are removed.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
310
     *
311
     * @param string $sec
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
312
     * @param int    $fieldIndex
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
313
     *
314
     * @return array
315
     */
316 6
    public function getValuesForFieldInPolicyAllTypes(string $sec, int $fieldIndex): array
317
    {
318 6
        $values = [];
319
320 6
        foreach ($this->items[$sec] as $key => $ptype) {
321 6
            $values = array_merge($values, $this->getValuesForFieldInPolicy($sec, $key, $fieldIndex));
322
        }
323
324 6
        Util::arrayRemoveDuplicates($values);
325
326 6
        return $values;
327
    }
328
}
329