9
9
use Casbin \Persist \FilteredAdapter ;
10
10
use Casbin \Persist \Adapters \Filter ;
11
11
use Casbin \Exceptions \InvalidFilterTypeException ;
12
+ use Closure ;
12
13
13
14
/**
14
15
* DatabaseAdapter.
@@ -27,6 +28,8 @@ class Adapter implements AdapterContract, FilteredAdapter
27
28
28
29
public $ casbinRuleTableName = 'casbin_rule ' ;
29
30
31
+ public $ rows = [];
32
+
30
33
public function __construct (array $ config )
31
34
{
32
35
$ this ->config = $ config ;
@@ -45,6 +48,16 @@ public function isFiltered(): bool
45
48
return $ this ->filtered ;
46
49
}
47
50
51
+ /**
52
+ * Sets filtered parameter.
53
+ *
54
+ * @param bool $filtered
55
+ */
56
+ public function setFiltered (bool $ filtered ): void
57
+ {
58
+ $ this ->filtered = $ filtered ;
59
+ }
60
+
48
61
public static function newAdapter (array $ config )
49
62
{
50
63
return new static ($ config );
@@ -90,55 +103,6 @@ public function loadPolicy(Model $model): void
90
103
}
91
104
}
92
105
93
- /**
94
- * Loads only policy rules that match the filter from storage.
95
- *
96
- * @param Model $model
97
- * @param mixed $filter
98
- *
99
- * @throws CasbinException
100
- */
101
- public function loadFilteredPolicy (Model $ model , $ filter ): void
102
- {
103
- // if $filter is empty, load all policies
104
- if (is_null ($ filter )) {
105
- $ this ->loadPolicy ($ model );
106
- return ;
107
- }
108
- // validate $filter is a instance of Filter
109
- if (!$ filter instanceof Filter) {
110
- throw new InvalidFilterTypeException ('invalid filter type ' );
111
- }
112
- $ type = '' ;
113
- $ filter = (array ) $ filter ;
114
- // choose which ptype to use
115
- foreach ($ filter as $ i => $ v ) {
116
- if (!empty ($ v )) {
117
- array_unshift ($ filter [$ i ], $ i );
118
- $ type = $ i ;
119
- break ;
120
- }
121
- }
122
- $ sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->casbinRuleTableName . ' WHERE ' ;
123
- $ items = ['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ];
124
- $ temp = [];
125
- $ values = [];
126
- foreach ($ items as $ i => $ item ) {
127
- if (isset ($ filter [$ type ][$ i ]) && !empty ($ filter [$ type ][$ i ])) {
128
- array_push ($ temp , $ item . '=: ' . $ item );
129
- $ values [$ item ] = $ filter [$ type ][$ i ];
130
- }
131
- }
132
- $ sql .= implode (' and ' , $ temp );
133
- $ rows = $ this ->connection ->query ($ sql , $ values );
134
- foreach ($ rows as $ row ) {
135
- $ line = implode (', ' , $ row );
136
- $ this ->loadPolicyLine ($ line , $ model );
137
- }
138
-
139
- $ this ->filtered = true ;
140
- }
141
-
142
106
/**
143
107
* saves all policy rules to the storage.
144
108
*
@@ -219,4 +183,61 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
219
183
220
184
$ this ->connection ->execute ($ sql , $ where );
221
185
}
186
+
187
+ /**
188
+ * Loads only policy rules that match the filter from storage.
189
+ *
190
+ * @param Model $model
191
+ * @param mixed $filter
192
+ *
193
+ * @throws CasbinException
194
+ */
195
+ public function loadFilteredPolicy (Model $ model , $ filter ): void
196
+ {
197
+ // if $filter is empty, load all policies
198
+ if (is_null ($ filter )) {
199
+ $ this ->loadPolicy ($ model );
200
+ return ;
201
+ }
202
+ // the basic sql
203
+ $ sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->casbinRuleTableName . ' WHERE ' ;
204
+
205
+ if ($ filter instanceof Filter) {
206
+ $ type = '' ;
207
+ $ filter = (array ) $ filter ;
208
+ // choose which ptype to use
209
+ foreach ($ filter as $ i => $ v ) {
210
+ if (!empty ($ v )) {
211
+ array_unshift ($ filter [$ i ], $ i );
212
+ $ type = $ i ;
213
+ break ;
214
+ }
215
+ }
216
+ $ items = ['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ];
217
+ $ temp = [];
218
+ $ values = [];
219
+ foreach ($ items as $ i => $ item ) {
220
+ if (isset ($ filter [$ type ][$ i ]) && !empty ($ filter [$ type ][$ i ])) {
221
+ array_push ($ temp , $ item . '=: ' . $ item );
222
+ $ values [$ item ] = $ filter [$ type ][$ i ];
223
+ }
224
+ }
225
+ $ sql .= implode (' and ' , $ temp );
226
+ $ rows = $ this ->connection ->query ($ sql , $ values );
227
+ } elseif (is_string ($ filter )) {
228
+ $ sql .= $ filter ;
229
+ $ rows = $ this ->connection ->query ($ sql );
230
+ } else if ($ filter instanceof Closure) {
231
+ $ filter ($ this ->connection , $ sql , $ this ->rows );
232
+ } else {
233
+ throw new InvalidFilterTypeException ('invalid filter type ' );
234
+ }
235
+
236
+ $ rows = $ rows ?? $ this ->rows ;
237
+ foreach ($ rows as $ row ) {
238
+ $ line = implode (', ' , $ row );
239
+ $ this ->loadPolicyLine ($ line , $ model );
240
+ }
241
+ $ this ->filtered = true ;
242
+ }
222
243
}
0 commit comments