/
Role.php
226 lines (204 loc) · 5.94 KB
/
Role.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
<?php
/**
* Copyright (C) MUNPANEL
* This file is part of MUNPANEL System.
*
* Open-sourced under AGPL v3 License.
*/
use Illuminate\Cache\TaggableStore;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
//Big block of caching functionality.
public function cachedPermissions()
{
$rolePrimaryKey = $this->primaryKey;
$cacheKey = 'permissions_for_role_'.$this->$rolePrimaryKey;
if(Cache::getStore() instanceof TaggableStore) {
return Cache::tags('permission_role')->remember($cacheKey, Config::get('cache.ttl', 60), function () {
return $this->perms()->get();
});
}
else return $this->perms()->get();
}
public function save(array $options = [])
{ //both inserts and updates
if(!parent::save($options)){
return false;
}
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags('permission_role')->flush();
}
return true;
}
public function delete(array $options = [])
{ //soft or hard
if(!parent::delete($options)){
return false;
}
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags('permission_role')->flush();
}
return true;
}
public function restore()
{ //soft delete undo's
if(!parent::restore()){
return false;
}
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags('permission_role')->flush();
}
return true;
}
/**
* Many-to-Many relations with the user model.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users()
{
return $this->belongsToMany('App\Reg');
}
/**
* Many-to-Many relations with the permission model.
* Named "perms" for backwards compatibility. Also because "perms" is short and sweet.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function perms()
{
return $this->belongsToMany('App\Permission');
}
/**
* Boot the role model
* Attach 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 boot()
{
parent::boot();
static::deleting(function($role) {
if (!method_exists('App\Role', 'bootSoftDeletes')) {
$role->users()->sync([]);
$role->perms()->sync([]);
}
return true;
});
}
/**
* Checks if the role has a permission by its name.
*
* @param string|array $name Permission name or array of permission names.
* @param bool $requireAll All permissions in the array are required.
*
* @return bool
*/
public function hasPermission($name, $requireAll = false)
{
if (is_array($name)) {
foreach ($name 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;
} else {
foreach ($this->cachedPermissions() as $permission) {
if ($permission->name == $name) {
return true;
}
}
}
return false;
}
/**
* Save the inputted permissions.
*
* @param mixed $inputPermissions
*
* @return void
*/
public function savePermissions($inputPermissions)
{
if (!empty($inputPermissions)) {
$this->perms()->sync($inputPermissions);
} else {
$this->perms()->detach();
}
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags('permission_role')->flush();
}
}
/**
* Attach permission to current role.
*
* @param object|array $permission
*
* @return void
*/
public function attachPermission($permission)
{
if (is_object($permission)) {
$permission = $permission->getKey();
}
if (is_array($permission)) {
return $this->attachPermissions($permission);
}
$this->perms()->attach($permission);
}
/**
* Detach permission from current role.
*
* @param object|array $permission
*
* @return void
*/
public function detachPermission($permission)
{
if (is_object($permission)) {
$permission = $permission->getKey();
}
if (is_array($permission)) {
return $this->detachPermissions($permission);
}
$this->perms()->detach($permission);
}
/**
* Attach multiple permissions to current role.
*
* @param mixed $permissions
*
* @return void
*/
public function attachPermissions($permissions)
{
foreach ($permissions as $permission) {
$this->attachPermission($permission);
}
}
/**
* Detach multiple permissions from current role
*
* @param mixed $permissions
*
* @return void
*/
public function detachPermissions($permissions = null)
{
if (!$permissions) $permissions = $this->perms()->get();
foreach ($permissions as $permission) {
$this->detachPermission($permission);
}
}
}