Skip to content

Commit e0355bd

Browse files
committed
Added Casbin 2.0 support
1 parent 4d94595 commit e0355bd

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ language: php
22
sudo: false
33

44
php:
5-
- 5.6
6-
- 7.0
75
- 7.1
86
- 7.2
97
- 7.3

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
],
1111
"license": "Apache-2.0",
1212
"require": {
13-
"casbin/casbin": "~1.0",
14-
"techone/database": ">=0.2.0"
13+
"casbin/casbin": "^2.0",
14+
"techone/database": "^0.2"
1515
},
1616
"require-dev": {
17-
"phpunit/phpunit": "~5.7|~6.0|~7.0",
17+
"phpunit/phpunit": "~7.0",
1818
"php-coveralls/php-coveralls": "^2.1"
1919
},
2020
"autoload": {

src/Adapter.php

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CasbinAdapter\Database;
44

5+
use Casbin\Model\Model;
56
use Casbin\Persist\Adapter as AdapterContract;
67
use TechOne\Database\Manager;
78
use Casbin\Persist\AdapterHelper;
@@ -56,7 +57,12 @@ public function savePolicyLine($ptype, array $rule)
5657
$this->connection->execute($sql, array_values($col));
5758
}
5859

59-
public function loadPolicy($model)
60+
/**
61+
* loads all policy rules from the storage.
62+
*
63+
* @param Model $model
64+
*/
65+
public function loadPolicy(Model $model): void
6066
{
6167
$rows = $this->connection->query('SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName.'');
6268

@@ -68,29 +74,47 @@ public function loadPolicy($model)
6874
}
6975
}
7076

71-
public function savePolicy($model)
77+
/**
78+
* saves all policy rules to the storage.
79+
*
80+
* @param Model $model
81+
*/
82+
public function savePolicy(Model $model): void
7283
{
73-
foreach ($model->model['p'] as $ptype => $ast) {
84+
foreach ($model['p'] as $ptype => $ast) {
7485
foreach ($ast->policy as $rule) {
7586
$this->savePolicyLine($ptype, $rule);
7687
}
7788
}
7889

79-
foreach ($model->model['g'] as $ptype => $ast) {
90+
foreach ($model['g'] as $ptype => $ast) {
8091
foreach ($ast->policy as $rule) {
8192
$this->savePolicyLine($ptype, $rule);
8293
}
8394
}
84-
85-
return true;
8695
}
8796

88-
public function addPolicy($sec, $ptype, $rule)
97+
/**
98+
* adds a policy rule to the storage.
99+
* This is part of the Auto-Save feature.
100+
*
101+
* @param string $sec
102+
* @param string $ptype
103+
* @param array $rule
104+
*/
105+
public function addPolicy(string $sec, string $ptype, array $rule): void
89106
{
90-
return $this->savePolicyLine($ptype, $rule);
107+
$this->savePolicyLine($ptype, $rule);
91108
}
92109

93-
public function removePolicy($sec, $ptype, $rule)
110+
/**
111+
* This is part of the Auto-Save feature.
112+
*
113+
* @param string $sec
114+
* @param string $ptype
115+
* @param array $rule
116+
*/
117+
public function removePolicy(string $sec, string $ptype, array $rule): void
94118
{
95119
$where['ptype'] = $ptype;
96120
$condition[] = 'ptype = :ptype';
@@ -101,10 +125,19 @@ public function removePolicy($sec, $ptype, $rule)
101125

102126
$sql = 'DELETE FROM '.$this->casbinRuleTableName.' WHERE '.implode(' AND ', $condition);
103127

104-
return $this->connection->execute($sql, $where);
128+
$this->connection->execute($sql, $where);
105129
}
106130

107-
public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
131+
/**
132+
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
133+
* This is part of the Auto-Save feature.
134+
*
135+
* @param string $sec
136+
* @param string $ptype
137+
* @param int $fieldIndex
138+
* @param string ...$fieldValues
139+
*/
140+
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
108141
{
109142
$where['ptype'] = $ptype;
110143
$condition[] = 'ptype = :ptype';
@@ -119,6 +152,6 @@ public function removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues)
119152

120153
$sql = 'DELETE FROM '.$this->casbinRuleTableName.' WHERE '.implode(' AND ', $condition);
121154

122-
return $this->connection->execute($sql, $where);
155+
$this->connection->execute($sql, $where);
123156
}
124157
}

tests/casbin.db

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)