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