Skip to content

Commit 97c0d37

Browse files
authored
feat: Support policy table name configuration (#22)
1 parent 6fb213b commit 97c0d37

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

Diff for: src/Adapter.php

+15-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Adapter implements AdapterContract, FilteredAdapterContract, BatchAdapterC
2929

3030
protected $connection;
3131

32-
public $casbinRuleTableName = 'casbin_rule';
32+
public $policyTableName = 'casbin_rule';
3333

3434
public $rows = [];
3535

@@ -38,6 +38,11 @@ public function __construct(array $config)
3838
$this->config = $config;
3939
$this->filtered = false;
4040
$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+
4146
$this->initTable();
4247
}
4348

@@ -89,7 +94,7 @@ public static function newAdapter(array $config)
8994
public function initTable()
9095
{
9196
$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);
9398
$this->connection->execute($sql, []);
9499
}
95100

@@ -104,7 +109,7 @@ public function savePolicyLine($ptype, array $rule)
104109

105110
$name = rtrim(str_repeat('?, ', count($col)), ', ');
106111

107-
$sql = 'INSERT INTO '.$this->casbinRuleTableName.'('.$colStr.') VALUES ('.$name.') ';
112+
$sql = 'INSERT INTO '.$this->policyTableName.'('.$colStr.') VALUES ('.$name.') ';
108113

109114
$this->connection->execute($sql, array_values($col));
110115
}
@@ -116,7 +121,7 @@ public function savePolicyLine($ptype, array $rule)
116121
*/
117122
public function loadPolicy(Model $model): void
118123
{
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.'');
120125

121126
foreach ($rows as $row) {
122127
$this->loadPolicyArray($this->filterRule($row), $model);
@@ -158,7 +163,7 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
158163

159164
public function addPolicies(string $sec, string $ptype, array $rules): void
160165
{
161-
$table = $this->casbinRuleTableName;
166+
$table = $this->policyTableName;
162167
$columns = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
163168
$values = [];
164169
$sets = [];
@@ -206,7 +211,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
206211
$condition[] = 'v'.strval($key).' = :'.'v'.strval($key);
207212
}
208213

209-
$sql = 'DELETE FROM '.$this->casbinRuleTableName.' WHERE '.implode(' AND ', $condition);
214+
$sql = 'DELETE FROM '.$this->policyTableName.' WHERE '.implode(' AND ', $condition);
210215

211216
$this->connection->execute($sql, $where);
212217
}
@@ -233,9 +238,9 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
233238
}
234239
}
235240

236-
$deleteSql = "DELETE FROM {$this->casbinRuleTableName} WHERE " . implode(' AND ', $condition);
241+
$deleteSql = "DELETE FROM {$this->policyTableName} WHERE " . implode(' AND ', $condition);
237242

238-
$selectSql = "SELECT * FROM {$this->casbinRuleTableName} WHERE " . implode(' AND ', $condition);
243+
$selectSql = "SELECT * FROM {$this->policyTableName} WHERE " . implode(' AND ', $condition);
239244

240245
$oldP = $this->connection->query($selectSql, $where);
241246
foreach ($oldP as &$item) {
@@ -275,7 +280,7 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
275280
public function loadFilteredPolicy(Model $model, $filter): void
276281
{
277282
// 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 ';
279284

280285
$bind = [];
281286

@@ -346,7 +351,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
346351
$update[] = 'v' . strval($key) . ' = :' . $placeholder;
347352
}
348353

349-
$sql = "UPDATE {$this->casbinRuleTableName} SET " . implode(', ', $update) . " WHERE " . implode(' AND ', $condition);
354+
$sql = "UPDATE {$this->policyTableName} SET " . implode(', ', $update) . " WHERE " . implode(' AND ', $condition);
350355

351356
$this->connection->execute($sql, array_merge($updateValue, $where));
352357
}

0 commit comments

Comments
 (0)