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