Skip to content

Commit

Permalink
refactor: change the field ptype to p_type
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqvip committed Dec 16, 2020
1 parent 17841ca commit d70a5bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
54 changes: 29 additions & 25 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class Adapter implements AdapterContract
protected $connection;

/**
* CasbinRule table name.
* Casbin policies table name.
*
* @var string
*/
public $casbinRuleTableName = 'casbin_rule';
public $policyTableName = 'casbin_rule';

/**
* Adapter constructor.
Expand All @@ -48,6 +48,10 @@ public function __construct($connection)
$connection,
new Configuration()
);

if (is_array($connection) && isset($connection['policy_table_name']) && !is_null( $connection['policy_table_name'])){
$this->policyTableName = $connection['policy_table_name'];
}
}

$this->initTable();
Expand All @@ -73,11 +77,11 @@ public static function newAdapter($connection): AdapterContract
public function initTable()
{
$sm = $this->connection->getSchemaManager();
if (!$sm->tablesExist([$this->casbinRuleTableName])) {
if (!$sm->tablesExist([$this->policyTableName])) {
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable($this->casbinRuleTableName);
$table = $schema->createTable($this->policyTableName);
$table->addColumn('id', 'integer', array('autoincrement' => true));
$table->addColumn('ptype', 'string', ['notnull' => false]);
$table->addColumn('p_type', 'string', ['notnull' => false]);
$table->addColumn('v0', 'string', ['notnull' => false]);
$table->addColumn('v1', 'string', ['notnull' => false]);
$table->addColumn('v2', 'string', ['notnull' => false]);
Expand All @@ -89,15 +93,15 @@ public function initTable()
}
}

public function savePolicyLine($ptype, array $rule)
public function savePolicyLine($pType, array $rule)
{
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder
->insert($this->casbinRuleTableName)
->insert($this->policyTableName)
->values([
'ptype' => '?',
'p_type' => '?',
])
->setParameter(0, $ptype);
->setParameter(0, $pType);

foreach ($rule as $key => $value) {
$queryBuilder->setValue('v'.strval($key), '?')->setParameter($key + 1, $value);
Expand All @@ -114,7 +118,7 @@ public function savePolicyLine($ptype, array $rule)
public function loadPolicy(Model $model): void
{
$queryBuilder = $this->connection->createQueryBuilder();
$stmt = $queryBuilder->select('ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->from($this->casbinRuleTableName)->execute();
$stmt = $queryBuilder->select('p_type', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5')->from($this->policyTableName)->execute();

while ($row = $stmt->fetch()) {
$line = implode(', ', array_filter($row, function ($val) {
Expand All @@ -131,14 +135,14 @@ public function loadPolicy(Model $model): void
*/
public function savePolicy(Model $model): void
{
foreach ($model['p'] as $ptype => $ast) {
foreach ($model['p'] as $pType => $ast) {
foreach ($ast->policy as $rule) {
$this->savePolicyLine($ptype, $rule);
$this->savePolicyLine($pType, $rule);
}
}
foreach ($model['g'] as $ptype => $ast) {
foreach ($model['g'] as $pType => $ast) {
foreach ($ast->policy as $rule) {
$this->savePolicyLine($ptype, $rule);
$this->savePolicyLine($pType, $rule);
}
}
}
Expand All @@ -148,46 +152,46 @@ public function savePolicy(Model $model): void
* This is part of the Auto-Save feature.
*
* @param string $sec
* @param string $ptype
* @param string $pType
* @param array $rule
*/
public function addPolicy(string $sec, string $ptype, array $rule): void
public function addPolicy(string $sec, string $pType, array $rule): void
{
$this->savePolicyLine($ptype, $rule);
$this->savePolicyLine($pType, $rule);
}

/**
* This is part of the Auto-Save feature.
*
* @param string $sec
* @param string $ptype
* @param string $pType
* @param array $rule
*/
public function removePolicy(string $sec, string $ptype, array $rule): void
public function removePolicy(string $sec, string $pType, array $rule): void
{
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder->delete($this->casbinRuleTableName)->where('ptype = ?')->setParameter(0, $ptype);
$queryBuilder->delete($this->policyTableName)->where('p_type = ?')->setParameter(0, $pType);

foreach ($rule as $key => $value) {
$queryBuilder->andWhere('v'.strval($key).' = ?')->setParameter($key + 1, $value);
}

$queryBuilder->delete($this->casbinRuleTableName)->execute();
$queryBuilder->delete($this->policyTableName)->execute();
}

/**
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
* This is part of the Auto-Save feature.
*
* @param string $sec
* @param string $ptype
* @param string $pType
* @param int $fieldIndex
* @param string ...$fieldValues
*/
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
public function removeFilteredPolicy(string $sec, string $pType, int $fieldIndex, string ...$fieldValues): void
{
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder->where('ptype = :ptype')->setParameter(':ptype', $ptype);
$queryBuilder->where('p_type = :pType')->setParameter(':pType', $pType);

foreach (range(0, 5) as $value) {
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
Expand All @@ -198,7 +202,7 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
}
}

$queryBuilder->delete($this->casbinRuleTableName)->execute();
$queryBuilder->delete($this->policyTableName)->execute();
}

/**
Expand Down
18 changes: 7 additions & 11 deletions tests/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ protected function initConfig()

protected function initDb(DatabaseAdapter $adapter)
{
$tableName = $adapter->casbinRuleTableName;
$tableName = $adapter->policyTableName;
$conn = $adapter->getConnection();
$queryBuilder = $conn->createQueryBuilder();
$queryBuilder->delete($tableName)->where('1 = 1')->execute();

$data = [
['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read'],
['ptype' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write'],
['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read'],
['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write'],
['ptype' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin'],
['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read'],
['p_type' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write'],
['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read'],
['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write'],
['p_type' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin'],
];
foreach ($data as $row) {
$queryBuilder->insert($tableName)->values(array_combine(array_keys($row), array_fill(0, count($row), '?')))->setParameters(array_values($row))->execute();
Expand All @@ -47,11 +47,7 @@ protected function initDb(DatabaseAdapter $adapter)
protected function getEnforcer()
{
$this->initConfig();
$connection = DriverManager::getConnection(
$this->config,
new Configuration()
);
$adapter = DatabaseAdapter::newAdapter($connection);
$adapter = DatabaseAdapter::newAdapter($this->config);

$this->initDb($adapter);
$model = Model::newModelFromString(
Expand Down

0 comments on commit d70a5bc

Please sign in to comment.