enableCache();
17 | $this->assertFalse(Enforcer::enforce('eve', 'data3', 'read'));
18 | Enforcer::addPermissionForUser('eve', 'data3', 'read');
19 | $this->refreshPolicies();
20 | $this->assertTrue(Enforcer::enforce('eve', 'data3', 'read'));
21 | }
22 |
23 | public function testAddPolicies()
24 | {
25 | $this->enableCache();
26 | $policies = [
27 | ['u1', 'd1', 'read'],
28 | ['u2', 'd2', 'read'],
29 | ['u3', 'd3', 'read'],
30 | ];
31 | $this->refreshPolicies();
32 | Rule::truncate();
33 | Enforcer::addPolicies($policies);
34 | $this->refreshPolicies();
35 | $this->assertEquals($policies, Enforcer::getPolicy());
36 | }
37 |
38 | public function testSavePolicy()
39 | {
40 | $this->enableCache();
41 | $this->assertFalse(Enforcer::enforce('alice', 'data4', 'read'));
42 |
43 | $model = Enforcer::getModel();
44 | $model->clearPolicy();
45 | $model->addPolicy('p', 'p', ['alice', 'data4', 'read']);
46 |
47 | $adapter = Enforcer::getAdapter();
48 | $adapter->savePolicy($model);
49 | $this->refreshPolicies();
50 | $this->assertTrue(Enforcer::enforce('alice', 'data4', 'read'));
51 | }
52 |
53 | public function testRemovePolicy()
54 | {
55 | $this->enableCache();
56 | $this->assertFalse(Enforcer::enforce('alice', 'data5', 'read'));
57 |
58 | Enforcer::addPermissionForUser('alice', 'data5', 'read');
59 | $this->refreshPolicies();
60 | $this->assertTrue(Enforcer::enforce('alice', 'data5', 'read'));
61 |
62 | Enforcer::deletePermissionForUser('alice', 'data5', 'read');
63 | $this->refreshPolicies();
64 | $this->assertFalse(Enforcer::enforce('alice', 'data5', 'read'));
65 | }
66 |
67 | public function testRemovePolicies()
68 | {
69 | $this->enableCache();
70 | $this->assertEquals([
71 | ['alice', 'data1', 'read'],
72 | ['bob', 'data2', 'write'],
73 | ['data2_admin', 'data2', 'read'],
74 | ['data2_admin', 'data2', 'write'],
75 | ], Enforcer::getPolicy());
76 |
77 | Enforcer::removePolicies([
78 | ['data2_admin', 'data2', 'read'],
79 | ['data2_admin', 'data2', 'write'],
80 | ]);
81 | $this->refreshPolicies();
82 | $this->assertEquals([
83 | ['alice', 'data1', 'read'],
84 | ['bob', 'data2', 'write']
85 | ], Enforcer::getPolicy());
86 | }
87 |
88 | public function testRemoveFilteredPolicy()
89 | {
90 | $this->enableCache();
91 | $this->assertTrue(Enforcer::enforce('alice', 'data1', 'read'));
92 | Enforcer::removeFilteredPolicy(1, 'data1');
93 | $this->refreshPolicies();
94 | $this->assertFalse(Enforcer::enforce('alice', 'data1', 'read'));
95 | $this->assertTrue(Enforcer::enforce('bob', 'data2', 'write'));
96 | $this->assertTrue(Enforcer::enforce('alice', 'data2', 'read'));
97 | $this->assertTrue(Enforcer::enforce('alice', 'data2', 'write'));
98 | Enforcer::removeFilteredPolicy(1, 'data2', 'read');
99 | $this->refreshPolicies();
100 | $this->assertTrue(Enforcer::enforce('bob', 'data2', 'write'));
101 | $this->assertFalse(Enforcer::enforce('alice', 'data2', 'read'));
102 | $this->assertTrue(Enforcer::enforce('alice', 'data2', 'write'));
103 | Enforcer::removeFilteredPolicy(2, 'write');
104 | $this->refreshPolicies();
105 | $this->assertFalse(Enforcer::enforce('bob', 'data2', 'write'));
106 | $this->assertFalse(Enforcer::enforce('alice', 'data2', 'write'));
107 | }
108 |
109 | public function testUpdatePolicy()
110 | {
111 | $this->enableCache();
112 | $this->assertEquals([
113 | ['alice', 'data1', 'read'],
114 | ['bob', 'data2', 'write'],
115 | ['data2_admin', 'data2', 'read'],
116 | ['data2_admin', 'data2', 'write'],
117 | ], Enforcer::getPolicy());
118 |
119 | Enforcer::updatePolicy(
120 | ['alice', 'data1', 'read'],
121 | ['alice', 'data1', 'write']
122 | );
123 |
124 | Enforcer::updatePolicy(
125 | ['bob', 'data2', 'write'],
126 | ['bob', 'data2', 'read']
127 | );
128 | $this->refreshPolicies();
129 | $this->assertEquals([
130 | ['alice', 'data1', 'write'],
131 | ['bob', 'data2', 'read'],
132 | ['data2_admin', 'data2', 'read'],
133 | ['data2_admin', 'data2', 'write'],
134 | ], Enforcer::getPolicy());
135 | }
136 |
137 | protected function refreshPolicies()
138 | {
139 | Enforcer::loadPolicy();
140 | }
141 |
142 | protected function enableCache()
143 | {
144 | $this->app['config']->set('lauthz.basic.cache.enabled', true);
145 | }
146 |
147 | }
148 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | Laravel Authorization
3 |
4 |
5 |
6 | Laravel-authz is an authorization library for the laravel framework.
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | It's based on [Casbin](https://github.com/php-casbin/php-casbin), an authorization library that supports access control models like ACL, RBAC, ABAC.
28 |
29 | All you need to learn to use `Casbin` first.
30 |
31 | * [Installation](#installation)
32 | * [Usage](#usage)
33 | * [Quick start](#quick-start)
34 | * [Using Enforcer Api](#using-enforcer-api)
35 | * [Using a middleware](#using-a-middleware)
36 | * [basic Enforcer Middleware](#basic-enforcer-middleware)
37 | * [HTTP Request Middleware ( RESTful is also supported )](#http-request-middleware--restful-is-also-supported-)
38 | * [Using Gates](#using-gates)
39 | * [Multiple enforcers](#multiple-enforcers)
40 | * [Using artisan commands](#using-artisan-commands)
41 | * [Cache](#using-cache)
42 | * [Thinks](#thinks)
43 | * [License](#license)
44 |
45 | ## Installation
46 |
47 | Require this package in the `composer.json` of your Laravel project. This will download the package.
48 |
49 | ```
50 | composer require casbin/laravel-authz
51 | ```
52 |
53 | The `Lauthz\LauthzServiceProvider` is `auto-discovered` and registered by default, but if you want to register it yourself:
54 |
55 | Add the ServiceProvider in `config/app.php`
56 |
57 | ```php
58 | 'providers' => [
59 | /*
60 | * Package Service Providers...
61 | */
62 | Lauthz\LauthzServiceProvider::class,
63 | ]
64 | ```
65 |
66 | The Enforcer facade is also `auto-discovered`, but if you want to add it manually:
67 |
68 | Add the Facade in `config/app.php`
69 |
70 | ```php
71 | 'aliases' => [
72 | // ...
73 | 'Enforcer' => Lauthz\Facades\Enforcer::class,
74 | ]
75 | ```
76 |
77 | To publish the config, run the vendor publish command:
78 |
79 | ```
80 | php artisan vendor:publish
81 | ```
82 |
83 | This will create a new model config file named `config/lauthz-rbac-model.conf` and a new lauthz config file named `config/lauthz.php`.
84 |
85 |
86 | To migrate the migrations, run the migrate command:
87 |
88 | ```
89 | php artisan migrate
90 | ```
91 |
92 | This will create a new table named `rules`
93 |
94 |
95 | ## Usage
96 |
97 | ### Quick start
98 |
99 | Once installed you can do stuff like this:
100 |
101 | ```php
102 |
103 | use Enforcer;
104 |
105 | // adds permissions to a user
106 | Enforcer::addPermissionForUser('eve', 'articles', 'read');
107 | // adds a role for a user.
108 | Enforcer::addRoleForUser('eve', 'writer');
109 | // adds permissions to a role
110 | Enforcer::addPolicy('writer', 'articles','edit');
111 |
112 | ```
113 |
114 | You can check if a user has a permission like this:
115 |
116 | ```php
117 | // to check if a user has permission
118 | if (Enforcer::enforce("eve", "articles", "edit")) {
119 | // permit eve to edit articles
120 | } else {
121 | // deny the request, show an error
122 | }
123 |
124 | ```
125 |
126 | By default, [Gate](https://laravel.com/docs/11.x/authorization#gates) checks will be automatically intercepted
127 | . You can check if a user has a permission with Laravel's default `can` function:
128 |
129 | ```php
130 | $user->can('articles,read');
131 | ```
132 |
133 | ### Using Enforcer Api
134 |
135 | It provides a very rich api to facilitate various operations on the Policy:
136 |
137 | Gets all roles:
138 |
139 | ```php
140 | Enforcer::getAllRoles(); // ['writer', 'reader']
141 | ```
142 |
143 | Gets all the authorization rules in the policy.:
144 |
145 | ```php
146 | Enforcer::getPolicy();
147 | ```
148 |
149 | Gets the roles that a user has.
150 |
151 | ```php
152 | Enforcer::getRolesForUser('eve'); // ['writer']
153 | ```
154 |
155 | Gets the users that has a role.
156 |
157 | ```php
158 | Enforcer::getUsersForRole('writer'); // ['eve']
159 | ```
160 |
161 | Determines whether a user has a role.
162 |
163 | ```php
164 | Enforcer::hasRoleForUser('eve', 'writer'); // true or false
165 | ```
166 |
167 | Adds a role for a user.
168 |
169 | ```php
170 | Enforcer::addRoleForUser('eve', 'writer');
171 | ```
172 |
173 | Adds a permission for a user or role.
174 |
175 | ```php
176 | // to user
177 | Enforcer::addPermissionForUser('eve', 'articles', 'read');
178 | // to role
179 | Enforcer::addPermissionForUser('writer', 'articles','edit');
180 | ```
181 |
182 | Deletes a role for a user.
183 |
184 | ```php
185 | Enforcer::deleteRoleForUser('eve', 'writer');
186 | ```
187 |
188 | Deletes all roles for a user.
189 |
190 | ```php
191 | Enforcer::deleteRolesForUser('eve');
192 | ```
193 |
194 | Deletes a role.
195 |
196 | ```php
197 | Enforcer::deleteRole('writer');
198 | ```
199 |
200 | Deletes a permission.
201 |
202 | ```php
203 | Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).
204 | ```
205 |
206 | Deletes a permission for a user or role.
207 |
208 | ```php
209 | Enforcer::deletePermissionForUser('eve', 'articles', 'read');
210 | ```
211 |
212 | Deletes permissions for a user or role.
213 |
214 | ```php
215 | // to user
216 | Enforcer::deletePermissionsForUser('eve');
217 | // to role
218 | Enforcer::deletePermissionsForUser('writer');
219 | ```
220 |
221 | Gets permissions for a user or role.
222 |
223 | ```php
224 | Enforcer::getPermissionsForUser('eve'); // return array
225 | ```
226 |
227 | Determines whether a user has a permission.
228 |
229 | ```php
230 | Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or false
231 | ```
232 |
233 | See [Casbin API](https://casbin.org/docs/management-api#reference) for more APIs.
234 |
235 | ### Using a middleware
236 |
237 | This package comes with `EnforcerMiddleware`, `RequestMiddleware` middlewares. You can add them inside your `app/Http/Kernel.php` file.
238 |
239 | ```php
240 | protected $routeMiddleware = [
241 | // ...
242 | // a basic Enforcer Middleware
243 | 'enforcer' => \Lauthz\Middlewares\EnforcerMiddleware::class,
244 | // an HTTP Request Middleware
245 | 'http_request' => \Lauthz\Middlewares\RequestMiddleware::class,
246 | ];
247 | ```
248 |
249 | #### basic Enforcer Middleware
250 |
251 | Then you can protect your routes using middleware rules:
252 |
253 | ```php
254 | Route::group(['middleware' => ['enforcer:articles,read']], function () {
255 | // pass
256 | });
257 | ```
258 |
259 | #### HTTP Request Middleware ( RESTful is also supported )
260 |
261 | If you need to authorize a Request,you need to define the model configuration first in `config/lauthz-rbac-model.conf`:
262 |
263 | ```ini
264 | [request_definition]
265 | r = sub, obj, act
266 |
267 | [policy_definition]
268 | p = sub, obj, act
269 |
270 | [role_definition]
271 | g = _, _
272 |
273 | [policy_effect]
274 | e = some(where (p.eft == allow))
275 |
276 | [matchers]
277 | m = g(r.sub, p.sub) && keyMatch2(r.obj, p.obj) && regexMatch(r.act, p.act)
278 | ```
279 |
280 | Then, using middleware rules:
281 |
282 | ```php
283 | Route::group(['middleware' => ['http_request']], function () {
284 | Route::resource('photo', 'PhotoController');
285 | });
286 | ```
287 |
288 | ### Using Gates
289 |
290 | You can use Laravel Gates to check if a user has a permission, provided that you have set an existing user instance as the currently authenticated user.
291 |
292 | ```php
293 | $user->can('articles,read');
294 | // For multiple enforcers
295 | $user->can('articles,read', 'second');
296 | // The methods cant, cannot, canAny, etc. also work
297 | ```
298 |
299 | If you require custom Laravel Gates, you can disable the automatic registration by setting `enabled_register_at_gates` to `false` in the lauthz file. After that, you can use `Gates::before` or `Gates::after` in your ServiceProvider to register custom Gates. See [Gates](https://laravel.com/docs/11.x/authorization#gates) for more details.
300 |
301 | ### Multiple enforcers
302 |
303 | If you need multiple permission controls in your project, you can configure multiple enforcers.
304 |
305 | In the lauthz file, it should be like this:
306 |
307 | ```php
308 | return [
309 | 'default' => 'basic',
310 |
311 | 'basic' => [
312 | 'model' => [
313 | // ...
314 | ],
315 |
316 | 'adapter' => Lauthz\Adapters\DatabaseAdapter::class,
317 | // ...
318 | ],
319 |
320 | 'second' => [
321 | 'model' => [
322 | // ...
323 | ],
324 |
325 | 'adapter' => Lauthz\Adapters\DatabaseAdapter::class,
326 | // ...
327 | ],
328 | ];
329 |
330 | ```
331 |
332 | Then you can choose which enforcers to use.
333 |
334 | ```php
335 | Enforcer::guard('second')->enforce("eve", "articles", "edit");
336 | ```
337 |
338 |
339 | ### Using artisan commands
340 |
341 | You can create a policy from a console with artisan commands.
342 |
343 | To user:
344 |
345 | ```bash
346 | php artisan policy:add eve,articles,read
347 | ```
348 |
349 | To Role:
350 |
351 | ```bash
352 | php artisan policy:add writer,articles,edit
353 | ```
354 |
355 | Adds a role for a user:
356 |
357 | ```bash
358 | php artisan role:assign eve writer
359 | # Specify the ptype of the role assignment by using the --ptype option.
360 | php artisan role:assign eve writer --ptype=g2
361 | ```
362 |
363 | ### Using cache
364 |
365 | Authorization rules are cached to speed up performance. The default is off.
366 |
367 | Sets your own cache configs in Laravel's `config/lauthz.php`.
368 |
369 | ```php
370 | 'cache' => [
371 | // changes whether Lauthz will cache the rules.
372 | 'enabled' => false,
373 |
374 | // cache store
375 | 'store' => 'default',
376 |
377 | // cache Key
378 | 'key' => 'rules',
379 |
380 | // ttl \DateTimeInterface|\DateInterval|int|null
381 | 'ttl' => 24 * 60,
382 | ],
383 | ```
384 |
385 | ## Thinks
386 |
387 | [Casbin](https://github.com/php-casbin/php-casbin) in Laravel. You can find the full documentation of Casbin [on the website](https://casbin.org/).
388 |
389 | ## License
390 |
391 | This project is licensed under the [Apache 2.0 license](LICENSE).
392 |
--------------------------------------------------------------------------------
/src/Adapters/DatabaseAdapter.php:
--------------------------------------------------------------------------------
1 | eloquent = $eloquent;
48 | }
49 |
50 | /**
51 | * Filter the rule.
52 | *
53 | * @param array $rule
54 | * @return array
55 | */
56 | public function filterRule(array $rule): array
57 | {
58 | $rule = array_values($rule);
59 |
60 | $i = count($rule) - 1;
61 | for (; $i >= 0; $i--) {
62 | if ($rule[$i] != '' && !is_null($rule[$i])) {
63 | break;
64 | }
65 | }
66 |
67 | return array_slice($rule, 0, $i + 1);
68 | }
69 |
70 | /**
71 | * savePolicyLine function.
72 | *
73 | * @param string $ptype
74 | * @param array $rule
75 | */
76 | public function savePolicyLine(string $ptype, array $rule): void
77 | {
78 | $col['ptype'] = $ptype;
79 | foreach ($rule as $key => $value) {
80 | $col['v'.strval($key)] = $value;
81 | }
82 |
83 | $this->eloquent->create($col);
84 | }
85 |
86 | /**
87 | * loads all policy rules from the storage.
88 | *
89 | * @param Model $model
90 | */
91 | public function loadPolicy(Model $model): void
92 | {
93 | $rows = $this->eloquent->getAllFromCache();
94 |
95 | foreach ($rows as $row) {
96 | $this->loadPolicyArray($this->filterRule($row), $model);
97 | }
98 | }
99 |
100 | /**
101 | * saves all policy rules to the storage.
102 | *
103 | * @param Model $model
104 | */
105 | public function savePolicy(Model $model): void
106 | {
107 | foreach ($model['p'] as $ptype => $ast) {
108 | foreach ($ast->policy as $rule) {
109 | $this->savePolicyLine($ptype, $rule);
110 | }
111 | }
112 |
113 | foreach ($model['g'] as $ptype => $ast) {
114 | foreach ($ast->policy as $rule) {
115 | $this->savePolicyLine($ptype, $rule);
116 | }
117 | }
118 | }
119 |
120 | /**
121 | * adds a policy rule to the storage.
122 | * This is part of the Auto-Save feature.
123 | *
124 | * @param string $sec
125 | * @param string $ptype
126 | * @param array $rule
127 | */
128 | public function addPolicy(string $sec, string $ptype, array $rule): void
129 | {
130 | $this->savePolicyLine($ptype, $rule);
131 | }
132 |
133 | /**
134 | * Adds a policy rules to the storage.
135 | * This is part of the Auto-Save feature.
136 | *
137 | * @param string $sec
138 | * @param string $ptype
139 | * @param string[][] $rules
140 | */
141 | public function addPolicies(string $sec, string $ptype, array $rules): void
142 | {
143 | $cols = [];
144 | $i = 0;
145 |
146 | foreach($rules as $rule) {
147 | $temp['ptype'] = $ptype;
148 | $temp['created_at'] = new DateTime();
149 | $temp['updated_at'] = $temp['created_at'];
150 | foreach ($rule as $key => $value) {
151 | $temp['v'.strval($key)] = $value;
152 | }
153 | $cols[$i++] = $temp ?? [];
154 | $temp = [];
155 | }
156 | $this->eloquent->insert($cols);
157 | Rule::fireModelEvent('saved');
158 | }
159 |
160 | /**
161 | * This is part of the Auto-Save feature.
162 | *
163 | * @param string $sec
164 | * @param string $ptype
165 | * @param array $rule
166 | */
167 | public function removePolicy(string $sec, string $ptype, array $rule): void
168 | {
169 | $instance = $this->eloquent->where('ptype', $ptype);
170 |
171 | foreach ($rule as $key => $value) {
172 | $instance->where('v'.strval($key), $value);
173 | }
174 |
175 | $instance->delete();
176 | Rule::fireModelEvent('deleted');
177 | }
178 |
179 | /**
180 | * Removes policy rules from the storage.
181 | * This is part of the Auto-Save feature.
182 | *
183 | * @param string $sec
184 | * @param string $ptype
185 | * @param string[][] $rules
186 | * @throws Throwable
187 | */
188 | public function removePolicies(string $sec, string $ptype, array $rules): void
189 | {
190 | $this->eloquent->getConnection()->transaction(function () use ($sec, $rules, $ptype) {
191 | foreach ($rules as $rule) {
192 | $this->removePolicy($sec, $ptype, $rule);
193 | }
194 | });
195 | }
196 |
197 | /**
198 | * @param string $sec
199 | * @param string $ptype
200 | * @param int $fieldIndex
201 | * @param string|null ...$fieldValues
202 | * @return array
203 | * @throws Throwable
204 | */
205 | public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, ?string ...$fieldValues): array
206 | {
207 | $removedRules = [];
208 | $instance = $this->eloquent->where('ptype', $ptype);
209 |
210 | foreach (range(0, 5) as $value) {
211 | if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
212 | if ('' != $fieldValues[$value - $fieldIndex]) {
213 | $instance->where('v' . strval($value), $fieldValues[$value - $fieldIndex]);
214 | }
215 | }
216 | }
217 |
218 | $oldP = $instance->get()->makeHidden(['created_at','updated_at', 'id', 'ptype'])->toArray();
219 | foreach ($oldP as &$item) {
220 | $item = $this->filterRule($item);
221 | $removedRules[] = $item;
222 | }
223 |
224 | $instance->delete();
225 | Rule::fireModelEvent('deleted');
226 |
227 | return $removedRules;
228 | }
229 |
230 | /**
231 | * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
232 | * This is part of the Auto-Save feature.
233 | *
234 | * @param string $sec
235 | * @param string $ptype
236 | * @param int $fieldIndex
237 | * @param string|null ...$fieldValues
238 | * @return void
239 | * @throws Throwable
240 | */
241 | public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, ?string ...$fieldValues): void
242 | {
243 | $this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
244 | }
245 |
246 | /**
247 | * Updates a policy rule from storage.
248 | * This is part of the Auto-Save feature.
249 | *
250 | * @param string $sec
251 | * @param string $ptype
252 | * @param string[] $oldRule
253 | * @param string[] $newPolicy
254 | */
255 | public function updatePolicy(string $sec, string $ptype, array $oldRule, array $newPolicy): void
256 | {
257 | $instance = $this->eloquent->where('ptype', $ptype);
258 | foreach($oldRule as $k => $v) {
259 | $instance->where('v' . $k, $v);
260 | }
261 | $instance = $instance->first();
262 | if (!$instance) {
263 | return;
264 | }
265 |
266 | $update = [];
267 | foreach($newPolicy as $k => $v) {
268 | $update['v' . $k] = $v;
269 | }
270 | $instance->update($update);
271 | Rule::fireModelEvent('saved');
272 | }
273 |
274 | /**
275 | * UpdatePolicies updates some policy rules to storage, like db, redis.
276 | *
277 | * @param string $sec
278 | * @param string $ptype
279 | * @param string[][] $oldRules
280 | * @param string[][] $newRules
281 | * @return void
282 | * @throws Throwable
283 | */
284 | public function updatePolicies(string $sec, string $ptype, array $oldRules, array $newRules): void
285 | {
286 | $this->eloquent->getConnection()->transaction(function () use ($sec, $ptype, $oldRules, $newRules) {
287 | foreach ($oldRules as $i => $oldRule) {
288 | $this->updatePolicy($sec, $ptype, $oldRule, $newRules[$i]);
289 | }
290 | });
291 | }
292 |
293 | /**
294 | * UpdateFilteredPolicies deletes old rules and adds new rules.
295 | *
296 | * @param string $sec
297 | * @param string $ptype
298 | * @param array $newPolicies
299 | * @param integer $fieldIndex
300 | * @param string ...$fieldValues
301 | * @return array
302 | * @throws Throwable
303 | */
304 | public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array
305 | {
306 | $oldRules = [];
307 | $this->eloquent->getConnection()->transaction(function () use ($sec, $ptype, $fieldIndex, $fieldValues, $newPolicies, &$oldRules) {
308 | $oldRules = $this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
309 | $this->addPolicies($sec, $ptype, $newPolicies);
310 | });
311 | return $oldRules;
312 | }
313 |
314 | /**
315 | * Loads only policy rules that match the filter.
316 | *
317 | * @param Model $model
318 | * @param mixed $filter
319 | * @throws InvalidFilterTypeException
320 | */
321 | public function loadFilteredPolicy(Model $model, $filter): void
322 | {
323 | $instance = $this->eloquent;
324 |
325 | if (is_string($filter)) {
326 | $instance = $instance->whereRaw($filter);
327 | } else if ($filter instanceof Filter) {
328 | foreach($filter->p as $k => $v) {
329 | $where[$v] = $filter->g[$k];
330 | $instance = $instance->where($v, $filter->g[$k]);
331 | }
332 | } else if ($filter instanceof \Closure) {
333 | $instance = $instance->where($filter);
334 | } else {
335 | throw new InvalidFilterTypeException('invalid filter type');
336 | }
337 | $rows = $instance->get()->makeHidden(['created_at','updated_at', 'id'])->toArray();
338 | foreach ($rows as $row) {
339 | $row = array_filter($row, static fn($value): bool => !is_null($value) && $value !== '');
340 | $line = implode(', ', array_filter($row, static fn ($val): bool => '' != $val && !is_null($val)));
341 | $this->loadPolicyLine(trim($line), $model);
342 | }
343 | $this->setFiltered(true);
344 | }
345 |
346 | /**
347 | * Returns true if the loaded policy has been filtered.
348 | *
349 | * @return bool
350 | */
351 | public function isFiltered(): bool
352 | {
353 | return $this->filtered;
354 | }
355 |
356 | /**
357 | * Sets filtered parameter.
358 | *
359 | * @param bool $filtered
360 | */
361 | public function setFiltered(bool $filtered): void
362 | {
363 | $this->filtered = $filtered;
364 | }
365 | }
366 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/tests/DatabaseAdapterTest.php:
--------------------------------------------------------------------------------
1 | assertTrue(Enforcer::enforce('alice', 'data1', 'read'));
17 |
18 | $this->assertFalse(Enforcer::enforce('bob', 'data1', 'read'));
19 | $this->assertTrue(Enforcer::enforce('bob', 'data2', 'write'));
20 |
21 | $this->assertTrue(Enforcer::enforce('alice', 'data2', 'read'));
22 | $this->assertTrue(Enforcer::enforce('alice', 'data2', 'write'));
23 | }
24 |
25 | public function testAddPolicy()
26 | {
27 | $this->assertFalse(Enforcer::enforce('eve', 'data3', 'read'));
28 | Enforcer::addPermissionForUser('eve', 'data3', 'read');
29 | $this->assertTrue(Enforcer::enforce('eve', 'data3', 'read'));
30 | }
31 |
32 | public function testAddPolicies()
33 | {
34 | $policies = [
35 | ['u1', 'd1', 'read'],
36 | ['u2', 'd2', 'read'],
37 | ['u3', 'd3', 'read'],
38 | ];
39 | Enforcer::clearPolicy();
40 | $this->initTable();
41 | $this->assertEquals([], Enforcer::getPolicy());
42 | Enforcer::addPolicies($policies);
43 | $this->assertEquals($policies, Enforcer::getPolicy());
44 | }
45 |
46 | public function testSavePolicy()
47 | {
48 | $this->assertFalse(Enforcer::enforce('alice', 'data4', 'read'));
49 |
50 | $model = Enforcer::getModel();
51 | $model->clearPolicy();
52 | $model->addPolicy('p', 'p', ['alice', 'data4', 'read']);
53 |
54 | $adapter = Enforcer::getAdapter();
55 | $adapter->savePolicy($model);
56 | $this->assertTrue(Enforcer::enforce('alice', 'data4', 'read'));
57 | }
58 |
59 | public function testRemovePolicy()
60 | {
61 | $this->assertFalse(Enforcer::enforce('alice', 'data5', 'read'));
62 |
63 | Enforcer::addPermissionForUser('alice', 'data5', 'read');
64 | $this->assertTrue(Enforcer::enforce('alice', 'data5', 'read'));
65 |
66 | Enforcer::deletePermissionForUser('alice', 'data5', 'read');
67 | $this->assertFalse(Enforcer::enforce('alice', 'data5', 'read'));
68 | }
69 |
70 | public function testRemovePolicies()
71 | {
72 | $this->assertEquals([
73 | ['alice', 'data1', 'read'],
74 | ['bob', 'data2', 'write'],
75 | ['data2_admin', 'data2', 'read'],
76 | ['data2_admin', 'data2', 'write'],
77 | ], Enforcer::getPolicy());
78 |
79 | Enforcer::removePolicies([
80 | ['data2_admin', 'data2', 'read'],
81 | ['data2_admin', 'data2', 'write'],
82 | ]);
83 |
84 | $this->assertEquals([
85 | ['alice', 'data1', 'read'],
86 | ['bob', 'data2', 'write']
87 | ], Enforcer::getPolicy());
88 | }
89 |
90 | public function testRemoveFilteredPolicy()
91 | {
92 | $this->assertTrue(Enforcer::enforce('alice', 'data1', 'read'));
93 | Enforcer::removeFilteredPolicy(1, 'data1');
94 | $this->assertFalse(Enforcer::enforce('alice', 'data1', 'read'));
95 | $this->assertTrue(Enforcer::enforce('bob', 'data2', 'write'));
96 | $this->assertTrue(Enforcer::enforce('alice', 'data2', 'read'));
97 | $this->assertTrue(Enforcer::enforce('alice', 'data2', 'write'));
98 | Enforcer::removeFilteredPolicy(1, 'data2', 'read');
99 | $this->assertTrue(Enforcer::enforce('bob', 'data2', 'write'));
100 | $this->assertFalse(Enforcer::enforce('alice', 'data2', 'read'));
101 | $this->assertTrue(Enforcer::enforce('alice', 'data2', 'write'));
102 | Enforcer::removeFilteredPolicy(2, 'write');
103 | $this->assertFalse(Enforcer::enforce('bob', 'data2', 'write'));
104 | $this->assertFalse(Enforcer::enforce('alice', 'data2', 'write'));
105 | }
106 |
107 | public function testUpdatePolicy()
108 | {
109 | $this->assertEquals([
110 | ['alice', 'data1', 'read'],
111 | ['bob', 'data2', 'write'],
112 | ['data2_admin', 'data2', 'read'],
113 | ['data2_admin', 'data2', 'write'],
114 | ], Enforcer::getPolicy());
115 |
116 | Enforcer::updatePolicy(
117 | ['alice', 'data1', 'read'],
118 | ['alice', 'data1', 'write']
119 | );
120 |
121 | Enforcer::updatePolicy(
122 | ['bob', 'data2', 'write'],
123 | ['bob', 'data2', 'read']
124 | );
125 |
126 | $this->assertEquals([
127 | ['alice', 'data1', 'write'],
128 | ['bob', 'data2', 'read'],
129 | ['data2_admin', 'data2', 'read'],
130 | ['data2_admin', 'data2', 'write'],
131 | ], Enforcer::getPolicy());
132 | }
133 |
134 | public function testUpdatePolicies()
135 | {
136 | $this->assertEquals([
137 | ['alice', 'data1', 'read'],
138 | ['bob', 'data2', 'write'],
139 | ['data2_admin', 'data2', 'read'],
140 | ['data2_admin', 'data2', 'write'],
141 | ], Enforcer::getPolicy());
142 |
143 | $oldPolicies = [
144 | ['alice', 'data1', 'read'],
145 | ['bob', 'data2', 'write']
146 | ];
147 | $newPolicies = [
148 | ['alice', 'data1', 'write'],
149 | ['bob', 'data2', 'read']
150 | ];
151 |
152 | Enforcer::updatePolicies($oldPolicies, $newPolicies);
153 |
154 | $this->assertEquals([
155 | ['alice', 'data1', 'write'],
156 | ['bob', 'data2', 'read'],
157 | ['data2_admin', 'data2', 'read'],
158 | ['data2_admin', 'data2', 'write'],
159 | ], Enforcer::getPolicy());
160 | }
161 |
162 | public function arrayEqualsWithoutOrder(array $expected, array $actual)
163 | {
164 | if (method_exists($this, 'assertEqualsCanonicalizing')) {
165 | $this->assertEqualsCanonicalizing($expected, $actual);
166 | } else {
167 | array_multisort($expected);
168 | array_multisort($actual);
169 | $this->assertEquals($expected, $actual);
170 | }
171 | }
172 |
173 | public function testUpdateFilteredPolicies()
174 | {
175 | $this->assertEquals([
176 | ['alice', 'data1', 'read'],
177 | ['bob', 'data2', 'write'],
178 | ['data2_admin', 'data2', 'read'],
179 | ['data2_admin', 'data2', 'write'],
180 | ], Enforcer::getPolicy());
181 |
182 | Enforcer::updateFilteredPolicies([["alice", "data1", "write"]], 0, "alice", "data1", "read");
183 | Enforcer::updateFilteredPolicies([["bob", "data2", "read"]], 0, "bob", "data2", "write");
184 |
185 | $policies = [
186 | ['alice', 'data1', 'write'],
187 | ['bob', 'data2', 'read'],
188 | ['data2_admin', 'data2', 'read'],
189 | ['data2_admin', 'data2', 'write']
190 | ];
191 |
192 | $this->arrayEqualsWithoutOrder($policies, Enforcer::getPolicy());
193 |
194 | // test use updateFilteredPolicies to update all policies of a user
195 | $this->initTable();
196 | Enforcer::loadPolicy();
197 | $policies = [
198 | ['alice', 'data2', 'write'],
199 | ['bob', 'data1', 'read']
200 | ];
201 | Enforcer::addPolicies($policies);
202 |
203 | $this->arrayEqualsWithoutOrder([
204 | ['alice', 'data1', 'read'],
205 | ['bob', 'data2', 'write'],
206 | ['data2_admin', 'data2', 'read'],
207 | ['data2_admin', 'data2', 'write'],
208 | ['alice', 'data2', 'write'],
209 | ['bob', 'data1', 'read']
210 | ], Enforcer::getPolicy());
211 |
212 | Enforcer::updateFilteredPolicies([['alice', 'data1', 'write'], ['alice', 'data2', 'read']], 0, 'alice');
213 | Enforcer::updateFilteredPolicies([['bob', 'data1', 'write'], ["bob", "data2", "read"]], 0, 'bob');
214 |
215 | $policies = [
216 | ['alice', 'data1', 'write'],
217 | ['alice', 'data2', 'read'],
218 | ['bob', 'data1', 'write'],
219 | ['bob', 'data2', 'read'],
220 | ['data2_admin', 'data2', 'read'],
221 | ['data2_admin', 'data2', 'write']
222 | ];
223 |
224 | $this->arrayEqualsWithoutOrder($policies, Enforcer::getPolicy());
225 |
226 | // test if $fieldValues contains empty string
227 | $this->initTable();
228 | Enforcer::loadPolicy();
229 | $policies = [
230 | ['alice', 'data2', 'write'],
231 | ['bob', 'data1', 'read']
232 | ];
233 | Enforcer::addPolicies($policies);
234 |
235 | $this->assertEquals([
236 | ['alice', 'data1', 'read'],
237 | ['bob', 'data2', 'write'],
238 | ['data2_admin', 'data2', 'read'],
239 | ['data2_admin', 'data2', 'write'],
240 | ['alice', 'data2', 'write'],
241 | ['bob', 'data1', 'read']
242 | ], Enforcer::getPolicy());
243 |
244 | Enforcer::updateFilteredPolicies([['alice', 'data1', 'write'], ['alice', 'data2', 'read']], 0, 'alice', '', '');
245 | Enforcer::updateFilteredPolicies([['bob', 'data1', 'write'], ["bob", "data2", "read"]], 0, 'bob', '', '');
246 |
247 | $policies = [
248 | ['alice', 'data1', 'write'],
249 | ['alice', 'data2', 'read'],
250 | ['bob', 'data1', 'write'],
251 | ['bob', 'data2', 'read'],
252 | ['data2_admin', 'data2', 'read'],
253 | ['data2_admin', 'data2', 'write']
254 | ];
255 |
256 | $this->arrayEqualsWithoutOrder($policies, Enforcer::getPolicy());
257 |
258 | // test if $fieldIndex is not zero
259 | $this->initTable();
260 | Enforcer::loadPolicy();
261 | $policies = [
262 | ['alice', 'data2', 'write'],
263 | ['bob', 'data1', 'read']
264 | ];
265 | Enforcer::addPolicies($policies);
266 |
267 | $this->assertEquals([
268 | ['alice', 'data1', 'read'],
269 | ['bob', 'data2', 'write'],
270 | ['data2_admin', 'data2', 'read'],
271 | ['data2_admin', 'data2', 'write'],
272 | ['alice', 'data2', 'write'],
273 | ['bob', 'data1', 'read']
274 | ], Enforcer::getPolicy());
275 |
276 | Enforcer::updateFilteredPolicies([['alice', 'data1', 'write'], ['bob', 'data1', 'write']], 2, 'read');
277 | Enforcer::updateFilteredPolicies([['alice', 'data2', 'read'], ["bob", "data2", "read"]], 2, 'write');
278 |
279 | $policies = [
280 | ['alice', 'data2', 'read'],
281 | ['bob', 'data2', 'read'],
282 | ];
283 |
284 | $this->arrayEqualsWithoutOrder($policies, Enforcer::getPolicy());
285 | }
286 |
287 | public function testLoadFilteredPolicy()
288 | {
289 | $this->initTable();
290 | Enforcer::clearPolicy();
291 | $this->initConfig();
292 | $adapter = Enforcer::getAdapter();
293 | $adapter->setFiltered(true);
294 | $this->assertEquals([], Enforcer::getPolicy());
295 |
296 | // invalid filter type
297 | try {
298 | $filter = ['alice', 'data1', 'read'];
299 | Enforcer::loadFilteredPolicy($filter);
300 | $e = InvalidFilterTypeException::class;
301 | $this->fail("Expected exception $e not thrown");
302 | } catch (InvalidFilterTypeException $e) {
303 | $this->assertEquals("invalid filter type", $e->getMessage());
304 | }
305 |
306 | // string
307 | $filter = "v0 = 'bob'";
308 | Enforcer::loadFilteredPolicy($filter);
309 | $this->assertEquals([
310 | ['bob', 'data2', 'write']
311 | ], Enforcer::getPolicy());
312 |
313 | // Filter
314 | $filter = new Filter(['v2'], ['read']);
315 | Enforcer::loadFilteredPolicy($filter);
316 | $this->assertEquals([
317 | ['alice', 'data1', 'read'],
318 | ['data2_admin', 'data2', 'read'],
319 | ], Enforcer::getPolicy());
320 |
321 | // Closure
322 | Enforcer::loadFilteredPolicy(function ($query) {
323 | $query->where('v1', 'data1');
324 | });
325 |
326 | $this->assertEquals([
327 | ['alice', 'data1', 'read'],
328 | ], Enforcer::getPolicy());
329 | }
330 | }
331 |
--------------------------------------------------------------------------------