diff --git a/src/Adapter.php b/src/Adapter.php index 9b1b7e7..7848c7a 100644 --- a/src/Adapter.php +++ b/src/Adapter.php @@ -29,7 +29,7 @@ class Adapter implements AdapterContract, FilteredAdapterContract, BatchAdapterC protected $connection; - public $casbinRuleTableName = 'casbin_rule'; + public $policyTableName = 'casbin_rule'; public $rows = []; @@ -38,6 +38,11 @@ public function __construct(array $config) $this->config = $config; $this->filtered = false; $this->connection = (new Manager($config))->getConnection(); + + if (isset($config['policy_table_name']) && !is_null($config['policy_table_name'])) { + $this->policyTableName = $config['policy_table_name']; + } + $this->initTable(); } @@ -89,7 +94,7 @@ public static function newAdapter(array $config) public function initTable() { $sql = file_get_contents(__DIR__.'/../migrations/'.$this->config['type'].'.sql'); - $sql = str_replace('%table_name%', $this->casbinRuleTableName, $sql); + $sql = str_replace('%table_name%', $this->policyTableName, $sql); $this->connection->execute($sql, []); } @@ -104,7 +109,7 @@ public function savePolicyLine($ptype, array $rule) $name = rtrim(str_repeat('?, ', count($col)), ', '); - $sql = 'INSERT INTO '.$this->casbinRuleTableName.'('.$colStr.') VALUES ('.$name.') '; + $sql = 'INSERT INTO '.$this->policyTableName.'('.$colStr.') VALUES ('.$name.') '; $this->connection->execute($sql, array_values($col)); } @@ -116,7 +121,7 @@ public function savePolicyLine($ptype, array $rule) */ public function loadPolicy(Model $model): void { - $rows = $this->connection->query('SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName.''); + $rows = $this->connection->query('SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->policyTableName.''); foreach ($rows as $row) { $this->loadPolicyArray($this->filterRule($row), $model); @@ -158,7 +163,7 @@ public function addPolicy(string $sec, string $ptype, array $rule): void public function addPolicies(string $sec, string $ptype, array $rules): void { - $table = $this->casbinRuleTableName; + $table = $this->policyTableName; $columns = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5']; $values = []; $sets = []; @@ -206,7 +211,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void $condition[] = 'v'.strval($key).' = :'.'v'.strval($key); } - $sql = 'DELETE FROM '.$this->casbinRuleTableName.' WHERE '.implode(' AND ', $condition); + $sql = 'DELETE FROM '.$this->policyTableName.' WHERE '.implode(' AND ', $condition); $this->connection->execute($sql, $where); } @@ -233,9 +238,9 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde } } - $deleteSql = "DELETE FROM {$this->casbinRuleTableName} WHERE " . implode(' AND ', $condition); + $deleteSql = "DELETE FROM {$this->policyTableName} WHERE " . implode(' AND ', $condition); - $selectSql = "SELECT * FROM {$this->casbinRuleTableName} WHERE " . implode(' AND ', $condition); + $selectSql = "SELECT * FROM {$this->policyTableName} WHERE " . implode(' AND ', $condition); $oldP = $this->connection->query($selectSql, $where); foreach ($oldP as &$item) { @@ -275,7 +280,7 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex public function loadFilteredPolicy(Model $model, $filter): void { // the basic sql - $sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName . ' WHERE '; + $sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->policyTableName . ' WHERE '; $bind = []; @@ -346,7 +351,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $ $update[] = 'v' . strval($key) . ' = :' . $placeholder; } - $sql = "UPDATE {$this->casbinRuleTableName} SET " . implode(', ', $update) . " WHERE " . implode(' AND ', $condition); + $sql = "UPDATE {$this->policyTableName} SET " . implode(', ', $update) . " WHERE " . implode(' AND ', $condition); $this->connection->execute($sql, array_merge($updateValue, $where)); }