1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Models; |
6
|
|
|
|
7
|
|
|
use Rinvex\Tenants\Traits\Tenantable; |
8
|
|
|
use Rinvex\Support\Traits\HashidsTrait; |
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 HashidsTrait; |
20
|
|
|
use LogsActivity; |
21
|
|
|
use ValidatingTrait; |
22
|
|
|
use HasTranslations; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected $fillable = [ |
28
|
|
|
'name', |
29
|
|
|
'title', |
30
|
|
|
'scope', |
31
|
|
|
'abilities', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
protected $casts = [ |
38
|
|
|
'name' => 'string', |
39
|
|
|
'title' => 'string', |
40
|
|
|
'scope' => 'integer', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
protected $observables = [ |
47
|
|
|
'validating', |
48
|
|
|
'validated', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The attributes that are translatable. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
public $translatable = [ |
57
|
|
|
'title', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The default rules that the model will validate against. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $rules = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected $validationMessages = [ |
71
|
|
|
'name.unique' => 'The combination of (name & scope) fields has already been taken.', |
72
|
|
|
'scope.unique' => 'The combination of (name & scope) fields has already been taken.', |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Whether the model should throw a |
77
|
|
|
* ValidationException if it fails validation. |
78
|
|
|
* |
79
|
|
|
* @var bool |
80
|
|
|
*/ |
81
|
|
|
protected $throwValidationExceptions = true; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Indicates whether to log only dirty attributes or all. |
85
|
|
|
* |
86
|
|
|
* @var bool |
87
|
|
|
*/ |
88
|
|
|
protected static $logOnlyDirty = true; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The attributes that are logged on change. |
92
|
|
|
* |
93
|
|
|
* @var array |
94
|
|
|
*/ |
95
|
|
|
protected static $logFillable = true; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The attributes that are ignored on change. |
99
|
|
|
* |
100
|
|
|
* @var array |
101
|
|
|
*/ |
102
|
|
|
protected static $ignoreChangedAttributes = [ |
103
|
|
|
'created_at', |
104
|
|
|
'updated_at', |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Create a new Eloquent model instance. |
109
|
|
|
* |
110
|
|
|
* @param array $attributes |
111
|
|
|
*/ |
112
|
|
|
public function __construct(array $attributes = []) |
113
|
|
|
{ |
114
|
|
|
parent::__construct($attributes); |
115
|
|
|
|
116
|
|
|
$this->setRules([ |
117
|
|
|
'title' => 'nullable|string|max:150', |
118
|
|
|
'name' => 'required|string|max:150|unique:'.config('cortex.auth.tables.roles').',name,NULL,id,scope,'.($this->scope ?? 'null'), |
|
|
|
|
119
|
|
|
'scope' => 'nullable|integer|unique:'.config('cortex.auth.tables.roles').',scope,NULL,id,name,'.($this->name ?? 'null'), |
|
|
|
|
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Attach the given abilities to the model. |
125
|
|
|
* |
126
|
|
|
* @param mixed $abilities |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function setAbilitiesAttribute($abilities): void |
131
|
|
|
{ |
132
|
|
|
static::saved(function (self $model) use ($abilities) { |
133
|
|
|
$abilities = collect($abilities)->filter(); |
|
|
|
|
134
|
|
|
|
135
|
|
|
$model->abilities->pluck('id')->similar($abilities) |
136
|
|
|
|| activity() |
137
|
|
|
->performedOn($model) |
138
|
|
|
->withProperties(['attributes' => ['abilities' => $abilities], 'old' => ['abilities' => $model->abilities->pluck('id')->toArray()]]) |
|
|
|
|
139
|
|
|
->log('updated'); |
140
|
|
|
|
141
|
|
|
$model->abilities()->sync($abilities, true); |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
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.