1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Models; |
6
|
|
|
|
7
|
|
|
use Cortex\Foundation\Traits\Auditable; |
8
|
|
|
use Rinvex\Support\Traits\HasTranslations; |
9
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
10
|
|
|
use Spatie\Activitylog\Traits\LogsActivity; |
11
|
|
|
use Silber\Bouncer\Database\Ability as BaseAbility; |
12
|
|
|
|
13
|
|
|
class Ability extends BaseAbility |
14
|
|
|
{ |
15
|
|
|
use Auditable; |
16
|
|
|
use LogsActivity; |
17
|
|
|
use HasTranslations; |
18
|
|
|
use ValidatingTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
protected $fillable = [ |
24
|
|
|
'name', |
25
|
|
|
'title', |
26
|
|
|
'entity_id', |
27
|
|
|
'entity_type', |
28
|
|
|
'only_owned', |
29
|
|
|
'scope', |
30
|
|
|
'roles', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected $casts = [ |
37
|
|
|
'name' => 'string', |
38
|
|
|
'title' => 'string', |
39
|
|
|
'entity_id' => 'integer', |
40
|
|
|
'entity_type' => 'string', |
41
|
|
|
'only_owned' => 'boolean', |
42
|
|
|
'scope' => 'integer', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
protected $observables = [ |
49
|
|
|
'validating', |
50
|
|
|
'validated', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The attributes that are translatable. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
public $translatable = [ |
59
|
|
|
'title', |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The default rules that the model will validate against. |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $rules = [ |
68
|
|
|
'title' => 'required|string', |
69
|
|
|
'name' => 'required|string|max:150', |
70
|
|
|
'entity_id' => 'nullable|integer', |
71
|
|
|
'entity_type' => 'nullable|string', |
72
|
|
|
'only_owned' => 'sometimes|boolean', |
73
|
|
|
'scope' => 'nullable|integer', |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Whether the model should throw a |
78
|
|
|
* ValidationException if it fails validation. |
79
|
|
|
* |
80
|
|
|
* @var bool |
81
|
|
|
*/ |
82
|
|
|
protected $throwValidationExceptions = true; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Indicates whether to log only dirty attributes or all. |
86
|
|
|
* |
87
|
|
|
* @var bool |
88
|
|
|
*/ |
89
|
|
|
protected static $logOnlyDirty = true; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The attributes that are logged on change. |
93
|
|
|
* |
94
|
|
|
* @var array |
95
|
|
|
*/ |
96
|
|
|
protected static $logFillable = true; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* The attributes that are ignored on change. |
100
|
|
|
* |
101
|
|
|
* @var array |
102
|
|
|
*/ |
103
|
|
|
protected static $ignoreChangedAttributes = [ |
104
|
|
|
'created_at', |
105
|
|
|
'updated_at', |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Attach the given roles to the model. |
110
|
|
|
* |
111
|
|
|
* @param mixed $roles |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function setRolesAttribute($roles): void |
116
|
|
|
{ |
117
|
|
|
static::saved(function (self $model) use ($roles) { |
118
|
|
|
$roles = collect($roles)->filter(); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$model->roles->pluck('id')->similar($roles) |
121
|
|
|
|| activity() |
122
|
|
|
->performedOn($model) |
123
|
|
|
->withProperties(['attributes' => ['roles' => $roles], 'old' => ['roles' => $model->roles->pluck('id')->toArray()]]) |
|
|
|
|
124
|
|
|
->log('updated'); |
125
|
|
|
|
126
|
|
|
$model->roles()->sync($roles, true); |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope