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\Traits; |
13
|
|
|
|
14
|
|
|
use Closure; |
15
|
|
|
use Enea\Authorization\Contracts\Grantable; |
|
|
|
|
16
|
|
|
use Enea\Authorization\Contracts\PermissionContract; |
17
|
|
|
use Enea\Authorization\Contracts\RoleContract; |
18
|
|
|
use Enea\Authorization\Facades\Authorizer; |
19
|
|
|
use Enea\Authorization\Facades\Denier; |
20
|
|
|
use Enea\Authorization\Facades\Granter; |
21
|
|
|
use Enea\Authorization\Facades\Revoker; |
22
|
|
|
use Enea\Authorization\Models\UserPermission; |
23
|
|
|
use Enea\Authorization\Support\Config; |
24
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
25
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
26
|
|
|
use Illuminate\Support\Collection; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Trait Authorizable. |
30
|
|
|
* |
31
|
|
|
* @package Enea\Authorization\Traits |
32
|
|
|
* |
33
|
|
|
* @property EloquentCollection permissions |
34
|
|
|
* @property EloquentCollection roles |
35
|
|
|
*/ |
36
|
|
|
trait Authorizable |
37
|
|
|
{ |
38
|
|
|
use Model; |
39
|
|
|
|
40
|
57 |
|
public function getIdentificationKey(): string |
41
|
|
|
{ |
42
|
57 |
|
return (string) $this->getKey(); |
43
|
|
|
} |
44
|
|
|
|
45
|
28 |
|
public function grant(Grantable $grantable): void |
46
|
|
|
{ |
47
|
28 |
|
$this->grantMultiple([$grantable]); |
48
|
28 |
|
} |
49
|
|
|
|
50
|
45 |
|
public function grantMultiple(array $grantables): void |
51
|
|
|
{ |
52
|
45 |
|
$this->operateOn(RoleContract::class, function (Collection $roles) { |
53
|
23 |
|
Granter::roles($this, $roles); |
54
|
45 |
|
}, $grantables); |
55
|
|
|
|
56
|
45 |
|
$this->operateOn(PermissionContract::class, function (Collection $permissions) { |
57
|
24 |
|
Granter::permissions($this, $permissions); |
58
|
45 |
|
}, $grantables); |
59
|
45 |
|
} |
60
|
|
|
|
61
|
6 |
|
public function deny(PermissionContract $permission): void |
62
|
|
|
{ |
63
|
6 |
|
$this->denyMultiple([$permission]); |
64
|
6 |
|
} |
65
|
|
|
|
66
|
6 |
|
public function denyMultiple(array $permissions): void |
67
|
|
|
{ |
68
|
6 |
|
Denier::permissions($this, collect($permissions)); |
|
|
|
|
69
|
6 |
|
} |
70
|
|
|
|
71
|
8 |
|
public function revoke(Grantable $grantable): void |
72
|
|
|
{ |
73
|
8 |
|
$this->revokeMultiple([$grantable]); |
74
|
8 |
|
} |
75
|
|
|
|
76
|
10 |
|
public function revokeMultiple(array $grantables): void |
77
|
|
|
{ |
78
|
10 |
|
$this->operateOn(RoleContract::class, function (Collection $roles) { |
79
|
5 |
|
Revoker::roles($this, $roles); |
80
|
10 |
|
}, $grantables); |
81
|
|
|
|
82
|
10 |
|
$this->operateOn(PermissionContract::class, function (Collection $permissions) { |
83
|
5 |
|
Revoker::permissions($this, $permissions); |
84
|
10 |
|
}, $grantables); |
85
|
10 |
|
} |
86
|
|
|
|
87
|
9 |
|
public function can(string $permission): bool |
88
|
|
|
{ |
89
|
9 |
|
return Authorizer::can($this, $permission); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function cannot(string $permission): bool |
93
|
|
|
{ |
94
|
2 |
|
return ! $this->can($permission); |
95
|
|
|
} |
96
|
|
|
|
97
|
6 |
|
public function isMemberOf(string $role): bool |
98
|
|
|
{ |
99
|
6 |
|
return Authorizer::is($this, $role); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
public function isntMemberOf(string $role): bool |
103
|
|
|
{ |
104
|
2 |
|
return ! $this->isMemberOf($role); |
105
|
|
|
} |
106
|
|
|
|
107
|
37 |
|
public function permissions(): BelongsToMany |
108
|
|
|
{ |
109
|
37 |
|
$params = [Config::permissionModel(), 'authorizable', Config::userPermissionTableName()]; |
110
|
37 |
|
return $this->morphToMany(...$params)->using(UserPermission::class)->withPivot('denied'); |
111
|
|
|
} |
112
|
|
|
|
113
|
52 |
|
public function roles(): BelongsToMany |
114
|
|
|
{ |
115
|
52 |
|
return $this->morphToMany(Config::roleModel(), 'authorizable', Config::userRoleTableName()); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
public function getPermissionModels(): EloquentCollection |
119
|
|
|
{ |
120
|
1 |
|
return $this->permissions; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function getRoleModels(): EloquentCollection |
124
|
|
|
{ |
125
|
1 |
|
return $this->roles; |
126
|
|
|
} |
127
|
|
|
|
128
|
45 |
|
private function operateOn(string $contract, Closure $closure, array $grantables): void |
129
|
|
|
{ |
130
|
45 |
|
$collection = $this->filterOnly($contract)($grantables); |
131
|
45 |
|
if (! $collection->isEmpty()) { |
132
|
45 |
|
$closure($collection); |
133
|
|
|
} |
134
|
45 |
|
} |
135
|
|
|
|
136
|
45 |
|
private function filterOnly(string $abstract): Closure |
137
|
|
|
{ |
138
|
45 |
|
return function (array $grantables) use ($abstract): Collection { |
139
|
45 |
|
return collect($grantables)->filter(function (Grantable $grantable) use ($abstract) { |
140
|
45 |
|
return $grantable instanceof $abstract; |
141
|
45 |
|
}); |
142
|
45 |
|
}; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: