1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelAuth\Events\Permissions\{ |
4
|
|
|
AttachedRoleToPermission, AttachingRoleToPermission, CreatedPermission, CreatingPermission, DeletedPermission, |
5
|
|
|
DeletingPermission, DetachedAllRolesFromPermission, DetachedRoleFromPermission, DetachingAllRolesFromPermission, |
6
|
|
|
DetachingRoleFromPermission, SavedPermission, SavingPermission, SyncedRolesWithPermission, SyncingRolesWithPermission, |
7
|
|
|
UpdatedPermission, UpdatingPermission |
8
|
|
|
}; |
9
|
|
|
use Arcanedev\LaravelAuth\Models\Traits\Roleable; |
10
|
|
|
use Arcanesoft\Contracts\Auth\Models\Permission as PermissionContract; |
11
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Permission |
16
|
|
|
* |
17
|
|
|
* @package Arcanedev\LaravelAuth\Models |
18
|
|
|
* @author ARCANEDEV <[email protected]> |
19
|
|
|
* |
20
|
|
|
* @property int id |
21
|
|
|
* @property int group_id |
22
|
|
|
* @property string name |
23
|
|
|
* @property string slug |
24
|
|
|
* @property string description |
25
|
|
|
* @property \Carbon\Carbon created_at |
26
|
|
|
* @property \Carbon\Carbon updated_at |
27
|
|
|
* |
28
|
|
|
* @property \Illuminate\Database\Eloquent\Collection roles |
29
|
|
|
* @property \Arcanedev\LaravelAuth\Models\PermissionsGroup group |
30
|
|
|
* @property \Arcanedev\LaravelAuth\Models\Pivots\PermissionRole pivot |
31
|
|
|
*/ |
32
|
|
|
class Permission extends AbstractModel implements PermissionContract |
33
|
|
|
{ |
34
|
|
|
/* ----------------------------------------------------------------- |
35
|
|
|
| Traits |
36
|
|
|
| ----------------------------------------------------------------- |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
use Roleable; |
40
|
|
|
|
41
|
|
|
/* ----------------------------------------------------------------- |
42
|
|
|
| Properties |
43
|
|
|
| ----------------------------------------------------------------- |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The attributes that are mass assignable. |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $fillable = [ |
52
|
|
|
'group_id', |
53
|
|
|
'name', |
54
|
|
|
'slug', |
55
|
|
|
'description', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The event map for the model. |
60
|
|
|
* |
61
|
|
|
* Allows for object-based events for native Eloquent events. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $dispatchesEvents = [ |
66
|
|
|
'creating' => CreatingPermission::class, |
67
|
|
|
'created' => CreatedPermission::class, |
68
|
|
|
'updating' => UpdatingPermission::class, |
69
|
|
|
'updated' => UpdatedPermission::class, |
70
|
|
|
'saving' => SavingPermission::class, |
71
|
|
|
'saved' => SavedPermission::class, |
72
|
|
|
'deleting' => DeletingPermission::class, |
73
|
|
|
'deleted' => DeletedPermission::class, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The attributes that should be cast to native types. |
78
|
|
|
* |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
protected $casts = [ |
82
|
|
|
'id' => 'integer', |
83
|
|
|
'group_id' => 'integer', |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
/* ----------------------------------------------------------------- |
87
|
|
|
| Constructor |
88
|
|
|
| ----------------------------------------------------------------- |
89
|
|
|
*/ |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create a new Eloquent model instance. |
93
|
|
|
* |
94
|
|
|
* @param array $attributes |
95
|
|
|
*/ |
96
|
108 |
|
public function __construct(array $attributes = []) |
97
|
|
|
{ |
98
|
108 |
|
parent::__construct($attributes); |
99
|
|
|
|
100
|
108 |
|
$this->setTable(config('laravel-auth.permissions.table', 'permissions')); |
101
|
108 |
|
} |
102
|
|
|
|
103
|
|
|
/* ----------------------------------------------------------------- |
104
|
|
|
| Relationships |
105
|
|
|
| ----------------------------------------------------------------- |
106
|
|
|
*/ |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Permission belongs to one group. |
110
|
|
|
* |
111
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
112
|
|
|
*/ |
113
|
3 |
|
public function group() |
114
|
|
|
{ |
115
|
3 |
|
return $this->belongsTo( |
116
|
3 |
|
config('laravel-auth.permissions-groups.model', PermissionsGroup::class), |
117
|
3 |
|
'group_id' |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Permission belongs to many roles. |
123
|
|
|
* |
124
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
125
|
|
|
*/ |
126
|
27 |
|
public function roles() |
127
|
|
|
{ |
128
|
|
|
return $this |
129
|
27 |
|
->belongsToMany( |
130
|
27 |
|
config('laravel-auth.roles.model', Role::class), |
131
|
27 |
|
$this->getPrefix().'permission_role' |
132
|
|
|
) |
133
|
27 |
|
->withTimestamps(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/* ----------------------------------------------------------------- |
137
|
|
|
| Setters & Getters |
138
|
|
|
| ----------------------------------------------------------------- |
139
|
|
|
*/ |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the slug attribute. |
143
|
|
|
* |
144
|
|
|
* @param string $slug |
145
|
|
|
*/ |
146
|
90 |
|
public function setSlugAttribute($slug) |
147
|
|
|
{ |
148
|
90 |
|
$this->attributes['slug'] = $this->slugify($slug); |
149
|
90 |
|
} |
150
|
|
|
|
151
|
|
|
/* ----------------------------------------------------------------- |
152
|
|
|
| Main Methods |
153
|
|
|
| ----------------------------------------------------------------- |
154
|
|
|
*/ |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Attach a role to a user. |
158
|
|
|
* |
159
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
160
|
|
|
* @param bool $reload |
161
|
|
|
*/ |
162
|
21 |
|
public function attachRole($role, $reload = true) |
163
|
|
|
{ |
164
|
21 |
|
if ($this->hasRole($role)) return; |
165
|
|
|
|
166
|
21 |
|
event(new AttachingRoleToPermission($this, $role)); |
167
|
21 |
|
$this->roles()->attach($role); |
168
|
21 |
|
event(new AttachedRoleToPermission($this, $role)); |
169
|
|
|
|
170
|
21 |
|
$this->loadRoles($reload); |
171
|
21 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Sync the roles by its slugs. |
175
|
|
|
* |
176
|
|
|
* @param array|\Illuminate\Support\Collection $slugs |
177
|
|
|
* @param bool $reload |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
3 |
|
public function syncRoles($slugs, $reload = true) |
182
|
|
|
{ |
183
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $roles */ |
184
|
3 |
|
$roles = app(RoleContract::class)->whereIn('slug', $slugs)->get(); |
185
|
|
|
|
186
|
3 |
|
event(new SyncingRolesWithPermission($this, $roles)); |
187
|
3 |
|
$synced = $this->roles()->sync($roles->pluck('id')); |
188
|
3 |
|
event(new SyncedRolesWithPermission($this, $roles, $synced)); |
189
|
|
|
|
190
|
3 |
|
$this->loadRoles($reload); |
191
|
|
|
|
192
|
3 |
|
return $synced; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Detach a role from a user. |
197
|
|
|
* |
198
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
199
|
|
|
* @param bool $reload |
200
|
|
|
* |
201
|
|
|
* @return int |
202
|
|
|
*/ |
203
|
3 |
|
public function detachRole($role, $reload = true) |
204
|
|
|
{ |
205
|
3 |
|
event(new DetachingRoleFromPermission($this, $role)); |
206
|
3 |
|
$results = $this->roles()->detach($role); |
207
|
3 |
|
event(new DetachedRoleFromPermission($this, $role, $results)); |
208
|
|
|
|
209
|
3 |
|
$this->loadRoles($reload); |
210
|
|
|
|
211
|
3 |
|
return $results; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Detach all roles from a user. |
216
|
|
|
* |
217
|
|
|
* @param bool $reload |
218
|
|
|
* |
219
|
|
|
* @return int |
220
|
|
|
*/ |
221
|
3 |
|
public function detachAllRoles($reload = true) |
222
|
|
|
{ |
223
|
3 |
|
event(new DetachingAllRolesFromPermission($this)); |
224
|
3 |
|
$results = $this->roles()->detach(); |
225
|
3 |
|
event(new DetachedAllRolesFromPermission($this, $results)); |
226
|
|
|
|
227
|
3 |
|
$this->loadRoles($reload); |
228
|
|
|
|
229
|
3 |
|
return $results; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/* ----------------------------------------------------------------- |
233
|
|
|
| Check Methods |
234
|
|
|
| ----------------------------------------------------------------- |
235
|
|
|
*/ |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Check if the permission has the same slug. |
239
|
|
|
* |
240
|
|
|
* @param string $slug |
241
|
|
|
* |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
18 |
|
public function hasSlug($slug) |
245
|
|
|
{ |
246
|
18 |
|
return $this->slug === $this->slugify($slug); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/* ----------------------------------------------------------------- |
250
|
|
|
| Other Functions |
251
|
|
|
| ----------------------------------------------------------------- |
252
|
|
|
*/ |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Slugify the value. |
256
|
|
|
* |
257
|
|
|
* @param string $value |
258
|
|
|
* |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
90 |
|
protected function slugify($value) |
262
|
|
|
{ |
263
|
90 |
|
return Str::slug($value, config('laravel-auth.permissions.slug-separator', '.')); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|