Skip to content

Commit

Permalink
feat: Support policy table name configuration (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip authored Sep 28, 2022
1 parent 6fb213b commit 97c0d37
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Adapter implements AdapterContract, FilteredAdapterContract, BatchAdapterC

protected $connection;

public $casbinRuleTableName = 'casbin_rule';
public $policyTableName = 'casbin_rule';

public $rows = [];

Expand All @@ -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();
}

Expand Down Expand Up @@ -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, []);
}

Expand All @@ -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));
}
Expand All @@ -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);
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 97c0d37

Please sign in to comment.