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\Support\Traits\HasTranslations; |
13
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
14
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
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 |
39
|
|
|
{ |
40
|
|
|
use HasSlug; |
41
|
|
|
use HasAbilities; |
42
|
|
|
use ValidatingTrait; |
43
|
|
|
use HasTranslations; |
44
|
|
|
use CacheableEloquent; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
protected $touches = [ |
50
|
|
|
'users', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected $fillable = [ |
57
|
|
|
'slug', |
58
|
|
|
'name', |
59
|
|
|
'description', |
60
|
|
|
'abilities', |
61
|
|
|
'users', |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
protected $casts = [ |
68
|
|
|
'slug' => 'string', |
69
|
|
|
'deleted_at' => 'datetime', |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
protected $with = [ |
76
|
|
|
'abilities', |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
protected $observables = [ |
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
|
|
|
* Get all attached abilities to the model. |
131
|
|
|
* |
132
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
133
|
|
|
*/ |
134
|
|
|
public function abilities(): BelongsToMany |
135
|
|
|
{ |
136
|
|
|
return $this->belongsToMany(config('rinvex.fort.models.ability'), config('rinvex.fort.tables.ability_role'), 'role_id', 'ability_id') |
|
|
|
|
137
|
|
|
->withTimestamps(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* A role may be assigned to various users. |
142
|
|
|
* |
143
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
144
|
|
|
*/ |
145
|
|
|
public function users(): BelongsToMany |
146
|
|
|
{ |
147
|
|
|
$userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model'); |
|
|
|
|
148
|
|
|
|
149
|
|
|
return $this->belongsToMany($userModel, config('rinvex.fort.tables.role_user'), 'role_id', 'user_id') |
150
|
|
|
->withTimestamps(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the options for generating the slug. |
155
|
|
|
* |
156
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
157
|
|
|
*/ |
158
|
|
|
public function getSlugOptions(): SlugOptions |
159
|
|
|
{ |
160
|
|
|
return SlugOptions::create() |
161
|
|
|
->doNotGenerateSlugsOnUpdate() |
162
|
|
|
->generateSlugsFrom('name') |
163
|
|
|
->saveSlugsTo('slug'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Determine if the role is super admin. |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function isSuperadmin(): bool |
172
|
|
|
{ |
173
|
|
|
return $this->abilities->where('action', 'superadmin')->where('resource', 'global')->where('policy', null); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Determine if the role is protected. |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function isProtected(): bool |
182
|
|
|
{ |
183
|
|
|
return in_array($this->getKey(), config('rinvex.fort.protected.roles')); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Attach the role users. |
188
|
|
|
* |
189
|
|
|
* @param mixed $users |
190
|
|
|
* |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
public function setUsersAttribute($users): void |
194
|
|
|
{ |
195
|
|
|
static::saved(function (self $model) use ($users) { |
196
|
|
|
$model->users()->sync($users); |
197
|
|
|
}); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.