Skip to content

Commit d70a5bc

Browse files
committed
refactor: change the field ptype to p_type
1 parent 17841ca commit d70a5bc

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

src/Adapter.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class Adapter implements AdapterContract
2828
protected $connection;
2929

3030
/**
31-
* CasbinRule table name.
31+
* Casbin policies table name.
3232
*
3333
* @var string
3434
*/
35-
public $casbinRuleTableName = 'casbin_rule';
35+
public $policyTableName = 'casbin_rule';
3636

3737
/**
3838
* Adapter constructor.
@@ -48,6 +48,10 @@ public function __construct($connection)
4848
$connection,
4949
new Configuration()
5050
);
51+
52+
if (is_array($connection) && isset($connection['policy_table_name']) && !is_null( $connection['policy_table_name'])){
53+
$this->policyTableName = $connection['policy_table_name'];
54+
}
5155
}
5256

5357
$this->initTable();
@@ -73,11 +77,11 @@ public static function newAdapter($connection): AdapterContract
7377
public function initTable()
7478
{
7579
$sm = $this->connection->getSchemaManager();
76-
if (!$sm->tablesExist([$this->casbinRuleTableName])) {
80+
if (!$sm->tablesExist([$this->policyTableName])) {
7781
$schema = new \Doctrine\DBAL\Schema\Schema();
78-
$table = $schema->createTable($this->casbinRuleTableName);
82+
$table = $schema->createTable($this->policyTableName);
7983
$table->addColumn('id', 'integer', array('autoincrement' => true));
80-
$table->addColumn('ptype', 'string', ['notnull' => false]);
84+
$table->addColumn('p_type', 'string', ['notnull' => false]);
8185
$table->addColumn('v0', 'string', ['notnull' => false]);
8286
$table->addColumn('v1', 'string', ['notnull' => false]);
8387
$table->addColumn('v2', 'string', ['notnull' => false]);
@@ -89,15 +93,15 @@ public function initTable()
8993
}
9094
}
9195

92-
public function savePolicyLine($ptype, array $rule)
96+
public function savePolicyLine($pType, array $rule)
9397
{
9498
$queryBuilder = $this->connection->createQueryBuilder();
9599
$queryBuilder
96-
->insert($this->casbinRuleTableName)
100+
->insert($this->policyTableName)
97101
->values([
98-
'ptype' => '?',
102+
'p_type' => '?',
99103
])
100-
->setParameter(0, $ptype);
104+
->setParameter(0, $pType);
101105

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

119123
while ($row = $stmt->fetch()) {
120124
$line = implode(', ', array_filter($row, function ($val) {
@@ -131,14 +135,14 @@ public function loadPolicy(Model $model): void
131135
*/
132136
public function savePolicy(Model $model): void
133137
{
134-
foreach ($model['p'] as $ptype => $ast) {
138+
foreach ($model['p'] as $pType => $ast) {
135139
foreach ($ast->policy as $rule) {
136-
$this->savePolicyLine($ptype, $rule);
140+
$this->savePolicyLine($pType, $rule);
137141
}
138142
}
139-
foreach ($model['g'] as $ptype => $ast) {
143+
foreach ($model['g'] as $pType => $ast) {
140144
foreach ($ast->policy as $rule) {
141-
$this->savePolicyLine($ptype, $rule);
145+
$this->savePolicyLine($pType, $rule);
142146
}
143147
}
144148
}
@@ -148,46 +152,46 @@ public function savePolicy(Model $model): void
148152
* This is part of the Auto-Save feature.
149153
*
150154
* @param string $sec
151-
* @param string $ptype
155+
* @param string $pType
152156
* @param array $rule
153157
*/
154-
public function addPolicy(string $sec, string $ptype, array $rule): void
158+
public function addPolicy(string $sec, string $pType, array $rule): void
155159
{
156-
$this->savePolicyLine($ptype, $rule);
160+
$this->savePolicyLine($pType, $rule);
157161
}
158162

159163
/**
160164
* This is part of the Auto-Save feature.
161165
*
162166
* @param string $sec
163-
* @param string $ptype
167+
* @param string $pType
164168
* @param array $rule
165169
*/
166-
public function removePolicy(string $sec, string $ptype, array $rule): void
170+
public function removePolicy(string $sec, string $pType, array $rule): void
167171
{
168172
$queryBuilder = $this->connection->createQueryBuilder();
169-
$queryBuilder->delete($this->casbinRuleTableName)->where('ptype = ?')->setParameter(0, $ptype);
173+
$queryBuilder->delete($this->policyTableName)->where('p_type = ?')->setParameter(0, $pType);
170174

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

175-
$queryBuilder->delete($this->casbinRuleTableName)->execute();
179+
$queryBuilder->delete($this->policyTableName)->execute();
176180
}
177181

178182
/**
179183
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
180184
* This is part of the Auto-Save feature.
181185
*
182186
* @param string $sec
183-
* @param string $ptype
187+
* @param string $pType
184188
* @param int $fieldIndex
185189
* @param string ...$fieldValues
186190
*/
187-
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
191+
public function removeFilteredPolicy(string $sec, string $pType, int $fieldIndex, string ...$fieldValues): void
188192
{
189193
$queryBuilder = $this->connection->createQueryBuilder();
190-
$queryBuilder->where('ptype = :ptype')->setParameter(':ptype', $ptype);
194+
$queryBuilder->where('p_type = :pType')->setParameter(':pType', $pType);
191195

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

201-
$queryBuilder->delete($this->casbinRuleTableName)->execute();
205+
$queryBuilder->delete($this->policyTableName)->execute();
202206
}
203207

204208
/**

tests/AdapterTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ protected function initConfig()
2727

2828
protected function initDb(DatabaseAdapter $adapter)
2929
{
30-
$tableName = $adapter->casbinRuleTableName;
30+
$tableName = $adapter->policyTableName;
3131
$conn = $adapter->getConnection();
3232
$queryBuilder = $conn->createQueryBuilder();
3333
$queryBuilder->delete($tableName)->where('1 = 1')->execute();
3434

3535
$data = [
36-
['ptype' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read'],
37-
['ptype' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write'],
38-
['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read'],
39-
['ptype' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write'],
40-
['ptype' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin'],
36+
['p_type' => 'p', 'v0' => 'alice', 'v1' => 'data1', 'v2' => 'read'],
37+
['p_type' => 'p', 'v0' => 'bob', 'v1' => 'data2', 'v2' => 'write'],
38+
['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'read'],
39+
['p_type' => 'p', 'v0' => 'data2_admin', 'v1' => 'data2', 'v2' => 'write'],
40+
['p_type' => 'g', 'v0' => 'alice', 'v1' => 'data2_admin'],
4141
];
4242
foreach ($data as $row) {
4343
$queryBuilder->insert($tableName)->values(array_combine(array_keys($row), array_fill(0, count($row), '?')))->setParameters(array_values($row))->execute();
@@ -47,11 +47,7 @@ protected function initDb(DatabaseAdapter $adapter)
4747
protected function getEnforcer()
4848
{
4949
$this->initConfig();
50-
$connection = DriverManager::getConnection(
51-
$this->config,
52-
new Configuration()
53-
);
54-
$adapter = DatabaseAdapter::newAdapter($connection);
50+
$adapter = DatabaseAdapter::newAdapter($this->config);
5551

5652
$this->initDb($adapter);
5753
$model = Model::newModelFromString(

0 commit comments

Comments
 (0)