Skip to content

Commit f7a0628

Browse files
committed
remove some unused files and methods
1 parent 4974570 commit f7a0628

File tree

3 files changed

+3
-173
lines changed

3 files changed

+3
-173
lines changed

.DS_Store

-8 KB
Binary file not shown.

index.php

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

src/FilterAdapter.php

Lines changed: 3 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ public function isFiltered(): bool
4747
*/
4848
public function loadFilteredPolicy(Model $model, $filter): void
4949
{
50-
// 如果$filter为空,就加载所有的策略
50+
// if $filter is empty, load all policies
5151
if (is_null($filter)) {
5252
$this->loadPolicy($model);
5353
return;
5454
}
55-
// 确保$filter的类型正确
55+
// validate $filter is a instance of Filter
5656
if (!$filter instanceof Filter) {
5757
throw new InvalidFilterTypeException('invalid filter type');
5858
}
5959
$type = '';
6060
$filter = (array) $filter;
61-
// 要判断ptype是p还是g
61+
// choose which ptype to use
6262
foreach($filter as $i => $v) {
6363
if (!empty($v)) {
6464
array_unshift($filter[$i], $i);
@@ -85,138 +85,4 @@ public function loadFilteredPolicy(Model $model, $filter): void
8585

8686
$this->filtered = true;
8787
}
88-
89-
/**
90-
* load filtered policy
91-
* 通过拼接sql语句来实现过滤功能,但直接将字段的值进行了拼接,存在安全方面的问题,无法防止SQL注入等情况,应该用query方法的第二个参数来改写
92-
*
93-
* @param Model $model
94-
* @param [type] $filter
95-
* @return void
96-
*/
97-
public function loadFilteredPolicy1(Model $model, $filter): void
98-
{
99-
// 如果$filter为空,就加载所有的策略
100-
if (is_null($filter)) {
101-
$this->loadPolicy($model);
102-
return;
103-
}
104-
// 确保$filter的类型正确
105-
if (!$filter instanceof Filter) {
106-
throw new InvalidFilterTypeException('invalid filter type');
107-
}
108-
$filter = (array) $filter;
109-
// 要判断ptype是p还是g
110-
foreach($filter as $i => $v) {
111-
if (!empty($v)) {
112-
array_unshift($filter[$i], $i);
113-
break;
114-
}
115-
}
116-
$sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName . ' WHERE ';
117-
$items = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'];
118-
$temp = [];
119-
foreach($items as $i => $item) {
120-
if (isset($filter['p'][$i]) && !empty($filter['p'][$i])) {
121-
array_push($temp, $item . '=' . '\'' . $filter['p'][$i] . '\'');
122-
}
123-
}
124-
$sql .= implode(' and ', $temp);
125-
$rows = $this->connection->query($sql);
126-
foreach($rows as $row) {
127-
$line = implode(', ', $row);
128-
$this->loadPolicyLine($line, $model);
129-
}
130-
$this->filtered = true;
131-
}
132-
133-
/**
134-
* load filtered policy
135-
* 仿照文件过滤适配器完成的,代码较多
136-
*
137-
* @param Model $model
138-
* @param [type] $filter
139-
* @return void
140-
*/
141-
public function loadFilteredPolicy2(Model $model, $filter): void
142-
{
143-
// 如果$filter为空,就加载所有的策略
144-
if (is_null($filter)) {
145-
$this->loadPolicy($model);
146-
return;
147-
}
148-
// 确保$filter的类型正确
149-
if (!$filter instanceof Filter) {
150-
throw new InvalidFilterTypeException('invalid filter type');
151-
}
152-
$rows = $this->connection->query('SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName.'');
153-
foreach($rows as $row) {
154-
if (self::filterLine(array_values($row), $filter)) {
155-
continue;
156-
}
157-
$line = implode(', ', $row);
158-
//var_dump($line);continue;
159-
$this->loadPolicyLine($line, $model);
160-
}
161-
$this->filtered = true;
162-
}
163-
164-
/**
165-
* FilterLine function.
166-
*
167-
* @param array $row
168-
* @param Filter $filter
169-
*
170-
* @return bool
171-
*/
172-
protected static function filterLine(array $row, Filter $filter): bool
173-
{
174-
if (0 == \count($row)) {
175-
return true;
176-
}
177-
178-
$filterSlice = [];
179-
switch (trim($row[0])) {
180-
case 'p':
181-
$filterSlice = $filter->p;
182-
// var_dump($filterSlice);exit;
183-
break;
184-
case 'g':
185-
$filterSlice = $filter->g;
186-
187-
break;
188-
}
189-
190-
return self::filterWords($row, $filterSlice);
191-
}
192-
193-
/**
194-
* FilterWords function.
195-
*
196-
* @param array $line ['p', 'alice', 'data1', 'read']
197-
* @param array $filter ['alice']
198-
*
199-
* @return bool
200-
*/
201-
protected static function filterWords(array $line, array $filter): bool
202-
{
203-
if (count($line) < count($filter) + 1) {
204-
return true;
205-
}
206-
$skipLine = false;
207-
// var_dump($filter);exit;
208-
// $i从0开始,依次递增
209-
// $filter中的第n个元素和$line中的第n+1个元素比较,不想等就跳过这一行(继续下一次循环,不会执行循环体下面的代码)
210-
foreach ($filter as $i => $v) {
211-
//var_dump($filter, $i, $v, $line[$i + 1]);exit;
212-
if (strlen($v) > 0 && \trim($v) != trim($line[$i + 1])) {
213-
$skipLine = true;
214-
215-
break;
216-
}
217-
}
218-
// var_dump($line, $filter, $skipLine);
219-
return $skipLine;
220-
}
221-
22288
}

0 commit comments

Comments
 (0)