|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Fort\Models; |
|
6
|
|
|
|
|
7
|
|
|
use Spatie\Sluggable\SlugOptions; |
|
8
|
|
|
use Rinvex\Support\Traits\HasSlug; |
|
9
|
|
|
use Rinvex\Fort\Traits\HasAbilities; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
11
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
|
12
|
|
|
use Rinvex\Fort\Contracts\RoleContract; |
|
13
|
|
|
use Rinvex\Support\Traits\HasTranslations; |
|
14
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Rinvex\Fort\Models\Role. |
|
18
|
|
|
* |
|
19
|
|
|
* @property int $id |
|
20
|
|
|
* @property string $slug |
|
21
|
|
|
* @property array $name |
|
22
|
|
|
* @property array $description |
|
23
|
|
|
* @property \Carbon\Carbon|null $created_at |
|
24
|
|
|
* @property \Carbon\Carbon|null $updated_at |
|
25
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
|
26
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Ability[] $abilities |
|
27
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\User[] $users |
|
28
|
|
|
* |
|
29
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereCreatedAt($value) |
|
30
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereDeletedAt($value) |
|
31
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereDescription($value) |
|
32
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereId($value) |
|
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereName($value) |
|
34
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereSlug($value) |
|
35
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereUpdatedAt($value) |
|
36
|
|
|
* @mixin \Eloquent |
|
37
|
|
|
*/ |
|
38
|
|
|
class Role extends Model implements RoleContract |
|
39
|
|
|
{ |
|
40
|
|
|
use HasSlug; |
|
41
|
|
|
use HasAbilities; |
|
42
|
|
|
use ValidatingTrait; |
|
43
|
|
|
use HasTranslations; |
|
44
|
|
|
use CacheableEloquent; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $fillable = [ |
|
50
|
|
|
'slug', |
|
51
|
|
|
'name', |
|
52
|
|
|
'description', |
|
53
|
|
|
'abilities', |
|
54
|
|
|
'users', |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $casts = [ |
|
61
|
|
|
'slug' => 'string', |
|
62
|
|
|
'deleted_at' => 'datetime', |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $with = [ |
|
69
|
|
|
'abilities', |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $observables = [ |
|
76
|
|
|
'attaching', |
|
77
|
|
|
'attached', |
|
78
|
|
|
'syncing', |
|
79
|
|
|
'synced', |
|
80
|
|
|
'detaching', |
|
81
|
|
|
'detached', |
|
82
|
|
|
'validating', |
|
83
|
|
|
'validated', |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* The attributes that are translatable. |
|
88
|
|
|
* |
|
89
|
|
|
* @var array |
|
90
|
|
|
*/ |
|
91
|
|
|
public $translatable = [ |
|
92
|
|
|
'name', |
|
93
|
|
|
'description', |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* The default rules that the model will validate against. |
|
98
|
|
|
* |
|
99
|
|
|
* @var array |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $rules = []; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Whether the model should throw a |
|
105
|
|
|
* ValidationException if it fails validation. |
|
106
|
|
|
* |
|
107
|
|
|
* @var bool |
|
108
|
|
|
*/ |
|
109
|
|
|
protected $throwValidationExceptions = true; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Create a new Eloquent model instance. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $attributes |
|
115
|
|
|
*/ |
|
116
|
|
|
public function __construct(array $attributes = []) |
|
117
|
|
|
{ |
|
118
|
|
|
parent::__construct($attributes); |
|
119
|
|
|
|
|
120
|
|
|
$this->setTable(config('rinvex.fort.tables.roles')); |
|
121
|
|
|
$this->setRules([ |
|
122
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.fort.tables.roles').',slug', |
|
123
|
|
|
'name' => 'required|string|max:150', |
|
124
|
|
|
'description' => 'nullable|string|max:10000', |
|
125
|
|
|
]); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
|
|
protected static function boot() |
|
132
|
|
|
{ |
|
133
|
|
|
parent::boot(); |
|
134
|
|
|
|
|
135
|
|
|
static::updated(function (self $role) { |
|
|
|
|
|
|
136
|
|
|
app('rinvex.fort.ability')->forgetCache(); |
|
137
|
|
|
app('rinvex.fort.user')->forgetCache(); |
|
138
|
|
|
}); |
|
139
|
|
|
|
|
140
|
|
|
static::deleted(function (self $role) { |
|
|
|
|
|
|
141
|
|
|
app('rinvex.fort.ability')->forgetCache(); |
|
142
|
|
|
app('rinvex.fort.user')->forgetCache(); |
|
143
|
|
|
}); |
|
144
|
|
|
|
|
145
|
|
|
static::attached(function (self $role) { |
|
|
|
|
|
|
146
|
|
|
app('rinvex.fort.ability')->forgetCache(); |
|
147
|
|
|
app('rinvex.fort.user')->forgetCache(); |
|
148
|
|
|
}); |
|
149
|
|
|
|
|
150
|
|
|
static::synced(function (self $role) { |
|
|
|
|
|
|
151
|
|
|
app('rinvex.fort.ability')->forgetCache(); |
|
152
|
|
|
app('rinvex.fort.user')->forgetCache(); |
|
153
|
|
|
}); |
|
154
|
|
|
|
|
155
|
|
|
static::detached(function (self $role) { |
|
|
|
|
|
|
156
|
|
|
app('rinvex.fort.ability')->forgetCache(); |
|
157
|
|
|
app('rinvex.fort.user')->forgetCache(); |
|
158
|
|
|
}); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Register an attaching role event with the dispatcher. |
|
163
|
|
|
* |
|
164
|
|
|
* @param \Closure|string $callback |
|
165
|
|
|
* |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
|
|
public static function attaching($callback) |
|
169
|
|
|
{ |
|
170
|
|
|
static::registerModelEvent('attaching', $callback); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Register an attached role event with the dispatcher. |
|
175
|
|
|
* |
|
176
|
|
|
* @param \Closure|string $callback |
|
177
|
|
|
* |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
|
|
public static function attached($callback) |
|
181
|
|
|
{ |
|
182
|
|
|
static::registerModelEvent('attached', $callback); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Register a syncing role event with the dispatcher. |
|
187
|
|
|
* |
|
188
|
|
|
* @param \Closure|string $callback |
|
189
|
|
|
* |
|
190
|
|
|
* @return void |
|
191
|
|
|
*/ |
|
192
|
|
|
public static function syncing($callback) |
|
193
|
|
|
{ |
|
194
|
|
|
static::registerModelEvent('syncing', $callback); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Register a synced role event with the dispatcher. |
|
199
|
|
|
* |
|
200
|
|
|
* @param \Closure|string $callback |
|
201
|
|
|
* |
|
202
|
|
|
* @return void |
|
203
|
|
|
*/ |
|
204
|
|
|
public static function synced($callback) |
|
205
|
|
|
{ |
|
206
|
|
|
static::registerModelEvent('synced', $callback); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Register a detaching role event with the dispatcher. |
|
211
|
|
|
* |
|
212
|
|
|
* @param \Closure|string $callback |
|
213
|
|
|
* |
|
214
|
|
|
* @return void |
|
215
|
|
|
*/ |
|
216
|
|
|
public static function detaching($callback) |
|
217
|
|
|
{ |
|
218
|
|
|
static::registerModelEvent('detaching', $callback); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Register a detached role event with the dispatcher. |
|
223
|
|
|
* |
|
224
|
|
|
* @param \Closure|string $callback |
|
225
|
|
|
* |
|
226
|
|
|
* @return void |
|
227
|
|
|
*/ |
|
228
|
|
|
public static function detached($callback) |
|
229
|
|
|
{ |
|
230
|
|
|
static::registerModelEvent('detached', $callback); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* A role may be given various abilities. |
|
235
|
|
|
* |
|
236
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
237
|
|
|
*/ |
|
238
|
|
|
public function abilities() |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->belongsToMany(config('rinvex.fort.models.ability'), config('rinvex.fort.tables.ability_role'), 'role_id', 'ability_id') |
|
|
|
|
|
|
241
|
|
|
->withTimestamps(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* A role may be assigned to various users. |
|
246
|
|
|
* |
|
247
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
248
|
|
|
*/ |
|
249
|
|
|
public function users() |
|
250
|
|
|
{ |
|
251
|
|
|
$userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model'); |
|
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
return $this->belongsToMany($userModel, config('rinvex.fort.tables.role_user'), 'role_id', 'user_id') |
|
254
|
|
|
->withTimestamps(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Get the options for generating the slug. |
|
259
|
|
|
* |
|
260
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
|
261
|
|
|
*/ |
|
262
|
|
|
public function getSlugOptions(): SlugOptions |
|
263
|
|
|
{ |
|
264
|
|
|
return SlugOptions::create() |
|
265
|
|
|
->doNotGenerateSlugsOnUpdate() |
|
266
|
|
|
->generateSlugsFrom('name') |
|
267
|
|
|
->saveSlugsTo('slug'); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Determine if the role is super admin. |
|
272
|
|
|
* |
|
273
|
|
|
* @return bool |
|
274
|
|
|
*/ |
|
275
|
|
|
public function isSuperadmin() |
|
276
|
|
|
{ |
|
277
|
|
|
return $this->abilities->where('resource', 'global')->where('policy', null)->contains('action', 'superadmin'); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Determine if the role is protected. |
|
282
|
|
|
* |
|
283
|
|
|
* @return bool |
|
284
|
|
|
*/ |
|
285
|
|
|
public function isProtected() |
|
286
|
|
|
{ |
|
287
|
|
|
return in_array($this->id, config('rinvex.fort.protected.roles')); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Attach the role abilities. |
|
292
|
|
|
* |
|
293
|
|
|
* @param mixed $abilities |
|
294
|
|
|
* |
|
295
|
|
|
* @return void |
|
296
|
|
|
*/ |
|
297
|
|
|
public function setAbilitiesAttribute($abilities) |
|
298
|
|
|
{ |
|
299
|
|
|
static::saved(function (self $model) use ($abilities) { |
|
300
|
|
|
$model->abilities()->sync($abilities); |
|
301
|
|
|
}); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Attach the role users. |
|
306
|
|
|
* |
|
307
|
|
|
* @param mixed $users |
|
308
|
|
|
* |
|
309
|
|
|
* @return void |
|
310
|
|
|
*/ |
|
311
|
|
|
public function setUsersAttribute($users) |
|
312
|
|
|
{ |
|
313
|
|
|
static::saved(function (self $model) use ($users) { |
|
314
|
|
|
$model->users()->sync($users); |
|
315
|
|
|
}); |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.