Skip to content

Commit c03b263

Browse files
committed
refactor: A code change that neither fixes a bug nor adds a feature.
1 parent a242119 commit c03b263

File tree

3 files changed

+353
-297
lines changed

3 files changed

+353
-297
lines changed

src/Adapters/DatabaseAdapter.php

Lines changed: 121 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -7,150 +7,142 @@
77
use Casbin\Persist\AdapterHelper;
88
use Casbin\Model\Model;
99
use Casbin\Persist\Adapter;
10+
use EasySwoole\ORM\Collection\Collection;
11+
use EasySwoole\ORM\Exception\Exception;
1012
use EasySwoole\Permission\Model\RulesModel;
13+
use Throwable;
1114

1215
class DatabaseAdapter implements Adapter
1316
{
14-
use AdapterHelper;
15-
16-
/**
17-
* Rules eloquent model
18-
*
19-
* @var Rule
20-
*/
21-
protected $eloquent;
22-
23-
/**
24-
* the DatabaseAdapter constructor.
25-
*
26-
* @param Rule $eloquent
27-
*/
28-
public function __construct(RulesModel $eloquent)
29-
{
30-
$this->eloquent = $eloquent;
31-
}
32-
33-
/**
34-
* savePolicyLine function.
35-
*
36-
* @param string $ptype
37-
* @param array $rule
38-
*/
39-
public function savePolicyLine(string $ptype, array $rule): void
40-
{
41-
$col['ptype'] = $ptype;
42-
foreach ($rule as $key => $value) {
43-
$col['v' . strval($key)] = $value;
44-
}
17+
use AdapterHelper;
18+
19+
/**
20+
* savePolicyLine function.
21+
*
22+
* @param string $ptype
23+
* @param array $rule
24+
* @throws Exception
25+
* @throws Throwable
26+
*/
27+
public function savePolicyLine(string $ptype, array $rule): void
28+
{
29+
$col['ptype'] = $ptype;
30+
foreach ($rule as $key => $value) {
31+
$col['v' . strval($key)] = $value;
32+
}
4533

46-
$this->eloquent->create()->data($col, false)->save();
47-
}
48-
49-
/**
50-
* loads all policy rules from the storage.
51-
*
52-
* @param Model $model
53-
*/
54-
public function loadPolicy(Model $model): void
55-
{
56-
$rows = $this->eloquent->create()->all();
57-
foreach ($rows as &$row) {
58-
$row = $row->hidden(['id', 'create_time', 'update_time'])->toArray(false, false);
34+
RulesModel::create()->data($col, false)->save();
5935
}
60-
unset($row);
6136

62-
foreach ($rows as $row) {
63-
$line = implode(', ', array_filter($row, function ($val) {
64-
return '' != $val && !is_null($val);
65-
}));
66-
$this->loadPolicyLine(trim($line), $model);
37+
/**
38+
* loads all policy rules from the storage.
39+
*
40+
* @param Model $model
41+
* @throws Exception
42+
* @throws Throwable
43+
*/
44+
public function loadPolicy(Model $model): void
45+
{
46+
$instance = RulesModel::create();
47+
$results = $instance->all();
48+
if (!$results) {
49+
return;
50+
}
51+
52+
if (!$results instanceof Collection) {
53+
$results = new Collection($results);
54+
}
55+
56+
$rows = $results->hidden(['id', 'create_time', 'update_time'])->toArray(false, false);
57+
58+
foreach ($rows as $row) {
59+
$line = implode(', ', array_filter($row, function ($val) {
60+
return '' != $val && !is_null($val);
61+
}));
62+
$this->loadPolicyLine(trim($line), $model);
63+
}
6764
}
68-
}
69-
70-
/**
71-
* saves all policy rules to the storage.
72-
*
73-
* @param Model $model
74-
*/
75-
public function savePolicy(Model $model): void
76-
{
77-
foreach ($model['p'] as $ptype => $ast) {
78-
foreach ($ast->policy as $rule) {
79-
$this->savePolicyLine($ptype, $rule);
80-
}
65+
66+
/**
67+
* saves all policy rules to the storage.
68+
*
69+
* @param Model $model
70+
* @throws Exception
71+
* @throws Throwable
72+
*/
73+
public function savePolicy(Model $model): void
74+
{
75+
foreach ($model['p'] as $ptype => $ast) {
76+
foreach ($ast->policy as $rule) {
77+
$this->savePolicyLine($ptype, $rule);
78+
}
79+
}
80+
81+
foreach ($model['g'] as $ptype => $ast) {
82+
foreach ($ast->policy as $rule) {
83+
$this->savePolicyLine($ptype, $rule);
84+
}
85+
}
8186
}
8287

83-
foreach ($model['g'] as $ptype => $ast) {
84-
foreach ($ast->policy as $rule) {
88+
/**
89+
* adds a policy rule to the storage.
90+
* This is part of the Auto-Save feature.
91+
*
92+
* @param string $sec
93+
* @param string $ptype
94+
* @param array $rule
95+
* @throws Exception
96+
* @throws Throwable
97+
*/
98+
public function addPolicy(string $sec, string $ptype, array $rule): void
99+
{
85100
$this->savePolicyLine($ptype, $rule);
86-
}
87-
}
88-
}
89-
90-
/**
91-
* adds a policy rule to the storage.
92-
* This is part of the Auto-Save feature.
93-
*
94-
* @param string $sec
95-
* @param string $ptype
96-
* @param array $rule
97-
*/
98-
public function addPolicy(string $sec, string $ptype, array $rule): void
99-
{
100-
$this->savePolicyLine($ptype, $rule);
101-
}
102-
103-
/**
104-
* This is part of the Auto-Save feature.
105-
*
106-
* @param string $sec
107-
* @param string $ptype
108-
* @param array $rule
109-
*/
110-
public function removePolicy(string $sec, string $ptype, array $rule): void
111-
{
112-
$count = 0;
113-
114-
$instance = $this->eloquent->create()->get(['ptype' => $ptype]);
115-
116-
foreach ($rule as $key => $value) {
117-
$instance->where('v' . strval($key), $value);
118101
}
119102

120-
foreach ($instance->get() as $model) {
121-
if ($model->delete()) {
122-
++$count;
123-
}
124-
}
125-
}
126-
127-
/**
128-
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
129-
* This is part of the Auto-Save feature.
130-
*
131-
* @param string $sec
132-
* @param string $ptype
133-
* @param int $fieldIndex
134-
* @param string ...$fieldValues
135-
*/
136-
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
137-
{
138-
$count = 0;
139-
140-
$instance = $this->eloquent->create()->get(['ptype' => $ptype]);
141-
142-
foreach (range(0, 5) as $value) {
143-
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
144-
if ('' != $fieldValues[$value - $fieldIndex]) {
145-
$instance->where('v' . strval($value), $fieldValues[$value - $fieldIndex]);
103+
/**
104+
* This is part of the Auto-Save feature.
105+
*
106+
* @param string $sec
107+
* @param string $ptype
108+
* @param array $rule
109+
* @throws Exception
110+
* @throws Throwable
111+
*/
112+
public function removePolicy(string $sec, string $ptype, array $rule): void
113+
{
114+
$instance = RulesModel::create()->where(['ptype' => $ptype]);
115+
116+
foreach ($rule as $key => $value) {
117+
$instance->where('v' . strval($key), $value);
146118
}
147-
}
119+
120+
$instance->destroy();
148121
}
149122

150-
foreach ($instance->get() as $model) {
151-
if ($model->delete()) {
152-
++$count;
153-
}
123+
/**
124+
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
125+
* This is part of the Auto-Save feature.
126+
*
127+
* @param string $sec
128+
* @param string $ptype
129+
* @param int $fieldIndex
130+
* @param string ...$fieldValues
131+
* @throws Exception
132+
* @throws Throwable
133+
*/
134+
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
135+
{
136+
$instance = RulesModel::create()->where(['ptype' => $ptype]);
137+
138+
foreach (range(0, 5) as $value) {
139+
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
140+
if ('' != $fieldValues[$value - $fieldIndex]) {
141+
$instance->where('v' . strval($value), $fieldValues[$value - $fieldIndex]);
142+
}
143+
}
144+
}
145+
146+
$instance->destroy();
154147
}
155-
}
156148
}

0 commit comments

Comments
 (0)