|
1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelAuth\Bases\Model; |
|
4
|
|
|
use Arcanesoft\Contracts\Auth\Models\Permission as PermissionContract; |
|
5
|
|
|
use Arcanesoft\Contracts\Auth\Models\PermissionsGroup as PermissionsGroupContract; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class PermissionsGroup |
|
11
|
|
|
* |
|
12
|
|
|
* @package Arcanedev\LaravelAuth\Models |
|
13
|
|
|
* @author ARCANEDEV <[email protected]> |
|
14
|
|
|
* |
|
15
|
|
|
* @property int id |
|
16
|
|
|
* @property string name |
|
17
|
|
|
* @property string slug |
|
18
|
|
|
* @property string description |
|
19
|
|
|
* @property \Carbon\Carbon created_at |
|
20
|
|
|
* @property \Carbon\Carbon updated_at |
|
21
|
|
|
* @property \Illuminate\Database\Eloquent\Collection permissions |
|
22
|
|
|
*/ |
|
23
|
|
|
class PermissionsGroup extends Model implements PermissionsGroupContract |
|
24
|
|
|
{ |
|
25
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
26
|
|
|
| Properties |
|
27
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
28
|
|
|
*/ |
|
29
|
|
|
/** |
|
30
|
|
|
* The attributes that are mass assignable. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $fillable = ['name', 'slug', 'description']; |
|
35
|
|
|
|
|
36
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
37
|
|
|
| Constructor |
|
38
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
39
|
|
|
*/ |
|
40
|
|
|
/** |
|
41
|
|
|
* Create a new Eloquent model instance. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $attributes |
|
44
|
|
|
*/ |
|
45
|
612 |
|
public function __construct(array $attributes = []) |
|
46
|
|
|
{ |
|
47
|
612 |
|
$this->setTable( |
|
48
|
612 |
|
config('laravel-auth.permissions-groups.table', 'permissions_groups') |
|
49
|
204 |
|
); |
|
50
|
|
|
|
|
51
|
612 |
|
parent::__construct($attributes); |
|
52
|
612 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
55
|
|
|
| Relationships |
|
56
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
57
|
|
|
*/ |
|
58
|
|
|
/** |
|
59
|
|
|
* Permissions Groups has many permissions. |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
62
|
|
|
*/ |
|
63
|
117 |
|
public function permissions() |
|
64
|
|
|
{ |
|
65
|
117 |
|
return $this->hasMany( |
|
66
|
117 |
|
config('laravel-auth.permissions.model', Permission::class), |
|
67
|
78 |
|
'group_id' |
|
68
|
39 |
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
72
|
|
|
| Getters & Setters |
|
73
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
74
|
|
|
*/ |
|
75
|
|
|
/** |
|
76
|
|
|
* Set the name attribute. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $name |
|
79
|
|
|
*/ |
|
80
|
117 |
|
public function setNameAttribute($name) |
|
81
|
|
|
{ |
|
82
|
117 |
|
$this->attributes['name'] = $name; |
|
83
|
117 |
|
$this->setSlugAttribute($name); |
|
84
|
117 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Set the slug attribute. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $slug |
|
90
|
|
|
*/ |
|
91
|
117 |
|
public function setSlugAttribute($slug) |
|
92
|
|
|
{ |
|
93
|
117 |
|
$this->attributes['slug'] = $this->slugify($slug); |
|
94
|
117 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
97
|
|
|
| CRUD Functions |
|
98
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
99
|
|
|
*/ |
|
100
|
|
|
/** |
|
101
|
|
|
* Create and attach a permission. |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $attributes |
|
104
|
|
|
* @param bool $reload |
|
105
|
|
|
*/ |
|
106
|
18 |
|
public function createPermission(array $attributes, $reload = true) |
|
107
|
|
|
{ |
|
108
|
18 |
|
$this->permissions()->create($attributes); |
|
109
|
|
|
|
|
110
|
18 |
|
$this->loadPermissions($reload); |
|
111
|
18 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Attach the permission to a group. |
|
115
|
|
|
* |
|
116
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission $permission |
|
117
|
|
|
* @param bool $reload |
|
118
|
|
|
*/ |
|
119
|
54 |
|
public function attachPermission(&$permission, $reload = true) |
|
120
|
|
|
{ |
|
121
|
54 |
|
if ($this->hasPermission($permission)) |
|
122
|
24 |
|
return; |
|
123
|
|
|
|
|
124
|
54 |
|
$permission = $this->permissions()->save($permission); |
|
125
|
|
|
|
|
126
|
54 |
|
$this->loadPermissions($reload); |
|
127
|
54 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Attach the permission by id to a group. |
|
131
|
|
|
* |
|
132
|
|
|
* @param int $id |
|
133
|
|
|
* @param bool $reload |
|
134
|
|
|
* |
|
135
|
|
|
* @return \Arcanesoft\Contracts\Auth\Models\Permission |
|
136
|
|
|
*/ |
|
137
|
9 |
|
public function attachPermissionById($id, $reload = true) |
|
138
|
|
|
{ |
|
139
|
9 |
|
$permission = $this->getPermissionById($id); |
|
140
|
|
|
|
|
141
|
9 |
|
if ( ! is_null($permission)) |
|
142
|
9 |
|
$this->attachPermission($permission, $reload); |
|
143
|
|
|
|
|
144
|
9 |
|
return $permission; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Attach a collection of permissions to the group. |
|
149
|
|
|
* |
|
150
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|array $permissions |
|
151
|
|
|
* @param bool $reload |
|
152
|
|
|
* |
|
153
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|array |
|
154
|
|
|
*/ |
|
155
|
27 |
|
public function attachPermissions($permissions, $reload = true) |
|
156
|
|
|
{ |
|
157
|
27 |
|
$permissions = $this->permissions()->saveMany($permissions); |
|
158
|
|
|
|
|
159
|
27 |
|
$this->loadPermissions($reload); |
|
160
|
|
|
|
|
161
|
27 |
|
return $permissions; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Attach the permission from a group. |
|
166
|
|
|
* |
|
167
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission $permission |
|
168
|
|
|
* @param bool $reload |
|
169
|
|
|
*/ |
|
170
|
27 |
|
public function detachPermission(&$permission, $reload = true) |
|
171
|
|
|
{ |
|
172
|
27 |
|
if ( ! $this->hasPermission($permission)) |
|
173
|
21 |
|
return; |
|
174
|
|
|
|
|
175
|
27 |
|
$permission = $this->getPermissionFromGroup($permission); |
|
176
|
27 |
|
$permission->update(['group_id' => 0]); |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
27 |
|
$this->loadPermissions($reload); |
|
179
|
27 |
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Attach the permission by id to a group. |
|
183
|
|
|
* |
|
184
|
|
|
* @param int $id |
|
185
|
|
|
* @param bool $reload |
|
186
|
|
|
* |
|
187
|
|
|
* @return \Arcanesoft\Contracts\Auth\Models\Permission |
|
188
|
|
|
*/ |
|
189
|
9 |
|
public function detachPermissionById($id, $reload = true) |
|
190
|
|
|
{ |
|
191
|
9 |
|
$permission = $this->getPermissionById($id); |
|
192
|
|
|
|
|
193
|
9 |
|
if ( ! is_null($permission)) |
|
194
|
9 |
|
$this->detachPermission($permission, $reload); |
|
195
|
|
|
|
|
196
|
9 |
|
return $permission; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Detach multiple permissions by ids. |
|
201
|
|
|
* |
|
202
|
|
|
* @param array $ids |
|
203
|
|
|
* @param bool $reload |
|
204
|
|
|
* |
|
205
|
|
|
* @return int |
|
206
|
|
|
*/ |
|
207
|
9 |
|
public function detachPermissions(array $ids, $reload = true) |
|
208
|
|
|
{ |
|
209
|
9 |
|
$detached = $this->permissions()->whereIn('id', $ids)->update(['group_id' => 0]); |
|
210
|
|
|
|
|
211
|
9 |
|
$this->loadPermissions($reload); |
|
212
|
|
|
|
|
213
|
9 |
|
return $detached; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Detach all permissions from the group. |
|
218
|
|
|
* |
|
219
|
|
|
* @param bool $reload |
|
220
|
|
|
* |
|
221
|
|
|
* @return int |
|
222
|
|
|
*/ |
|
223
|
18 |
|
public function detachAllPermissions($reload = true) |
|
224
|
|
|
{ |
|
225
|
18 |
|
$detached = $this->permissions()->update(['group_id' => 0]); |
|
226
|
|
|
|
|
227
|
18 |
|
$this->loadPermissions($reload); |
|
228
|
|
|
|
|
229
|
18 |
|
return $detached; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
233
|
|
|
| Check Functions |
|
234
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
235
|
|
|
*/ |
|
236
|
|
|
/** |
|
237
|
|
|
* Check if role has the given permission (Permission Model or Id). |
|
238
|
|
|
* |
|
239
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission|int $id |
|
240
|
|
|
* |
|
241
|
|
|
* @return bool |
|
242
|
|
|
*/ |
|
243
|
72 |
|
public function hasPermission($id) |
|
244
|
|
|
{ |
|
245
|
72 |
|
if ($id instanceof Eloquent) |
|
|
|
|
|
|
246
|
72 |
|
$id = $id->getKey(); |
|
247
|
|
|
|
|
248
|
72 |
|
return ! is_null($this->getPermissionFromGroup($id)); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
252
|
|
|
| Other Functions |
|
253
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
254
|
|
|
*/ |
|
255
|
|
|
/** |
|
256
|
|
|
* Get a permission from the group. |
|
257
|
|
|
* |
|
258
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission|int $id |
|
259
|
|
|
* |
|
260
|
|
|
* @return \Arcanesoft\Contracts\Auth\Models\Permission|null |
|
261
|
|
|
*/ |
|
262
|
72 |
|
private function getPermissionFromGroup($id) |
|
263
|
|
|
{ |
|
264
|
72 |
|
if ($id instanceof Eloquent) |
|
|
|
|
|
|
265
|
42 |
|
$id = $id->getKey(); |
|
266
|
|
|
|
|
267
|
72 |
|
$this->loadPermissions(); |
|
268
|
|
|
|
|
269
|
72 |
|
return $this->permissions->filter(function (PermissionContract $permission) use ($id) { |
|
270
|
72 |
|
return $permission->id == $id; |
|
|
|
|
|
|
271
|
72 |
|
})->first(); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Get a permission by id. |
|
276
|
|
|
* |
|
277
|
|
|
* @param int $id |
|
278
|
|
|
* |
|
279
|
|
|
* @return \Arcanesoft\Contracts\Auth\Models\Permission|null |
|
280
|
|
|
*/ |
|
281
|
18 |
|
private function getPermissionById($id) |
|
282
|
|
|
{ |
|
283
|
18 |
|
return $this->permissions() |
|
284
|
18 |
|
->getRelated() |
|
285
|
18 |
|
->where('id', $id) |
|
286
|
18 |
|
->first(); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Load the permissions. |
|
291
|
|
|
* |
|
292
|
|
|
* @param bool $load |
|
293
|
|
|
* |
|
294
|
|
|
* @return self |
|
295
|
|
|
*/ |
|
296
|
99 |
|
protected function loadPermissions($load = true) |
|
297
|
|
|
{ |
|
298
|
99 |
|
return $load ? $this->load('permissions') : $this; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Slugify the value. |
|
303
|
|
|
* |
|
304
|
|
|
* @param string $value |
|
305
|
|
|
* |
|
306
|
|
|
* @return string |
|
307
|
|
|
*/ |
|
308
|
117 |
|
protected function slugify($value) |
|
309
|
|
|
{ |
|
310
|
117 |
|
return Str::slug($value, config('laravel-auth.permissions-groups.slug-separator', '-')); |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.