1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Models; |
6
|
|
|
|
7
|
|
|
use Vinkla\Hashids\Facades\Hashids; |
8
|
|
|
use Rinvex\Tenants\Traits\Tenantable; |
9
|
|
|
use Cortex\Foundation\Traits\Auditable; |
10
|
|
|
use Rinvex\Support\Traits\HasTranslations; |
11
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
12
|
|
|
use Spatie\Activitylog\Traits\LogsActivity; |
13
|
|
|
use Silber\Bouncer\Database\Role as BaseRole; |
14
|
|
|
|
15
|
|
|
class Role extends BaseRole |
16
|
|
|
{ |
17
|
|
|
use Auditable; |
18
|
|
|
use Tenantable; |
19
|
|
|
use LogsActivity; |
20
|
|
|
use ValidatingTrait; |
21
|
|
|
use HasTranslations; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
protected $fillable = [ |
27
|
|
|
'name', |
28
|
|
|
'title', |
29
|
|
|
'scope', |
30
|
|
|
'abilities', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected $casts = [ |
37
|
|
|
'name' => 'string', |
38
|
|
|
'title' => 'string', |
39
|
|
|
'scope' => 'integer', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
protected $observables = [ |
46
|
|
|
'validating', |
47
|
|
|
'validated', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The attributes that are translatable. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
public $translatable = [ |
56
|
|
|
'title', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The default rules that the model will validate against. |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $rules = []; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
protected $validationMessages = [ |
70
|
|
|
'name.unique' => 'The combination of (name & scope) fields has already been taken.', |
71
|
|
|
'scope.unique' => 'The combination of (name & scope) fields has already been taken.', |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Whether the model should throw a |
76
|
|
|
* ValidationException if it fails validation. |
77
|
|
|
* |
78
|
|
|
* @var bool |
79
|
|
|
*/ |
80
|
|
|
protected $throwValidationExceptions = true; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Indicates whether to log only dirty attributes or all. |
84
|
|
|
* |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
protected static $logOnlyDirty = true; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* The attributes that are logged on change. |
91
|
|
|
* |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
protected static $logFillable = true; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* The attributes that are ignored on change. |
98
|
|
|
* |
99
|
|
|
* @var array |
100
|
|
|
*/ |
101
|
|
|
protected static $ignoreChangedAttributes = [ |
102
|
|
|
'created_at', |
103
|
|
|
'updated_at', |
104
|
|
|
]; |
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->setRules([ |
116
|
|
|
'title' => 'nullable|string|max:150', |
117
|
|
|
'name' => 'required|string|max:150|unique:'.config('cortex.auth.tables.roles').',name,NULL,id,scope,'.($this->scope ?? 'null'), |
|
|
|
|
118
|
|
|
'scope' => 'nullable|integer|unique:'.config('cortex.auth.tables.roles').',scope,NULL,id,name,'.($this->name ?? 'null'), |
|
|
|
|
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Attach the given abilities to the model. |
124
|
|
|
* |
125
|
|
|
* @param mixed $abilities |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function setAbilitiesAttribute($abilities): void |
130
|
|
|
{ |
131
|
|
|
static::saved(function (self $model) use ($abilities) { |
132
|
|
|
$abilities = collect($abilities)->filter(); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$model->abilities->pluck('id')->similar($abilities) |
135
|
|
|
|| activity() |
136
|
|
|
->performedOn($model) |
137
|
|
|
->withProperties(['attributes' => ['abilities' => $abilities], 'old' => ['abilities' => $model->abilities->pluck('id')->toArray()]]) |
|
|
|
|
138
|
|
|
->log('updated'); |
139
|
|
|
|
140
|
|
|
$model->abilities()->sync($abilities, true); |
141
|
|
|
}); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the value of the model's route key. |
146
|
|
|
* |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
public function getRouteKey() |
150
|
|
|
{ |
151
|
|
|
return Hashids::encode($this->getAttribute($this->getRouteKeyName())); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Retrieve the model for a bound value. |
156
|
|
|
* |
157
|
|
|
* @param mixed $value |
158
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
159
|
|
|
*/ |
160
|
|
|
public function resolveRouteBinding($value) |
161
|
|
|
{ |
162
|
|
|
$value = Hashids::decode($value)[0]; |
163
|
|
|
|
164
|
|
|
return $this->where($this->getRouteKeyName(), $value)->first(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
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.