|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author enea dhack <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Enea\Authorization\Drivers\Cache\Repositories; |
|
13
|
|
|
|
|
14
|
|
|
use Closure; |
|
15
|
|
|
use Enea\Authorization\Contracts\DeniableOwner; |
|
16
|
|
|
use Enea\Authorization\Contracts\Grantable; |
|
17
|
|
|
use Enea\Authorization\Contracts\PermissionsOwner; |
|
18
|
|
|
use Enea\Authorization\Contracts\RolesOwner; |
|
19
|
|
|
use Enea\Authorization\Models\Permission; |
|
20
|
|
|
use Illuminate\Support\Collection; |
|
21
|
|
|
|
|
22
|
|
|
class PermissionRepository extends Repository |
|
23
|
|
|
{ |
|
24
|
36 |
|
public static function getSuffix(): string |
|
25
|
|
|
{ |
|
26
|
36 |
|
return 'permissions'; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function toCollection(PermissionsOwner $owner): Collection |
|
30
|
|
|
{ |
|
31
|
26 |
|
return $this->remember($owner, function () use ($owner) { |
|
32
|
26 |
|
return $this->permissions($owner); |
|
33
|
26 |
|
}); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
26 |
|
private function permissions(PermissionsOwner $owner): Collection |
|
37
|
|
|
{ |
|
38
|
26 |
|
$permissions = $owner->permissions()->get(); |
|
39
|
|
|
|
|
40
|
26 |
|
if ($owner instanceof RolesOwner) { |
|
41
|
21 |
|
$permissions = $this->permissionsFromRole($owner)->merge($permissions); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
26 |
|
if ($owner instanceof DeniableOwner) { |
|
45
|
21 |
|
$permissions = $permissions->filter($this->excludeDeniedPermissions($owner)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
26 |
|
return $permissions->map($this->parse()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function permissionsFromRole(RolesOwner $owner): Collection |
|
52
|
|
|
{ |
|
53
|
21 |
|
return $this->extractPermissions($owner)->unique(function (Grantable $grantable) { |
|
54
|
3 |
|
return $grantable->getIdentificationKey(); |
|
55
|
21 |
|
}); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function extractPermissions(RolesOwner $owner): Collection |
|
59
|
|
|
{ |
|
60
|
21 |
|
return $owner->roles()->with('permissions')->get()->map(function (PermissionsOwner $owner): Collection { |
|
61
|
3 |
|
return $owner->getPermissionModels(); |
|
62
|
21 |
|
})->collapse(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
21 |
|
private function excludeDeniedPermissions(DeniableOwner $owner): Closure |
|
66
|
|
|
{ |
|
67
|
21 |
|
$denied = $this->deniedPermissions($owner); |
|
68
|
21 |
|
return function (Permission $permission) use ($denied) { |
|
69
|
16 |
|
return ! in_array($permission->getIdentificationKey(), $denied->pluck('id')->toArray()); |
|
70
|
21 |
|
}; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
21 |
|
protected function deniedPermissions(DeniableOwner $owner): Collection |
|
74
|
|
|
{ |
|
75
|
21 |
|
return $owner->denied()->select('permissions.id')->get(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|