Skip to content

Commit 966c990

Browse files
authored
Merge pull request #9 from lyt8384/main
多套配置共存及针对Laravel ORM的优化
2 parents dac242a + 7118449 commit 966c990

File tree

27 files changed

+689
-183
lines changed

27 files changed

+689
-183
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ vendor
33
.idea
44
.vscode
55
.phpunit*
6-
composer.lock
6+
composer.lock
7+
.DS_Store

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ if (Permission::enforce("eve", "articles", "edit")) {
109109
}
110110
```
111111

112+
多套配置
113+
```php
114+
$permission = Permission::client("other_conf")
115+
// adds permissions to a user
116+
$permission->addPermissionForUser('eve', 'articles', 'read');
117+
// adds a role for a user.
118+
$permission->addRoleForUser('eve', 'writer');
119+
// adds permissions to a rule
120+
$permission->addPolicy('writer', 'articles','edit');
121+
122+
if ($permission->enforce("eve", "articles", "edit")) {
123+
echo '恭喜你!通过权限认证';
124+
} else {
125+
echo '对不起,您没有该资源访问权限';
126+
}
127+
```
128+
112129
更多 `API` 参考 [Casbin API](https://casbin.org/docs/en/management-api)
113130

114131
## 感谢
@@ -140,4 +157,4 @@ if (is_null(static::$_manager)) {
140157
}
141158
```
142159
耦合太高,不建议这么搞,更多了解:https://www.workerman.net/doc/webman/di.html
143-
</details>
160+
</details>

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"casbin/casbin": "^3.20",
2626
"topthink/think-orm": "^2.0",
2727
"php-di/php-di": "^6.3",
28+
"doctrine/annotations": "^1.13",
2829
"workerman/redis": "^1.0"
2930
},
3031
"autoload": {
@@ -41,7 +42,12 @@
4142
"phpunit/phpunit": "~7.0|~8.0|~9.0",
4243
"php-coveralls/php-coveralls": "^2.1",
4344
"workerman/webman": "^1.0",
45+
"vlucas/phpdotenv": "^5.5",
46+
"psr/container": "^1.1.1",
4447
"illuminate/database": "^8.83",
45-
"illuminate/cache": "^8.83"
48+
"illuminate/pagination": "^8.83",
49+
"illuminate/events": "^8.83",
50+
"symfony/var-dumper": "^6.2",
51+
"webman/think-orm": "^1.0"
4652
}
4753
}

src/Adapter/DatabaseAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter, Filter
4545
/**
4646
* the DatabaseAdapter constructor.
4747
*
48-
* @param RuleModel $model
48+
* @param string|null $driver
4949
*/
50-
public function __construct(RuleModel $model)
50+
public function __construct(?string $driver = null)
5151
{
52-
$this->model = $model;
52+
$this->model = new RuleModel([], $driver);
5353
}
5454

5555
/**
@@ -371,4 +371,4 @@ public function loadFilteredPolicy(Model $model, $filter): void
371371
}
372372
$this->setFiltered(true);
373373
}
374-
}
374+
}

src/Adapter/LaravelDatabaseAdapter.php

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Casbin\Persist\Adapters\Filter;
2020
use Casbin\Exceptions\InvalidFilterTypeException;
2121
use Casbin\WebmanPermission\Model\LaravelRuleModel;
22+
use Illuminate\Database\Eloquent\Builder;
23+
use Illuminate\Database\Eloquent\Collection;
2224
use Illuminate\Support\Facades\DB;
2325
use Throwable;
2426

@@ -45,11 +47,12 @@ class LaravelDatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter,
4547

4648
/**
4749
* LaravelDatabaseAdapter constructor.
48-
* @param LaravelRuleModel $model
50+
*
51+
* @param string|null $driver
4952
*/
50-
public function __construct(LaravelRuleModel $model)
53+
public function __construct(?string $driver = null)
5154
{
52-
$this->model = $model;
55+
$this->model = new LaravelRuleModel([],$driver);
5356
}
5457

