Skip to content

Commit 6e69d2b

Browse files
committed
Support for multiple enforcers.
1 parent 4e53d87 commit 6e69d2b

File tree

6 files changed

+350
-249
lines changed

6 files changed

+350
-249
lines changed

README.md

100644100755
File mode changed.

composer.json

100644100755
File mode changed.

src/Adapters/DatabaseAdapter.php

100644100755
Lines changed: 169 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,169 @@
1-
<?php
2-
3-
namespace Lauthz\Adapters;
4-
5-
use Lauthz\Models\Rule;
6-
use Lauthz\Contracts\DatabaseAdapter as DatabaseAdapterContract;
7-
use Casbin\Persist\AdapterHelper;
8-
9-
/**
10-
* DatabaseAdapter.
11-
*
12-
13-
*/
14-
class DatabaseAdapter implements DatabaseAdapterContract
15-
{
16-
use AdapterHelper;
17-
18-
/**
19-
* Rules eloquent model.
20-
*
21-
* @var Rule
22-
*/
23-
protected $eloquent;
24-
25-
/**
26-
* the DatabaseAdapter constructor.
27-
*
28-
* @param Rule $eloquent
29-
*/
30-
public function __construct(Rule $eloquent)
31-
{
32-
$this->eloquent = $eloquent;
33-
}
34-
35-
/**
36-
* savePolicyLine function.
37-
*
38-
* @param string $ptype
39-
* @param array $rule
40-
*
41-
* @return void
42-
*/
43-
public function savePolicyLine($ptype, array $rule)
44-
{
45-
$col['ptype'] = $ptype;
46-
foreach ($rule as $key => $value) {
47-
$col['v'.strval($key)] = $value;
48-
}
49-
50-
$this->eloquent->create($col);
51-
}
52-
53-
/**
54-
* loads all policy rules from the storage.
55-
*
56-
* @param Model $model
57-
*
58-
* @return mixed
59-
*/
60-
public function loadPolicy($model)
61-
{
62-
$rows = $this->eloquent->getAllFromCache();
63-
64-
foreach ($rows as $row) {
65-
$line = implode(', ', array_filter($row, function ($val) {
66-
return '' != $val && !is_null($val);
67-
}));
68-
$this->loadPolicyLine(trim($line), $model);
69-
}
70-
}
71-
72-
/**
73-
* saves all policy rules to the storage.
74-
*
75-
* @param Model $model
76-
*
77-
* @return bool
78-
*/
79-
public function savePolicy($model)
80-
{
81-
foreach ($model->model['p'] as $ptype => $ast) {
82-
foreach ($ast->policy as $rule) {
83-
$this->savePolicyLine($ptype, $rule);
84-
}
85-
}
86-
87-
foreach ($model->model['g'] as $ptype => $ast) {
88-
foreach ($ast->policy as $rule) {
89-
$this->savePolicyLine($ptype, $rule);
90-
}
91-
}
92-
93-
return true;
94-
}
95-
96-
/**
97-
* Adds a policy rule to the storage.
98-
* This is part of the Auto-Save feature.
99-
*
100-
* @param string $sec
101-
* @param string $ptype
102-
* @param array $rule
103-
*
104-
* @return mixed
105-
*/
106-
public function addPolicy($sec, $ptype, $rule)
107-
{
108-
return $this->savePolicyLine($ptype, $rule);
109-
}
110-
111-
/**
112-
* This is part of the Auto-Save feature.
113-
*
114-
* @param string $sec
115-
* @param string $ptype
116-
* @param array $rule
117-
*
118-
* @return mixed
119-
*/
120-
public function removePolicy($sec, $ptype, $rule)
121-
{
122-
$count = 0;
123-
124-
$instance = $this->eloquent->where('ptype', $ptype);
125-
126-
foreach ($rule as $key => $value) {
127-
$instance->where('v'.strval($key), $value);
128-
}
129-
130-
foreach ($instance->get() as $model) {
131-
if ($model->delete()) {
132-
++$count;
133-
}
134-
}
135-
136-
return $count;
137-
}
138-
139-
/**
140-
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
141-
* This is part of the Auto-Save feature.
142-
*
143-
* @param string $sec
144-
* @param string $ptype
145-
* @param int $fieldIndex
146-
* @param mixed ...$fieldValues
147-
*
148-
* @return mixed
149-
*/
150-
public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
151-
{
152-
$count = 0;
153-
154-
$instance = $this->eloquent->where('ptype', $ptype);
155-
foreach (range(0, 5) as $value) {
156-
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
157-
if ('' != $fieldValues[$value - $fieldIndex]) {
158-
$instance->where('v'.strval($value), $fieldValues[$value - $fieldIndex]);
159-
}
160-
}
161-
}
162-
163-
foreach ($instance->get() as $model) {
164-
if ($model->delete()) {
165-
++$count;
166-
}
167-
}
168-
169-
return $count;
170-
}
171-
}
1+
<?php
2+
3+
namespace Lauthz\Adapters;
4+
5+
use Lauthz\Models\Rule;
6+
use Lauthz\Contracts\DatabaseAdapter as DatabaseAdapterContract;
7+
use Casbin\Persist\AdapterHelper;
8+
9+
/**
10+
* DatabaseAdapter.
11+
*
12+
13+
*/
14+
class DatabaseAdapter implements DatabaseAdapterContract
15+
{
16+
use AdapterHelper;
17+
18+
/**
19+
* Rules eloquent model.
20+
*
21+
* @var Rule
22+
*/
23+
protected $eloquent;
24+
25+
/**
26+
* the DatabaseAdapter constructor.
27+
*
28+
* @param Rule $eloquent
29+
*/
30+
public function __construct(Rule $eloquent)
31+
{
32+
$this->eloquent = $eloquent;
33+
}
34+
35+
/**
36+
* savePolicyLine function.
37+
*
38+
* @param string $ptype
39+
* @param array $rule
40+
*/
41+
public function savePolicyLine($ptype, array $rule)
42+
{
43+
$col['ptype'] = $ptype;
44+
foreach ($rule as $key => $value) {
45+
$col['v'.strval($key)] = $value;
46+
}
47+
48+
$this->eloquent->create($col);
49+
}
50+
51+
/**
52+
* loads all policy rules from the storage.
53+
*
54+
* @param Model $model
55+
*
56+
* @return mixed
57+
*/
58+
public function loadPolicy($model)
59+
{
60+
$rows = $this->eloquent->getAllFromCache();
61+
62+
foreach ($rows as $row) {
63+
$line = implode(', ', array_filter($row, function ($val) {
64+
return '' != $val && !is_null($val);
65+
}));
66+
$this->loadPolicyLine(trim($line), $model);
67+
}
68+
}
69+
70+
/**
71+
* saves all policy rules to the storage.
72+
*
73+
* @param Model $model
74+
*
75+
* @return bool
76+
*/
77+
public function savePolicy($model)
78+
{
79+
foreach ($model->model['p'] as $ptype => $ast) {
80+
foreach ($ast->policy as $rule) {
81+
$this->savePolicyLine($ptype, $rule);
82+
}
83+
}
84+
85+
foreach ($model->model['g'] as $ptype => $ast) {
86+
foreach ($ast->policy as $rule) {
87+
$this->savePolicyLine($ptype, $rule);
88+
}
89+
}
90+
91+
return true;
92+
}
93+
94+
/**
95+
* Adds a policy rule to the storage.
96+
* This is part of the Auto-Save feature.
97+
*
98+
* @param string $sec
99+
* @param string $ptype
100+
* @param array $rule
101+
*
102+
* @return mixed
103+
*/
104+
public function addPolicy($sec, $ptype, $rule)
105+
{
106+
return $this->savePolicyLine($ptype, $rule);
107+
}
108+
109+
/**
110+
* This is part of the Auto-Save feature.
111+
*
112+
* @param string $sec
113+
* @param string $ptype
114+
* @param array $rule
115+
*
116+
* @return mixed
117+
*/
118+
public function removePolicy($sec, $ptype, $rule)
119+
{
120+
$count = 0;
121+
122+
$instance = $this->eloquent->where('ptype', $ptype);
123+
124+
foreach ($rule as $key => $value) {
125+
$instance->where('v'.strval($key), $value);
126+
}
127+
128+
foreach ($instance->get() as $model) {
129+
if ($model->delete()) {
130+
++$count;
131+
}
132+
}
133+
134+
return $count;
135+
}
136+
137+
/**
138+
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
139+
* This is part of the Auto-Save feature.
140+
*
141+
* @param string $sec
142+
* @param string $ptype
143+
* @param int $fieldIndex
144+
* @param mixed ...$fieldValues
145+
*
146+
* @return mixed
147+
*/
148+
public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
149+
{
150+
$count = 0;
151+
152+
$instance = $this->eloquent->where('ptype', $ptype);
153+
foreach (range(0, 5) as $value) {
154+
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
155+
if ('' != $fieldValues[$value - $fieldIndex]) {
156+
$instance->where('v'.strval($value), $fieldValues[$value - $fieldIndex]);
157+
}
158+
}
159+
}
160+
161+
foreach ($instance->get() as $model) {
162+
if ($model->delete()) {
163+
++$count;
164+
}
165+
}
166+
167+
return $count;
168+
}
169+
}

0 commit comments

Comments
 (0)