|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Created on 13/02/18 by enea dhack. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Enea\Authorization\Traits; |
|
10
|
|
|
|
|
11
|
|
|
use Closure; |
|
12
|
|
|
use Enea\Authorization\Contracts\Grantable; |
|
|
|
|
|
|
13
|
|
|
use Enea\Authorization\Contracts\PermissionContract; |
|
14
|
|
|
use Enea\Authorization\Contracts\RoleContract; |
|
15
|
|
|
use Enea\Authorization\Facades\Authorizer; |
|
16
|
|
|
use Enea\Authorization\Facades\Granter; |
|
17
|
|
|
use Enea\Authorization\Facades\Revoker; |
|
18
|
|
|
use Enea\Authorization\Support\Config; |
|
19
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
|
20
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
21
|
|
|
use Illuminate\Support\Collection; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Trait Authorizable. |
|
25
|
|
|
* |
|
26
|
|
|
* @package Enea\Authorization\Traits |
|
27
|
|
|
* |
|
28
|
|
|
* @property EloquentCollection permissions |
|
29
|
|
|
* @property EloquentCollection roles |
|
30
|
|
|
*/ |
|
31
|
|
|
trait Authorizable |
|
32
|
|
|
{ |
|
33
|
|
|
use Model; |
|
34
|
|
|
|
|
35
|
5 |
|
public function getIdentificationKey(): string |
|
36
|
|
|
{ |
|
37
|
5 |
|
return (string) $this->getKey(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
9 |
|
public function grant(Grantable $grantable): void |
|
41
|
|
|
{ |
|
42
|
9 |
|
$this->grantMultiple([$grantable]); |
|
43
|
9 |
|
} |
|
44
|
|
|
|
|
45
|
23 |
|
public function grantMultiple(array $grantables): void |
|
46
|
|
|
{ |
|
47
|
23 |
|
Granter::permissions($this, $this->filterPermissions($grantables)); |
|
48
|
23 |
|
Granter::roles($this, $this->filterRoles($grantables)); |
|
49
|
23 |
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
public function revoke(Grantable $grantable): void |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->revokeMultiple([$grantable]); |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
public function revokeMultiple(array $grantables): void |
|
57
|
|
|
{ |
|
58
|
4 |
|
Revoker::permissions($this, $this->filterPermissions($grantables)); |
|
59
|
4 |
|
Revoker::roles($this, $this->filterRoles($grantables)); |
|
60
|
4 |
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
public function can(string $permission): bool |
|
63
|
|
|
{ |
|
64
|
4 |
|
return Authorizer::can($this, $permission); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
public function cannot(string $permission): bool |
|
68
|
|
|
{ |
|
69
|
2 |
|
return ! $this->can($permission); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
4 |
|
public function isMemberOf(string $role): bool |
|
73
|
|
|
{ |
|
74
|
4 |
|
return Authorizer::is($this, $role); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
public function isntMemberOf(string $role): bool |
|
78
|
|
|
{ |
|
79
|
2 |
|
return ! $this->isMemberOf($role); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
29 |
|
public function permissions(): BelongsToMany |
|
83
|
|
|
{ |
|
84
|
29 |
|
return $this->morphToMany(Config::permissionModel(), 'authorizable', Config::userPermissionTableName()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
32 |
|
public function roles(): BelongsToMany |
|
88
|
|
|
{ |
|
89
|
32 |
|
return $this->morphToMany(Config::roleModel(), 'authorizable', Config::userRoleTableName()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public function getPermissionModels(): EloquentCollection |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->permissions; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
public function getRoleModels(): EloquentCollection |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->roles; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
23 |
|
private function filterPermissions(array $grantables): Collection |
|
103
|
|
|
{ |
|
104
|
23 |
|
return $this->filterOnly(PermissionContract::class)($grantables); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
23 |
|
private function filterRoles(array $grantables): Collection |
|
108
|
|
|
{ |
|
109
|
23 |
|
return $this->filterOnly(RoleContract::class)($grantables); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function filterOnly(string $abstract): Closure |
|
113
|
|
|
{ |
|
114
|
|
|
return function (array $grantables) use ($abstract): Collection { |
|
115
|
23 |
|
return collect($grantables)->filter(function (Grantable $grantable) use ($abstract) { |
|
116
|
23 |
|
return $grantable instanceof $abstract; |
|
117
|
23 |
|
}); |
|
118
|
23 |
|
}; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: