|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EmilMoe\Guardian\Http\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use EmilMoe\Guardian\Http\Models\Role; |
|
6
|
|
|
use EmilMoe\Guardian\Support\Guardian; |
|
7
|
|
|
use EmilMoe\Guardian\Http\Models\Permission; |
|
8
|
|
|
|
|
9
|
|
|
use Illuminate\Support\Facades\DB; |
|
10
|
|
|
use Illuminate\Support\Collection; |
|
11
|
|
|
use Illuminate\Support\Facades\Auth; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
|
13
|
|
|
|
|
14
|
|
|
trait WithPermissions |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Get all roles the user is attached to. |
|
18
|
|
|
* |
|
19
|
|
|
* @return EloquentCollection |
|
20
|
|
|
*/ |
|
21
|
|
|
public function roles() |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->belongsToMany(Role::class, Guardian::getUsersRolesTable()); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get all permissions the user has access to through roles. |
|
28
|
|
|
* |
|
29
|
|
|
* @return Collection |
|
30
|
|
|
*/ |
|
31
|
|
|
public function permissions() |
|
32
|
|
|
{ |
|
33
|
|
|
$p = collect([]); |
|
34
|
|
|
|
|
35
|
|
|
foreach ($this->roles()->get() as $role) |
|
36
|
|
|
$p = $p->merge($role->permissions()->get()); |
|
37
|
|
|
|
|
38
|
|
|
return $p->unique(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Check whether the user has access to a specific permission. |
|
43
|
|
|
* If ID is provided a check for local access and inherit access |
|
44
|
|
|
* will be performed automatically too. |
|
45
|
|
|
* |
|
46
|
|
|
* @param $permission |
|
47
|
|
|
* @param null $id |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
|
|
public function hasAccess($permission, $id = null) |
|
51
|
|
|
{ |
|
52
|
|
|
if ($this->hasGlobalAccess($permission)) |
|
53
|
|
|
return true; |
|
54
|
|
|
|
|
55
|
|
|
if ($this->hasLocalAccess($permission, $id)) |
|
56
|
|
|
return true; |
|
57
|
|
|
|
|
58
|
|
|
if ($this->hasInheritAccess($permission, $id)) |
|
59
|
|
|
return true; |
|
60
|
|
|
|
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Check whether the user has access to a permission at any |
|
66
|
|
|
* given point. If user does not have global access but |
|
67
|
|
|
* local access in 1 or more occurrences the hasAccessAny |
|
68
|
|
|
* will return true. |
|
69
|
|
|
* |
|
70
|
|
|
* @param $permission |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function hasAccessAny($permission) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($this->hasAccess($permission)) |
|
76
|
|
|
return true; |
|
77
|
|
|
|
|
78
|
|
|
$permission = Permission::where('name', $permission); |
|
79
|
|
|
|
|
80
|
|
|
if ($permission->count() == 0 || $permission->first()->table == null) |
|
81
|
|
|
return false; |
|
82
|
|
|
|
|
83
|
|
|
return DB::table(Permission::where('name', $permission)->first()->table) |
|
84
|
|
|
->where($permission->first()->user_id_column, Auth::id()) |
|
85
|
|
|
->where('is_privileged', true) |
|
86
|
|
|
->count() > 0; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Determines whether the user has global access to the |
|
91
|
|
|
* given permission. |
|
92
|
|
|
* |
|
93
|
|
|
* @param $permission |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
private function hasGlobalAccess($permission) |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->permissions()->contains('name', $permission); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Determines whether the user has local access to the |
|
103
|
|
|
* permission with the given ID. |
|
104
|
|
|
* |
|
105
|
|
|
* @param $permission |
|
106
|
|
|
* @param $id |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
private function hasLocalAccess($permission, $id) |
|
110
|
|
|
{ |
|
111
|
|
|
if (! $id) |
|
112
|
|
|
return false; |
|
113
|
|
|
|
|
114
|
|
|
$permission = Permission::where('name', $permission); |
|
115
|
|
|
|
|
116
|
|
|
if ($permission->count() == 0 || $permission->first()->table == null) |
|
117
|
|
|
return false; |
|
118
|
|
|
|
|
119
|
|
|
if (DB::table($permission->first()->table) |
|
120
|
|
|
->where($permission->first()->user_id_column, Auth::id()) |
|
121
|
|
|
->where($permission->first()->foreign_id_column, $id) |
|
122
|
|
|
->where('is_privileged', true) |
|
123
|
|
|
->count() > 0) |
|
124
|
|
|
return true; |
|
125
|
|
|
|
|
126
|
|
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Determining whether the user is granted access to a |
|
131
|
|
|
* permission indirectly through inheritance where a |
|
132
|
|
|
* superior permission grants access down a tree |
|
133
|
|
|
* structure. |
|
134
|
|
|
* |
|
135
|
|
|
* @param $permission |
|
136
|
|
|
* @param $id |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
|
|
private function hasInheritAccess($permission, $id) |
|
140
|
|
|
{ |
|
141
|
|
|
if (! $id) |
|
142
|
|
|
return false; |
|
143
|
|
|
|
|
144
|
|
|
if (method_exists(Auth::user(), $permission .'Privilege')) |
|
145
|
|
|
return Auth::user()->{$permission .'Privilege'}($id); |
|
146
|
|
|
|
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.