5558
/**
@@ -84,9 +87,9 @@ public function savePolicyLine(string $ptype, array $rule)
8487
{
8588
$col['ptype'] = $ptype;
8689
foreach ($rule as $key => $value) {
87-
$col['v' . strval($key) . ''] = $value;
90+
$col['v' . $key] = $value;
8891
}
89-
$this->model->create($col);
92+
$this->model->updateOrCreate($col);
9093
}
9194

9295
/**
@@ -96,7 +99,7 @@ public function savePolicyLine(string $ptype, array $rule)
9699
*/
97100
public function loadPolicy(Model $model): void
98101
{
99-
$rows = $this->model->getAllFromCache();
102+
$rows = $this->model->select(['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5'])->get()->toArray();;
100103
foreach ($rows as $row) {
101104
$this->loadPolicyArray($this->filterRule($row), $model);
102105
}
@@ -145,21 +148,13 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
145148
*/
146149
public function addPolicies(string $sec, string $ptype, array $rules): void
147150
{
148-
$cols = [];
149-
$i = 0;
150-
151151
foreach ($rules as $rule) {
152-
$temp['ptype'] = $ptype;
153-
$temp['created_at'] = date("Y-m-d h:m:i");
154-
$temp['updated_at'] = $temp['created_at'];
152+
$temp = ['ptype' => $ptype];
155153
foreach ($rule as $key => $value) {
156-
$temp['v' . strval($key)] = $value;
154+
$temp['v' . $key] = $value;
157155
}
158-
$cols[$i++] = $temp ?? [];
159-
$temp = [];
156+
$this->model->updateOrCreate($temp);
160157
}
161-
$this->model->insert($cols);
162-
LaravelRuleModel::fireModelEvent('saved');
163158
}
164159

165160
/**
@@ -173,10 +168,12 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
173168
{
174169
$instance = $this->model->where('ptype', $ptype);
175170
foreach ($rule as $key => $value) {
176-
$instance->where('v' . strval($key), $value);
171+
$instance->where('v' . $key, $value);
172+
}
173+
$data = $instance->get();
174+
foreach ($data as $item) {
175+
$item->delete();
177176
}
178-
$instance->delete();
179-
LaravelRuleModel::fireModelEvent('deleted');
180177
}
181178

182179
/**
@@ -189,25 +186,13 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
189186
*/
190187
public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, ?string ...$fieldValues): array
191188
{
192-
$count = 0;
193189
$removedRules = [];
190+
$data = $this->getCollection($ptype, $fieldIndex, $fieldValues);
194191

195-
$instance = $this->model->where('ptype', $ptype);
196-
foreach (range(0, 5) as $value) {
197-
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
198-
if ('' != $fieldValues[$value - $fieldIndex]) {
199-
$instance->where('v' . strval($value), $fieldValues[$value - $fieldIndex]);
200-
}
201-
}
202-
}
203-
204-
foreach ($instance->select() as $model) {
205-
$item = $model->hidden(['id', 'ptype'])->toArray();
206-
$item = $this->filterRule($item);
192+
foreach ($data as $model) {
193+
$item = $model->hidden(['id', 'ptype'])->toArray();
194+
$item = $this->filterRule($item);
207195
$removedRules[] = $item;
208-
if ($model->cache('tauthz')->delete()) {
209-
++$count;
210-
}
211196
}
212197

213198
return $removedRules;
@@ -241,22 +226,10 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
241226
*/
242227
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
243228
{
244-
$instance = $this->model->where('ptype', $ptype);
245-
foreach (range(0, 5) as $value) {
246-
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
247-
if ('' != $fieldValues[$value - $fieldIndex]) {
248-
$instance->where('v' . strval($value), $fieldValues[$value - $fieldIndex]);
249-
}
250-
}
251-
}
252-
253-
$oldP = $instance->get()->makeHidden(['created_at','updated_at', 'id', 'ptype'])->toArray();
254-
foreach ($oldP as &$item) {
255-
$item = $this->filterRule($item);
256-
$removedRules[] = $item;
229+
$data = $this->getCollection($ptype, $fieldIndex, $fieldValues);
230+
foreach ($data as $item) {
231+
$item->delete();
257232
}
258-
$instance->delete();
259-
LaravelRuleModel::fireModelEvent('deleted');
260233
}
261234

262235
/**
@@ -272,7 +245,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
272245
{
273246
$instance = $this->model->where('ptype', $ptype);
274247
foreach ($oldRule as $key => $value) {
275-
$instance->where('v' . strval($key), $value);
248+
$instance->where('v' . $key, $value);
276249
}
277250
$instance = $instance->first();
278251

@@ -281,8 +254,8 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
281254
$update['v' . $key] = $value;
282255
}
283256

284-
$instance->update($update);
285-
LaravelRuleModel::fireModelEvent('saved');
257+
$instance->fill($update);
258+
$instance->save();
286259
}
287260

288261
/**
@@ -316,7 +289,7 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
316289
public function updateFilteredPolicies(string $sec, string $ptype, array $newPolicies, int $fieldIndex, string ...$fieldValues): array
317290
{
318291
$oldRules = [];
319-
\Illuminate\Support\Facades\DB::transaction(function () use ($sec, $ptype, $fieldIndex, $fieldValues, $newPolicies, &$oldRules) {
292+
DB::transaction(function () use ($sec, $ptype, $fieldIndex, $fieldValues, $newPolicies, &$oldRules) {
320293
$oldRules = $this->_removeFilteredPolicy($sec, $ptype, $fieldIndex, ...$fieldValues);
321294
$this->addPolicies($sec, $ptype, $newPolicies);
322295
});
@@ -346,34 +319,67 @@ public function setFiltered(bool $filtered): void
346319
/**
347320
* Loads only policy rules that match the filter.
348321
*
349-
* @param Model $model
350-
* @param mixed $filter
322+
* @param Model $model
323+
* @param mixed $filter
324+
*
325+
* @throws InvalidFilterTypeException
351326
*/
352327
public function loadFilteredPolicy(Model $model, $filter): void
353328
{
354329
$instance = $this->model;
355330
if (is_string($filter)) {
356-
$instance = $instance->whereRaw($filter);
357-
} elseif ($filter instanceof Filter) {
331+
$instance->whereRaw($filter);
332+
}
333+
elseif ($filter instanceof Filter) {
334+
$where = [];
358335
foreach ($filter->p as $k => $v) {
359336
$where[$v] = $filter->g[$k];
360-
$instance = $instance->where($v, $filter->g[$k]);
361337
}
362-
} elseif ($filter instanceof \Closure) {
338+
$instance->where($where);
339+
}
340+
elseif ($filter instanceof Closure) {
363341
$instance = $instance->where($filter);
364-
} else {
342+
}
343+
else {
365344
throw new InvalidFilterTypeException('invalid filter type');
366345
}
367-
$rows = $instance->get()->makeHidden(['created_at','updated_at', 'id'])->toArray();
346+
$rows = $instance->get()->makeHidden(['created_at', 'updated_at', 'id'])->toArray();
368347
if ($rows) {
369348
foreach ($rows as $row) {
370-
$row = array_filter($row, function($value) { return !is_null($value) && $value !== ''; });
371-
$line = implode(', ', array_filter($row, function ($val) {
372-
return '' != $val && !is_null($val);
373-
}));
349+
$row = array_filter($row, function ($value) {
350+
return !is_null($value) && $value !== '';
351+
});
352+
$line = implode(
353+
', ',
354+
array_filter($row, function ($val) {
355+
return '' != $val && !is_null($val);
356+
})
357+
);
374358
$this->loadPolicyLine(trim($line), $model);
375359
}
376360
}
377361
$this->setFiltered(true);
378362
}
363+
364+
/**
365+
* @param string $ptype
366+
* @param int $fieldIndex
367+
* @param array $fieldValues
368+
*
369+
* @return Builder[]|Collection
370+
*/
371+
protected function getCollection(string $ptype, int $fieldIndex, array $fieldValues) {
372+
$where = [
373+
'ptype' => $ptype,
374+
];
375+
foreach (range(0, 5) as $value) {
376+
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
377+
if ('' != $fieldValues[$value - $fieldIndex]) {
378+
$where['v' . $value] = $fieldValues[$value - $fieldIndex];
379+
}
380+
}
381+
}
382+
383+
return $this->model->where($where)->get();
384+
}
379385
}

0 commit comments

Comments
 (0)