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