1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Casbin; |
6
|
|
|
|
7
|
|
|
use Casbin\Exceptions\NotImplementedException; |
8
|
|
|
use Casbin\Persist\BatchAdapter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait InternalApi. |
12
|
|
|
* |
13
|
|
|
* @author [email protected] |
|
|
|
|
14
|
|
|
*/ |
|
|
|
|
15
|
|
|
trait InternalApi |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* adds a rule to the current policy. |
|
|
|
|
19
|
|
|
* |
20
|
|
|
* @param string $sec |
|
|
|
|
21
|
|
|
* @param string $ptype |
|
|
|
|
22
|
|
|
* @param array $rule |
|
|
|
|
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
27 |
|
protected function addPolicyInternal(string $sec, string $ptype, array $rule): bool |
27
|
|
|
{ |
28
|
27 |
|
if ($this->model->hasPolicy($sec, $ptype, $rule)) { |
29
|
3 |
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
27 |
|
$this->model->addPolicy($sec, $ptype, $rule); |
33
|
|
|
|
34
|
27 |
|
if ($this->ShouldPersist()) { |
35
|
|
|
try { |
36
|
24 |
|
$this->adapter->addPolicy($sec, $ptype, $rule); |
37
|
24 |
|
} catch (NotImplementedException $e) { |
|
|
|
|
38
|
|
|
} |
39
|
|
|
} |
40
|
27 |
|
$this->checkWatcher(); |
41
|
|
|
|
42
|
27 |
|
return true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* adds a rules to the current policy. |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @param string $sec |
|
|
|
|
49
|
|
|
* @param string $ptype |
|
|
|
|
50
|
|
|
* @param array $rules |
|
|
|
|
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
6 |
|
protected function addPoliciesInternal(string $sec, string $ptype, array $rules): bool |
55
|
|
|
{ |
56
|
6 |
|
if ($this->model->hasPolicies($sec, $ptype, $rules)) { |
57
|
6 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
$this->model->addPolicies($sec, $ptype, $rules); |
61
|
|
|
|
62
|
6 |
|
if ($this->ShouldPersist() && $this->adapter instanceof BatchAdapter) { |
63
|
|
|
try { |
64
|
6 |
|
$this->adapter->addPolicies($sec, $ptype, $rules); |
65
|
6 |
|
} catch (NotImplementedException $e) { |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
6 |
|
$this->checkWatcher(); |
69
|
|
|
|
70
|
6 |
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* removes a rule from the current policy. |
|
|
|
|
75
|
|
|
* |
76
|
|
|
* @param string $sec |
|
|
|
|
77
|
|
|
* @param string $ptype |
|
|
|
|
78
|
|
|
* @param array $rule |
|
|
|
|
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
27 |
|
protected function removePolicyInternal(string $sec, string $ptype, array $rule): bool |
83
|
|
|
{ |
84
|
27 |
|
$ruleRemoved = $this->model->removePolicy($sec, $ptype, $rule); |
85
|
27 |
|
if (!$ruleRemoved) { |
86
|
3 |
|
return $ruleRemoved; |
87
|
|
|
} |
88
|
|
|
|
89
|
27 |
|
if ($this->ShouldPersist()) { |
90
|
|
|
try { |
91
|
27 |
|
$this->adapter->removePolicy($sec, $ptype, $rule); |
92
|
27 |
|
} catch (NotImplementedException $e) { |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
27 |
|
$this->checkWatcher(); |
96
|
|
|
|
97
|
27 |
|
return $ruleRemoved; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* removes a rules from the current policy. |
|
|
|
|
102
|
|
|
* |
103
|
|
|
* @param string $sec |
|
|
|
|
104
|
|
|
* @param string $ptype |
|
|
|
|
105
|
|
|
* @param array $rules |
|
|
|
|
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
6 |
|
protected function removePoliciesInternal(string $sec, string $ptype, array $rules): bool |
110
|
|
|
{ |
111
|
6 |
|
$ruleRemoved = $this->model->removePolicies($sec, $ptype, $rules); |
112
|
6 |
|
if (!$ruleRemoved) { |
113
|
6 |
|
return $ruleRemoved; |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
if ($this->ShouldPersist() && $this->adapter instanceof BatchAdapter) { |
117
|
|
|
try { |
118
|
6 |
|
$this->adapter->removePolicies($sec, $ptype, $rules); |
119
|
6 |
|
} catch (NotImplementedException $e) { |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
6 |
|
$this->checkWatcher(); |
123
|
|
|
|
124
|
6 |
|
return $ruleRemoved; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* removes rules based on field filters from the current policy. |
|
|
|
|
129
|
|
|
* |
130
|
|
|
* @param string $sec |
|
|
|
|
131
|
|
|
* @param string $ptype |
|
|
|
|
132
|
|
|
* @param int $fieldIndex |
|
|
|
|
133
|
|
|
* @param string ...$fieldValues |
|
|
|
|
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
24 |
|
protected function removeFilteredPolicyInternal(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): bool |
138
|
|
|
{ |
139
|
24 |
|
$ruleRemoved = $this->model->removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues); |
140
|
24 |
|
if (!$ruleRemoved) { |
141
|
|
|
return $ruleRemoved; |
142
|
|
|
} |
143
|
|
|
|
144
|
24 |
|
if ($this->ShouldPersist()) { |
145
|
|
|
try { |
146
|
24 |
|
$this->adapter->removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues); |
147
|
24 |
|
} catch (NotImplementedException $e) { |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
24 |
|
$this->checkWatcher(); |
151
|
|
|
|
152
|
24 |
|
return $ruleRemoved; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* check $this->watcher ans $this->autoNotifyWatcher. |
|
|
|
|
157
|
|
|
*/ |
|
|
|
|
158
|
51 |
|
private function checkWatcher(): void |
|
|
|
|
159
|
|
|
{ |
160
|
51 |
|
if (!is_null($this->watcher) && $this->autoNotifyWatcher) { |
161
|
|
|
// error intentionally ignored |
162
|
|
|
$this->watcher->update(); |
163
|
|
|
} |
164
|
51 |
|
} |
165
|
|
|
|
166
|
51 |
|
private function ShouldPersist(): bool |
|
|
|
|
167
|
|
|
{ |
168
|
51 |
|
return !is_null($this->adapter) && $this->autoNotifyWatcher; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|