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