19
19
use Casbin \Persist \Adapters \Filter ;
20
20
use Casbin \Exceptions \InvalidFilterTypeException ;
21
21
use Casbin \WebmanPermission \Model \LaravelRuleModel ;
22
+ use Illuminate \Database \Eloquent \Builder ;
23
+ use Illuminate \Database \Eloquent \Collection ;
22
24
use Illuminate \Support \Facades \DB ;
23
25
use Throwable ;
24
26
@@ -45,11 +47,12 @@ class LaravelDatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter,
45
47
46
48
/**
47
49
* LaravelDatabaseAdapter constructor.
48
- * @param LaravelRuleModel $model
50
+ *
51
+ * @param string|null $driver
49
52
*/
50
- public function __construct (LaravelRuleModel $ model )
53
+ public function __construct (? string $ driver = null )
51
54
{
52
- $ this ->model = $ model ;
55
+ $ this ->model = new LaravelRuleModel ([], $ driver ) ;
53
56
}
54
57
55
58
/**
@@ -84,9 +87,9 @@ public function savePolicyLine(string $ptype, array $rule)
84
87
{
85
88
$ col ['ptype ' ] = $ ptype ;
86
89
foreach ($ rule as $ key => $ value ) {
87
- $ col ['v ' . strval ( $ key) . '' ] = $ value ;
90
+ $ col ['v ' . $ key ] = $ value ;
88
91
}
89
- $ this ->model ->create ($ col );
92
+ $ this ->model ->updateOrCreate ($ col );
90
93
}
91
94
92
95
/**
@@ -96,7 +99,7 @@ public function savePolicyLine(string $ptype, array $rule)
96
99
*/
97
100
public function loadPolicy (Model $ model ): void
98
101
{
99
- $ rows = $ this ->model ->getAllFromCache () ;
102
+ $ rows = $ this ->model ->select ([ ' ptype ' , ' v0 ' , ' v1 ' , ' v2 ' , ' v3 ' , ' v4 ' , ' v5 ' ])-> get ()-> toArray (); ;
100
103
foreach ($ rows as $ row ) {
101
104
$ this ->loadPolicyArray ($ this ->filterRule ($ row ), $ model );
102
105
}
@@ -145,21 +148,13 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
145
148
*/
146
149
public function addPolicies (string $ sec , string $ ptype , array $ rules ): void
147
150
{
148
- $ cols = [];
149
- $ i = 0 ;
150
-
151
151
foreach ($ rules as $ rule ) {
152
- $ temp ['ptype ' ] = $ ptype ;
153
- $ temp ['created_at ' ] = date ("Y-m-d h:m:i " );
154
- $ temp ['updated_at ' ] = $ temp ['created_at ' ];
152
+ $ temp = ['ptype ' => $ ptype ];
155
153
foreach ($ rule as $ key => $ value ) {
156
- $ temp ['v ' . strval ( $ key) ] = $ value ;
154
+ $ temp ['v ' . $ key ] = $ value ;
157
155
}
158
- $ cols [$ i ++] = $ temp ?? [];
159
- $ temp = [];
156
+ $ this ->model ->updateOrCreate ($ temp );
160
157
}
161
- $ this ->model ->insert ($ cols );
162
- LaravelRuleModel::fireModelEvent ('saved ' );
163
158
}
164
159
165
160
/**
@@ -173,10 +168,12 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
173
168
{
174
169
$ instance = $ this ->model ->where ('ptype ' , $ ptype );
175
170
foreach ($ rule as $ key => $ value ) {
176
- $ instance ->where ('v ' . strval ($ key ), $ value );
171
+ $ instance ->where ('v ' . $ key , $ value );
172
+ }
173
+ $ data = $ instance ->get ();
174
+ foreach ($ data as $ item ) {
175
+ $ item ->delete ();
177
176
}
178
- $ instance ->delete ();
179
- LaravelRuleModel::fireModelEvent ('deleted ' );
180
177
}
181
178
182
179
/**
@@ -189,25 +186,13 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
189
186
*/
190
187
public function _removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , ?string ...$ fieldValues ): array
191
188
{
192
- $ count = 0 ;
193
189
$ removedRules = [];
190
+ $ data = $ this ->getCollection ($ ptype , $ fieldIndex , $ fieldValues );
194
191
195
- $ instance = $ this ->model ->where ('ptype ' , $ ptype );
196
- foreach (range (0 , 5 ) as $ value ) {
197
- if ($ fieldIndex <= $ value && $ value < $ fieldIndex + count ($ fieldValues )) {
198
- if ('' != $ fieldValues [$ value - $ fieldIndex ]) {
199
- $ instance ->where ('v ' . strval ($ value ), $ fieldValues [$ value - $ fieldIndex ]);
200
- }
201
- }
202
- }
203
-
204
- foreach ($ instance ->select () as $ model ) {
205
- $ item = $ model ->hidden (['id ' , 'ptype ' ])->toArray ();
206
- $ item = $ this ->filterRule ($ item );
192
+ foreach ($ data as $ model ) {
193
+ $ item = $ model ->hidden (['id ' , 'ptype ' ])->toArray ();
194
+ $ item = $ this ->filterRule ($ item );
207
195
$ removedRules [] = $ item ;
208
- if ($ model ->cache ('tauthz ' )->delete ()) {
209
- ++$ count ;
210
- }
211
196
}
212
197
213
198
return $ removedRules ;
@@ -241,22 +226,10 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
241
226
*/
242
227
public function removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , string ...$ fieldValues ): void
243
228
{
244
- $ instance = $ this ->model ->where ('ptype ' , $ ptype );
245
- foreach (range (0 , 5 ) as $ value ) {
246
- if ($ fieldIndex <= $ value && $ value < $ fieldIndex + count ($ fieldValues )) {
247
- if ('' != $ fieldValues [$ value - $ fieldIndex ]) {
248
- $ instance ->where ('v ' . strval ($ value ), $ fieldValues [$ value - $ fieldIndex ]);
249
- }
250
- }
251
- }
252
-
253
- $ oldP = $ instance ->get ()->makeHidden (['created_at ' ,'updated_at ' , 'id ' , 'ptype ' ])->toArray ();
254
- foreach ($ oldP as &$ item ) {
255
- $ item = $ this ->filterRule ($ item );
256
- $ removedRules [] = $ item ;
229
+ $ data = $ this ->getCollection ($ ptype , $ fieldIndex , $ fieldValues );
230
+ foreach ($ data as $ item ) {
231
+ $ item ->delete ();
257
232
}
258
- $ instance ->delete ();
259
- LaravelRuleModel::fireModelEvent ('deleted ' );
260
233
}
261
234
262
235
/**
@@ -272,7 +245,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
272
245
{
273
246
$ instance = $ this ->model ->where ('ptype ' , $ ptype );
274
247
foreach ($ oldRule as $ key => $ value ) {
275
- $ instance ->where ('v ' . strval ( $ key) , $ value );
248
+ $ instance ->where ('v ' . $ key , $ value );
276
249
}
277
250
$ instance = $ instance ->first ();
278
251
@@ -281,8 +254,8 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
281
254
$ update ['v ' . $ key ] = $ value ;
282
255
}
283
256
284
- $ instance ->update ($ update );
285
- LaravelRuleModel:: fireModelEvent ( ' saved ' );
257
+ $ instance ->fill ($ update );
258
+ $ instance -> save ( );
286
259
}
287
260
288
261
/**
@@ -316,7 +289,7 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
316
289
public function updateFilteredPolicies (string $ sec , string $ ptype , array $ newPolicies , int $ fieldIndex , string ...$ fieldValues ): array
317
290
{
318
291
$ oldRules = [];
319
- \ Illuminate \ Support \ Facades \ DB ::transaction (function () use ($ sec , $ ptype , $ fieldIndex , $ fieldValues , $ newPolicies , &$ oldRules ) {
292
+ DB ::transaction (function () use ($ sec , $ ptype , $ fieldIndex , $ fieldValues , $ newPolicies , &$ oldRules ) {
320
293
$ oldRules = $ this ->_removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues );
321
294
$ this ->addPolicies ($ sec , $ ptype , $ newPolicies );
322
295
});
@@ -346,34 +319,67 @@ public function setFiltered(bool $filtered): void
346
319
/**
347
320
* Loads only policy rules that match the filter.
348
321
*
349
- * @param Model $model
350
- * @param mixed $filter
322
+ * @param Model $model
323
+ * @param mixed $filter
324
+ *
325
+ * @throws InvalidFilterTypeException
351
326
*/
352
327
public function loadFilteredPolicy (Model $ model , $ filter ): void
353
328
{
354
329
$ instance = $ this ->model ;
355
330
if (is_string ($ filter )) {
356
- $ instance = $ instance ->whereRaw ($ filter );
357
- } elseif ($ filter instanceof Filter) {
331
+ $ instance ->whereRaw ($ filter );
332
+ }
333
+ elseif ($ filter instanceof Filter) {
334
+ $ where = [];
358
335
foreach ($ filter ->p as $ k => $ v ) {
359
336
$ where [$ v ] = $ filter ->g [$ k ];
360
- $ instance = $ instance ->where ($ v , $ filter ->g [$ k ]);
361
337
}
362
- } elseif ($ filter instanceof \Closure) {
338
+ $ instance ->where ($ where );
339
+ }
340
+ elseif ($ filter instanceof Closure) {
363
341
$ instance = $ instance ->where ($ filter );
364
- } else {
342
+ }
343
+ else {
365
344
throw new InvalidFilterTypeException ('invalid filter type ' );
366
345
}
367
- $ rows = $ instance ->get ()->makeHidden (['created_at ' ,'updated_at ' , 'id ' ])->toArray ();
346
+ $ rows = $ instance ->get ()->makeHidden (['created_at ' , 'updated_at ' , 'id ' ])->toArray ();
368
347
if ($ rows ) {
369
348
foreach ($ rows as $ row ) {
370
- $ row = array_filter ($ row , function ($ value ) { return !is_null ($ value ) && $ value !== '' ; });
371
- $ line = implode (', ' , array_filter ($ row , function ($ val ) {
372
- return '' != $ val && !is_null ($ val );
373
- }));
349
+ $ row = array_filter ($ row , function ($ value ) {
350
+ return !is_null ($ value ) && $ value !== '' ;
351
+ });
352
+ $ line = implode (
353
+ ', ' ,
354
+ array_filter ($ row , function ($ val ) {
355
+ return '' != $ val && !is_null ($ val );
356
+ })
357
+ );
374
358
$ this ->loadPolicyLine (trim ($ line ), $ model );
375
359
}
376
360
}
377
361
$ this ->setFiltered (true );
378
362
}
363
+
364
+ /**
365
+ * @param string $ptype
366
+ * @param int $fieldIndex
367
+ * @param array $fieldValues
368
+ *
369
+ * @return Builder[]|Collection
370
+ */
371
+ protected function getCollection (string $ ptype , int $ fieldIndex , array $ fieldValues ) {
372
+ $ where = [
373
+ 'ptype ' => $ ptype ,
374
+ ];
375
+ foreach (range (0 , 5 ) as $ value ) {
376
+ if ($ fieldIndex <= $ value && $ value < $ fieldIndex + count ($ fieldValues )) {
377
+ if ('' != $ fieldValues [$ value - $ fieldIndex ]) {
378
+ $ where ['v ' . $ value ] = $ fieldValues [$ value - $ fieldIndex ];
379
+ }
380
+ }
381
+ }
382
+
383
+ return $ this ->model ->where ($ where )->get ();
384
+ }
379
385
}
0 commit comments