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
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Rinvex\Fort\Models\Role. |
19
|
|
|
* |
20
|
|
|
* @property int $id |
21
|
|
|
* @property string $slug |
22
|
|
|
* @property array $name |
23
|
|
|
* @property array $description |
24
|
|
|
* @property \Carbon\Carbon|null $created_at |
25
|
|
|
* @property \Carbon\Carbon|null $updated_at |
26
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
27
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\Ability[] $abilities |
28
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\Rinvex\Fort\Models\User[] $users |
29
|
|
|
* |
30
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereCreatedAt($value) |
31
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereDeletedAt($value) |
32
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereDescription($value) |
33
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereId($value) |
34
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereName($value) |
35
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereSlug($value) |
36
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Role whereUpdatedAt($value) |
37
|
|
|
* @mixin \Eloquent |
38
|
|
|
*/ |
39
|
|
|
class Role extends Model implements RoleContract |
40
|
|
|
{ |
41
|
|
|
use HasSlug; |
42
|
|
|
use HasAbilities; |
43
|
|
|
use ValidatingTrait; |
44
|
|
|
use HasTranslations; |
45
|
|
|
use CacheableEloquent; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
protected $touches = [ |
51
|
|
|
'abilities', |
52
|
|
|
'users', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
protected $fillable = [ |
59
|
|
|
'slug', |
60
|
|
|
'name', |
61
|
|
|
'description', |
62
|
|
|
'abilities', |
63
|
|
|
'users', |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
protected $casts = [ |
70
|
|
|
'slug' => 'string', |
71
|
|
|
'deleted_at' => 'datetime', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
protected $with = [ |
78
|
|
|
'abilities', |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
protected $observables = [ |
85
|
|
|
'validating', |
86
|
|
|
'validated', |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* The attributes that are translatable. |
91
|
|
|
* |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
public $translatable = [ |
95
|
|
|
'name', |
96
|
|
|
'description', |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The default rules that the model will validate against. |
101
|
|
|
* |
102
|
|
|
* @var array |
103
|
|
|
*/ |
104
|
|
|
protected $rules = []; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Whether the model should throw a |
108
|
|
|
* ValidationException if it fails validation. |
109
|
|
|
* |
110
|
|
|
* @var bool |
111
|
|
|
*/ |
112
|
|
|
protected $throwValidationExceptions = true; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Create a new Eloquent model instance. |
116
|
|
|
* |
117
|
|
|
* @param array $attributes |
118
|
|
|
*/ |
119
|
|
|
public function __construct(array $attributes = []) |
120
|
|
|
{ |
121
|
|
|
parent::__construct($attributes); |
122
|
|
|
|
123
|
|
|
$this->setTable(config('rinvex.fort.tables.roles')); |
124
|
|
|
$this->setRules([ |
125
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.fort.tables.roles').',slug', |
126
|
|
|
'name' => 'required|string|max:150', |
127
|
|
|
'description' => 'nullable|string|max:10000', |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
protected static function boot() |
135
|
|
|
{ |
136
|
|
|
parent::boot(); |
137
|
|
|
|
138
|
|
|
static::updated(function (self $role) { |
|
|
|
|
139
|
|
|
app('rinvex.fort.ability')->forgetCache(); |
140
|
|
|
app('rinvex.fort.user')->forgetCache(); |
141
|
|
|
}); |
142
|
|
|
|
143
|
|
|
static::deleted(function (self $role) { |
|
|
|
|
144
|
|
|
app('rinvex.fort.ability')->forgetCache(); |
145
|
|
|
app('rinvex.fort.user')->forgetCache(); |
146
|
|
|
}); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get all attached abilities to the model. |
151
|
|
|
* |
152
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
153
|
|
|
*/ |
154
|
|
|
public function abilities(): BelongsToMany |
155
|
|
|
{ |
156
|
|
|
return $this->belongsToMany(config('rinvex.fort.models.ability'), config('rinvex.fort.tables.ability_role'), 'role_id', 'ability_id') |
|
|
|
|
157
|
|
|
->withTimestamps(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* A role may be assigned to various users. |
162
|
|
|
* |
163
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
164
|
|
|
*/ |
165
|
|
|
public function users() |
166
|
|
|
{ |
167
|
|
|
$userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model'); |
|
|
|
|
168
|
|
|
|
169
|
|
|
return $this->belongsToMany($userModel, config('rinvex.fort.tables.role_user'), 'role_id', 'user_id') |
170
|
|
|
->withTimestamps(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the options for generating the slug. |
175
|
|
|
* |
176
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
177
|
|
|
*/ |
178
|
|
|
public function getSlugOptions(): SlugOptions |
179
|
|
|
{ |
180
|
|
|
return SlugOptions::create() |
181
|
|
|
->doNotGenerateSlugsOnUpdate() |
182
|
|
|
->generateSlugsFrom('name') |
183
|
|
|
->saveSlugsTo('slug'); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Determine if the role is super admin. |
188
|
|
|
* |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
public function isSuperadmin() |
192
|
|
|
{ |
193
|
|
|
return $this->abilities->where('action', 'superadmin')->where('resource', 'global')->where('policy', null); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Determine if the role is protected. |
198
|
|
|
* |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public function isProtected() |
202
|
|
|
{ |
203
|
|
|
return in_array($this->getKey(), config('rinvex.fort.protected.roles')); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Attach the role users. |
208
|
|
|
* |
209
|
|
|
* @param mixed $users |
210
|
|
|
* |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
|
|
public function setUsersAttribute($users) |
214
|
|
|
{ |
215
|
|
|
static::saved(function (self $model) use ($users) { |
216
|
|
|
$model->users()->sync($users); |
217
|
|
|
}); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.