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