Skip to content

Commit e8ea0fa

Browse files
committed
make Adapter implement class Adapter implements FilteredAdapter, change an item of .travis.yml
1 parent f7a0628 commit e8ea0fa

File tree

3 files changed

+67
-90
lines changed

3 files changed

+67
-90
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ install:
2121
script:
2222
- vendor/bin/phpunit --version
2323
- mkdir -p build/logs
24-
- vendor/bin/phpunit
24+
- XDEBUG_MODE=coverage vendor/bin/phpunit
2525

2626
after_script:
2727
- travis_retry vendor/bin/php-coveralls -v

src/Adapter.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,94 @@
66
use Casbin\Persist\Adapter as AdapterContract;
77
use TechOne\Database\Manager;
88
use Casbin\Persist\AdapterHelper;
9+
use Casbin\Persist\FilteredAdapter;
10+
use Casbin\Persist\Adapters\Filter;
11+
use Casbin\Exceptions\InvalidFilterTypeException;
912

1013
/**
1114
* DatabaseAdapter.
1215
*
1316
1417
*/
15-
class Adapter implements AdapterContract
18+
class Adapter implements AdapterContract, FilteredAdapter
1619
{
1720
use AdapterHelper;
1821

1922
protected $config;
2023

24+
protected $filtered;
25+
2126
protected $connection;
2227

2328
public $casbinRuleTableName = 'casbin_rule';
2429

2530
public function __construct(array $config)
2631
{
2732
$this->config = $config;
33+
$this->filtered = false;
2834
$this->connection = (new Manager($config))->getConnection();
2935
$this->initTable();
3036
}
3137

38+
/**
39+
* Returns true if the loaded policy has been filtered.
40+
*
41+
* @return bool
42+
*/
43+
public function isFiltered(): bool
44+
{
45+
return $this->filtered;
46+
}
47+
48+
/**
49+
* Loads only policy rules that match the filter from storage.
50+
*
51+
* @param Model $model
52+
* @param mixed $filter
53+
*
54+
* @throws CasbinException
55+
*/
56+
public function loadFilteredPolicy(Model $model, $filter): void
57+
{
58+
// if $filter is empty, load all policies
59+
if (is_null($filter)) {
60+
$this->loadPolicy($model);
61+
return;
62+
}
63+
// validate $filter is a instance of Filter
64+
if (!$filter instanceof Filter) {
65+
throw new InvalidFilterTypeException('invalid filter type');
66+
}
67+
$type = '';
68+
$filter = (array) $filter;
69+
// choose which ptype to use
70+
foreach($filter as $i => $v) {
71+
if (!empty($v)) {
72+
array_unshift($filter[$i], $i);
73+
$type = $i;
74+
break;
75+
}
76+
}
77+
$sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName . ' WHERE ';
78+
$items = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
79+
$temp = [];
80+
$values = [];
81+
foreach($items as $i => $item) {
82+
if (isset($filter[$type][$i]) && !empty($filter[$type][$i])) {
83+
array_push($temp, $item . '=:' . $item);
84+
$values[$item] = $filter[$type][$i];
85+
}
86+
}
87+
$sql .= implode(' and ', $temp);
88+
$rows = $this->connection->query($sql, $values);
89+
foreach($rows as $row) {
90+
$line = implode(', ', $row);
91+
$this->loadPolicyLine($line, $model);
92+
}
93+
94+
$this->filtered = true;
95+
}
96+
3297
public static function newAdapter(array $config)
3398
{
3499
return new static($config);

src/FilterAdapter.php

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)