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