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