1919use Casbin \Persist \Adapters \Filter ;
2020use Casbin \Exceptions \InvalidFilterTypeException ;
2121use Casbin \WebmanPermission \Model \LaravelRuleModel ;
22+ use Illuminate \Database \Eloquent \Builder ;
23+ use Illuminate \Database \Eloquent \Collection ;
2224use Illuminate \Support \Facades \DB ;
2325use Throwable ;
2426
@@ -45,11 +47,12 @@ class LaravelDatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter,
4547
4648 /**
4749 * LaravelDatabaseAdapter constructor.
48- * @param LaravelRuleModel $model
50+ *
51+ * @param string|null $driver
4952 */
50- public function __construct (LaravelRuleModel $ model )
53+ public function __construct (? string $ driver = null )
5154 {
52- $ this ->model = $ model ;
55+ $ this ->model = new LaravelRuleModel ([], $ driver ) ;
5356 }
5457
5558 /**
@@ -84,9 +87,9 @@ public function savePolicyLine(string $ptype, array $rule)
8487 {
8588 $ col ['ptype ' ] = $ ptype ;
8689 foreach ($ rule as $ key => $ value ) {
87- $ col ['v ' . strval ( $ key) . '' ] = $ value ;
90+ $ col ['v ' . $ key ] = $ value ;
8891 }
89- $ this ->model ->create ($ col );
92+ $ this ->model ->updateOrCreate ($ col );
9093 }
9194
9295 /**
@@ -96,7 +99,7 @@ public function savePolicyLine(string $ptype, array $rule)
9699 */
97100 public function loadPolicy (Model $ model ): void
98101 {
99- $ rows = $ this ->model ->getAllFromCache () ;
102+ $ rows = $ this ->model ->select ([ ' ptype ' , ' v0 ' , ' v1 ' , ' v2 ' , ' v3 ' , ' v4 ' , ' v5 ' ])-> get ()-> toArray (); ;
100103 foreach ($ rows as $ row ) {
101104 $ this ->loadPolicyArray ($ this ->filterRule ($ row ), $ model );
102105 }
@@ -145,21 +148,13 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
145148 */
146149 public function addPolicies (string $ sec , string $ ptype , array $ rules ): void
147150 {
148- $ cols = [];
149- $ i = 0 ;
150-
151151 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 ];
155153 foreach ($ rule as $ key => $ value ) {
156- $ temp ['v ' . strval ( $ key) ] = $ value ;
154+ $ temp ['v ' . $ key ] = $ value ;
157155 }
158- $ cols [$ i ++] = $ temp ?? [];
159- $ temp = [];
156+ $ this ->model ->updateOrCreate ($ temp );
160157 }
161- $ this ->model ->insert ($ cols );
162- LaravelRuleModel::fireModelEvent ('saved ' );
163158 }
164159
165160 /**
@@ -173,10 +168,12 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
173168 {
174169 $ instance = $ this ->model ->where ('ptype ' , $ ptype );
175170 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 ();
177176 }
178- $ instance ->delete ();
179- LaravelRuleModel::fireModelEvent ('deleted ' );
180177 }
181178
182179 /**
@@ -189,25 +186,13 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
189186 */
190187 public function _removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , ?string ...$ fieldValues ): array
191188 {
192- $ count = 0 ;
193189 $ removedRules = [];
190+ $ data = $ this ->getCollection ($ ptype , $ fieldIndex , $ fieldValues );
194191
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 );
207195 $ removedRules [] = $ item ;
208- if ($ model ->cache ('tauthz ' )->delete ()) {
209- ++$ count ;
210- }
211196 }
212197
213198 return $ removedRules ;
@@ -241,22 +226,10 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
241226 */
242227 public function removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , string ...$ fieldValues ): void
243228 {
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 ();
257232 }
258- $ instance ->delete ();
259- LaravelRuleModel::fireModelEvent ('deleted ' );
260233 }
261234
262235 /**
@@ -272,7 +245,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
272245 {
273246 $ instance = $ this ->model ->where ('ptype ' , $ ptype );
274247 foreach ($ oldRule as $ key => $ value ) {
275- $ instance ->where ('v ' . strval ( $ key) , $ value );
248+ $ instance ->where ('v ' . $ key , $ value );
276249 }
277250 $ instance = $ instance ->first ();
278251
@@ -281,8 +254,8 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
281254 $ update ['v ' . $ key ] = $ value ;
282255 }
283256
284- $ instance ->update ($ update );
285- LaravelRuleModel:: fireModelEvent ( ' saved ' );
257+ $ instance ->fill ($ update );
258+ $ instance -> save ( );
286259 }
287260
288261 /**
@@ -316,7 +289,7 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
316289 public function updateFilteredPolicies (string $ sec , string $ ptype , array $ newPolicies , int $ fieldIndex , string ...$ fieldValues ): array
317290 {
318291 $ 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 ) {
320293 $ oldRules = $ this ->_removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues );
321294 $ this ->addPolicies ($ sec , $ ptype , $ newPolicies );
322295 });
@@ -346,34 +319,67 @@ public function setFiltered(bool $filtered): void
346319 /**
347320 * Loads only policy rules that match the filter.
348321 *
349- * @param Model $model
350- * @param mixed $filter
322+ * @param Model $model
323+ * @param mixed $filter
324+ *
325+ * @throws InvalidFilterTypeException
351326 */
352327 public function loadFilteredPolicy (Model $ model , $ filter ): void
353328 {
354329 $ instance = $ this ->model ;
355330 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 = [];
358335 foreach ($ filter ->p as $ k => $ v ) {
359336 $ where [$ v ] = $ filter ->g [$ k ];
360- $ instance = $ instance ->where ($ v , $ filter ->g [$ k ]);
361337 }
362- } elseif ($ filter instanceof \Closure) {
338+ $ instance ->where ($ where );
339+ }
340+ elseif ($ filter instanceof Closure) {
363341 $ instance = $ instance ->where ($ filter );
364- } else {
342+ }
343+ else {
365344 throw new InvalidFilterTypeException ('invalid filter type ' );
366345 }
367- $ rows = $ instance ->get ()->makeHidden (['created_at ' ,'updated_at ' , 'id ' ])->toArray ();
346+ $ rows = $ instance ->get ()->makeHidden (['created_at ' , 'updated_at ' , 'id ' ])->toArray ();
368347 if ($ rows ) {
369348 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+ );
374358 $ this ->loadPolicyLine (trim ($ line ), $ model );
375359 }
376360 }
377361 $ this ->setFiltered (true );
378362 }
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+ }
379385}
0 commit comments