1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Platfourm package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\User\Models\Eloquent\Traits; |
12
|
|
|
|
13
|
|
|
use Cache; |
14
|
|
|
use Config; |
15
|
|
|
|
16
|
|
|
trait RoleTrait |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
View Code Duplication |
public function cachedPermissions() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$cacheKey = 'entrust_permissions_for_role_' . $this->getKey(); |
|
|
|
|
22
|
|
|
return Cache::/*tags(Config::get('entrust.permission_role_table'))->*/ |
|
|
|
|
23
|
|
|
remember($cacheKey, Config::get('cache.ttl'), function () { |
24
|
|
|
return $this->perms()->get(); |
25
|
|
|
}); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public static function bootEntrustRoleTrait() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
static::saved(function ($item) { |
31
|
|
|
//Cache::tags(Config::get('entrust.permission_role_table'))->flush(); |
|
|
|
|
32
|
|
|
Cache::forget('entrust_permissions_for_role_' . $item->getKey()); |
33
|
|
|
}); |
34
|
|
|
static::deleted(function ($item) { |
35
|
|
|
//Cache::tags(Config::get('entrust.permission_role_table'))->flush(); |
|
|
|
|
36
|
|
|
Cache::forget('entrust_permissions_for_role_' . $item->getKey()); |
37
|
|
|
}); |
38
|
|
|
if (method_exists(static::class, 'restored')) { |
39
|
|
|
static::restored(function ($item) { |
40
|
|
|
//Cache::tags(Config::get('entrust.permission_role_table'))->flush(); |
|
|
|
|
41
|
|
|
Cache::forget('entrust_permissions_for_role_' . $item->getKey()); |
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Many-to-Many relations with the user model. |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
50
|
|
|
*/ |
51
|
|
|
public function users() |
52
|
|
|
{ |
53
|
|
|
return $this->hasMany(Config::get('auth.model')); |
|
|
|
|
54
|
|
|
// return $this->belongsToMany(Config::get('auth.model'), Config::get('entrust.role_user_table')); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Many-to-Many relations with the permission model. |
59
|
|
|
* Named "perms" for backwards compatibility. Also because "perms" is short and sweet. |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
62
|
|
|
*/ |
63
|
|
|
public function perms() |
64
|
|
|
{ |
65
|
|
|
return $this->belongsToMany(Config::get('entrust.permission'), Config::get('entrust.permission_role_table')); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Boot the role model |
70
|
|
|
* Attach event listener to remove the many-to-many records when trying to delete |
71
|
|
|
* Will NOT delete any records if the role model uses soft deletes. |
72
|
|
|
* |
73
|
|
|
* @return void|bool |
74
|
|
|
*/ |
75
|
|
|
public static function boot() |
76
|
|
|
{ |
77
|
|
|
parent::boot(); |
78
|
|
|
|
79
|
|
|
static::deleting(function ($role) { |
80
|
|
|
if (!method_exists(Config::get('entrust.role'), 'bootSoftDeletes')) { |
81
|
|
|
$role->users()->sync([]); |
82
|
|
|
$role->perms()->sync([]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Checks if the role has a permission by its name. |
91
|
|
|
* |
92
|
|
|
* @param string|array $name Permission name or array of permission names. |
93
|
|
|
* @param bool $requireAll All permissions in the array are required. |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function hasPermission($name, $requireAll = false) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if (is_array($name)) { |
99
|
|
|
foreach ($name as $permissionName) { |
100
|
|
|
$hasPermission = $this->hasPermission($permissionName); |
101
|
|
|
|
102
|
|
|
if ($hasPermission && !$requireAll) { |
103
|
|
|
return true; |
104
|
|
|
} elseif (!$hasPermission && $requireAll) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// If we've made it this far and $requireAll is FALSE, then NONE of the permissions were found |
110
|
|
|
// If we've made it this far and $requireAll is TRUE, then ALL of the permissions were found. |
111
|
|
|
// Return the value of $requireAll; |
112
|
|
|
return $requireAll; |
113
|
|
|
} else { |
114
|
|
|
foreach ($this->cachedPermissions() as $permission) { |
115
|
|
|
if ($permission->name == $name) { |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Save the inputted permissions. |
126
|
|
|
* |
127
|
|
|
* @param mixed $inputPermissions |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function savePermissions($inputPermissions) |
131
|
|
|
{ |
132
|
|
|
if (!empty($inputPermissions)) { |
133
|
|
|
$this->perms()->sync($inputPermissions); |
134
|
|
|
} else { |
135
|
|
|
$this->perms()->detach(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Attach permission to current role. |
141
|
|
|
* |
142
|
|
|
* @param object|array $permission |
143
|
|
|
* @return void |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public function attachPermission($permission) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
if (is_object($permission)) { |
148
|
|
|
$permission = $permission->getKey(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (is_array($permission)) { |
152
|
|
|
$permission = $permission['id']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->perms()->attach($permission); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Detach permission from current role. |
160
|
|
|
* |
161
|
|
|
* @param object|array $permission |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
public function detachPermission($permission) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
if (is_object($permission)) { |
167
|
|
|
$permission = $permission->getKey(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (is_array($permission)) { |
171
|
|
|
$permission = $permission['id']; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->perms()->detach($permission); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Attach multiple permissions to current role. |
179
|
|
|
* |
180
|
|
|
* @param mixed $permissions |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
public function attachPermissions($permissions) |
184
|
|
|
{ |
185
|
|
|
foreach ($permissions as $permission) { |
186
|
|
|
$this->attachPermission($permission); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Detach multiple permissions from current role |
192
|
|
|
* |
193
|
|
|
* @param mixed $permissions |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
public function detachPermissions($permissions) |
197
|
|
|
{ |
198
|
|
|
foreach ($permissions as $permission) { |
199
|
|
|
$this->detachPermission($permission); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.