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