@@ -29,7 +29,7 @@ class Adapter implements AdapterContract, FilteredAdapterContract, BatchAdapterC
29
29
30
30
protected $ connection ;
31
31
32
- public $ casbinRuleTableName = 'casbin_rule ' ;
32
+ public $ policyTableName = 'casbin_rule ' ;
33
33
34
34
public $ rows = [];
35
35
@@ -38,6 +38,11 @@ public function __construct(array $config)
38
38
$ this ->config = $ config ;
39
39
$ this ->filtered = false ;
40
40
$ this ->connection = (new Manager ($ config ))->getConnection ();
41
+
42
+ if (isset ($ config ['policy_table_name ' ]) && !is_null ($ config ['policy_table_name ' ])) {
43
+ $ this ->policyTableName = $ config ['policy_table_name ' ];
44
+ }
45
+
41
46
$ this ->initTable ();
42
47
}
43
48
@@ -89,7 +94,7 @@ public static function newAdapter(array $config)
89
94
public function initTable ()
90
95
{
91
96
$ sql = file_get_contents (__DIR__ .'/../migrations/ ' .$ this ->config ['type ' ].'.sql ' );
92
- $ sql = str_replace ('%table_name% ' , $ this ->casbinRuleTableName , $ sql );
97
+ $ sql = str_replace ('%table_name% ' , $ this ->policyTableName , $ sql );
93
98
$ this ->connection ->execute ($ sql , []);
94
99
}
95
100
@@ -104,7 +109,7 @@ public function savePolicyLine($ptype, array $rule)
104
109
105
110
$ name = rtrim (str_repeat ('?, ' , count ($ col )), ', ' );
106
111
107
- $ sql = 'INSERT INTO ' .$ this ->casbinRuleTableName .'( ' .$ colStr .') VALUES ( ' .$ name .') ' ;
112
+ $ sql = 'INSERT INTO ' .$ this ->policyTableName .'( ' .$ colStr .') VALUES ( ' .$ name .') ' ;
108
113
109
114
$ this ->connection ->execute ($ sql , array_values ($ col ));
110
115
}
@@ -116,7 +121,7 @@ public function savePolicyLine($ptype, array $rule)
116
121
*/
117
122
public function loadPolicy (Model $ model ): void
118
123
{
119
- $ rows = $ this ->connection ->query ('SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->casbinRuleTableName .'' );
124
+ $ rows = $ this ->connection ->query ('SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->policyTableName .'' );
120
125
121
126
foreach ($ rows as $ row ) {
122
127
$ this ->loadPolicyArray ($ this ->filterRule ($ row ), $ model );
@@ -158,7 +163,7 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
158
163
159
164
public function addPolicies (string $ sec , string $ ptype , array $ rules ): void
160
165
{
161
- $ table = $ this ->casbinRuleTableName ;
166
+ $ table = $ this ->policyTableName ;
162
167
$ columns = ['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ];
163
168
$ values = [];
164
169
$ sets = [];
@@ -206,7 +211,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
206
211
$ condition [] = 'v ' .strval ($ key ).' = : ' .'v ' .strval ($ key );
207
212
}
208
213
209
- $ sql = 'DELETE FROM ' .$ this ->casbinRuleTableName .' WHERE ' .implode (' AND ' , $ condition );
214
+ $ sql = 'DELETE FROM ' .$ this ->policyTableName .' WHERE ' .implode (' AND ' , $ condition );
210
215
211
216
$ this ->connection ->execute ($ sql , $ where );
212
217
}
@@ -233,9 +238,9 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
233
238
}
234
239
}
235
240
236
- $ deleteSql = "DELETE FROM {$ this ->casbinRuleTableName } WHERE " . implode (' AND ' , $ condition );
241
+ $ deleteSql = "DELETE FROM {$ this ->policyTableName } WHERE " . implode (' AND ' , $ condition );
237
242
238
- $ selectSql = "SELECT * FROM {$ this ->casbinRuleTableName } WHERE " . implode (' AND ' , $ condition );
243
+ $ selectSql = "SELECT * FROM {$ this ->policyTableName } WHERE " . implode (' AND ' , $ condition );
239
244
240
245
$ oldP = $ this ->connection ->query ($ selectSql , $ where );
241
246
foreach ($ oldP as &$ item ) {
@@ -275,7 +280,7 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
275
280
public function loadFilteredPolicy (Model $ model , $ filter ): void
276
281
{
277
282
// the basic sql
278
- $ sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->casbinRuleTableName . ' WHERE ' ;
283
+ $ sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->policyTableName . ' WHERE ' ;
279
284
280
285
$ bind = [];
281
286
@@ -346,7 +351,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
346
351
$ update [] = 'v ' . strval ($ key ) . ' = : ' . $ placeholder ;
347
352
}
348
353
349
- $ sql = "UPDATE {$ this ->casbinRuleTableName } SET " . implode (', ' , $ update ) . " WHERE " . implode (' AND ' , $ condition );
354
+ $ sql = "UPDATE {$ this ->policyTableName } SET " . implode (', ' , $ update ) . " WHERE " . implode (' AND ' , $ condition );
350
355
351
356
$ this ->connection ->execute ($ sql , array_merge ($ updateValue , $ where ));
352
357
}
0 commit comments