-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
LaratrustRoleTrait.php
245 lines (208 loc) · 6.59 KB
/
LaratrustRoleTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace Laratrust\Traits;
/**
* This file is part of Laratrust,
* a role & permission management solution for Laravel.
*
* @license MIT
* @package Laratrust
*/
use Laratrust\Helper;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
trait LaratrustRoleTrait
{
use LaratrustDynamicUserRelationsCalls;
use LaratrustHasEvents;
/**
* Boots the role model and attaches event listener to
* remove the many-to-many records when trying to delete.
* Will NOT delete any records if the role model uses soft deletes.
*
* @return void|bool
*/
public static function bootLaratrustRoleTrait()
{
$flushCache = function ($role) {
$role->flushCache();
};
// If the role doesn't use SoftDeletes.
if (method_exists(static::class, 'restored')) {
static::restored($flushCache);
}
static::deleted($flushCache);
static::saved($flushCache);
static::deleting(function ($role) {
if (method_exists($role, 'bootSoftDeletes') && $role->forceDeleting) {
return;
}
$role->permissions()->sync([]);
foreach (array_keys(Config::get('laratrust.user_models')) as $key) {
$role->$key()->sync([]);
}
});
}
/**
* Tries to return all the cached permissions of the role.
* If it can't bring the permissions from the cache,
* it brings them back from the DB.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function cachedPermissions()
{
$cacheKey = 'laratrust_permissions_for_role_' . $this->getKey();
if (! Config::get('laratrust.use_cache')) {
return $this->permissions()->get();
}
return Cache::remember($cacheKey, Config::get('cache.ttl', 60), function () {
return $this->permissions()->get()->toArray();
});
}
/**
* Morph by Many relationship between the role and the one of the possible user models.
*
* @param string $relationship
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function getMorphByUserRelation($relationship)
{
return $this->morphedByMany(
Config::get('laratrust.user_models')[$relationship],
'user',
Config::get('laratrust.tables.role_user'),
Config::get('laratrust.foreign_keys.role'),
Config::get('laratrust.foreign_keys.user')
);
}
/**
* Many-to-Many relations with the permission model.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function permissions()
{
return $this->belongsToMany(
Config::get('laratrust.models.permission'),
Config::get('laratrust.tables.permission_role'),
Config::get('laratrust.foreign_keys.role'),
Config::get('laratrust.foreign_keys.permission')
);
}
/**
* Checks if the role has a permission by its name.
*
* @param string|array $permission Permission name or array of permission names.
* @param bool $requireAll All permissions in the array are required.
* @return bool
*/
public function hasPermission($permission, $requireAll = false)
{
if (is_array($permission)) {
if (empty($permission)) {
return true;
}
foreach ($permission as $permissionName) {
$hasPermission = $this->hasPermission($permissionName);
if ($hasPermission && !$requireAll) {
return true;
} elseif (!$hasPermission && $requireAll) {
return false;
}
}
// If we've made it this far and $requireAll is FALSE, then NONE of the permissions were found.
// If we've made it this far and $requireAll is TRUE, then ALL of the permissions were found.
// Return the value of $requireAll.
return $requireAll;
}
foreach ($this->cachedPermissions() as $perm) {
if (str_is($permission, $perm['name'])) {
return true;
}
}
return false;
}
/**
* Save the inputted permissions.
*
* @param mixed $permissions
* @return array
*/
public function syncPermissions($permissions)
{
$mappedPermissions = [];
foreach ($permissions as $permission) {
$mappedPermissions[] = Helper::getIdFor($permission, 'permission');
}
$changes = $this->permissions()->sync($mappedPermissions);
$this->flushCache();
$this->fireLaratrustEvent("permission.synced", [$this, $changes]);
return $this;
}
/**
* Attach permission to current role.
*
* @param object|array $permission
* @return void
*/
public function attachPermission($permission)
{
$permission = Helper::getIdFor($permission, 'permission');
$this->permissions()->attach($permission);
$this->flushCache();
$this->fireLaratrustEvent("permission.attached", [$this, $permission]);
return $this;
}
/**
* Detach permission from current role.
*
* @param object|array $permission
* @return void
*/
public function detachPermission($permission)
{
$permission = Helper::getIdFor($permission, 'permission');
$this->permissions()->detach($permission);
$this->flushCache();
$this->fireLaratrustEvent("permission.detached", [$this, $permission]);
return $this;
}
/**
* Attach multiple permissions to current role.
*
* @param mixed $permissions
* @return void
*/
public function attachPermissions($permissions)
{
foreach ($permissions as $permission) {
$this->attachPermission($permission);
}
return $this;
}
/**
* Detach multiple permissions from current role
*
* @param mixed $permissions
* @return void
*/
public function detachPermissions($permissions = null)
{
if (!$permissions) {
$permissions = $this->permissions()->get();
}
foreach ($permissions as $permission) {
$this->detachPermission($permission);
}
return $this;
}
/**
* Flush the role's cache.
*
* @return void
*/
public function flushCache()
{
Cache::forget('laratrust_permissions_for_role_' . $this->getKey());
}
}