66
77use Lauthz \Models \Rule ;
88use Lauthz \Contracts \DatabaseAdapter as DatabaseAdapterContract ;
9+ use Lauthz \Contracts \BatchDatabaseAdapter as BatchDatabaseAdapterContract ;
910use Casbin \Model \Model ;
1011use Casbin \Persist \AdapterHelper ;
11-
12+ use DateTime ;
1213/**
1314 * DatabaseAdapter.
1415 *
15161617 */
17- class DatabaseAdapter implements DatabaseAdapterContract
18+ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterContract
1819{
1920 use AdapterHelper;
2021
@@ -101,6 +102,32 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
101102 $ this ->savePolicyLine ($ ptype , $ rule );
102103 }
103104
105+ /**
106+ * Adds a policy rules to the storage.
107+ * This is part of the Auto-Save feature.
108+ *
109+ * @param string $sec
110+ * @param string $ptype
111+ * @param string[][] $rules
112+ */
113+ public function addPolicies (string $ sec , string $ ptype , array $ rules ): void
114+ {
115+ $ cols = [];
116+ $ i = 0 ;
117+
118+ foreach ($ rules as $ rule ) {
119+ $ temp ['p_type ' ] = $ ptype ;
120+ $ temp ['created_at ' ] = new DateTime ();
121+ $ temp ['updated_at ' ] = $ temp ['created_at ' ];
122+ foreach ($ rule as $ key => $ value ) {
123+ $ temp ['v ' .strval ($ key )] = $ value ;
124+ }
125+ $ cols [$ i ++] = $ temp ?? [];
126+ $ temp = [];
127+ }
128+ $ this ->eloquent ->insert ($ cols );
129+ }
130+
104131 /**
105132 * This is part of the Auto-Save feature.
106133 *
@@ -125,6 +152,33 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
125152 }
126153 }
127154
155+ /**
156+ * Removes policy rules from the storage.
157+ * This is part of the Auto-Save feature.
158+ *
159+ * @param string $sec
160+ * @param string $ptype
161+ * @param string[][] $rules
162+ */
163+ public function removePolicies (string $ sec , string $ ptype , array $ rules ): void
164+ {
165+ $ count = 0 ;
166+ $ instance = $ this ->eloquent ->where ('p_type ' , $ ptype );
167+ foreach ($ rules as $ rule )
168+ {
169+ foreach ($ rule as $ key => $ value ) {
170+ $ keys [] = 'v ' .strval ($ key );
171+ $ con ['v ' .strval ($ key )][] = $ value ;
172+ }
173+ }
174+ $ keys = array_unique ($ keys );
175+ foreach ($ keys as $ key ){
176+ $ instance ->whereIn ($ key , $ con [$ key ]);
177+ }
178+ $ num = $ instance ->delete ();
179+ $ count += $ num ;
180+ }
181+
128182 /**
129183 * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
130184 * This is part of the Auto-Save feature.
0 commit comments