1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Auth\Authenticatable; |
8
|
|
|
use Illuminate\Support\Facades\Hash; |
9
|
|
|
use Rinvex\Auth\Traits\HasHashables; |
10
|
|
|
use Cortex\Foundation\Traits\Auditable; |
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
13
|
|
|
use Rinvex\Support\Traits\HashidsTrait; |
14
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
15
|
|
|
use Spatie\Activitylog\Traits\LogsActivity; |
16
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
17
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
18
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
19
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
20
|
|
|
|
21
|
|
|
class Guardian extends Model implements AuthenticatableContract, AuthorizableContract |
22
|
|
|
{ |
23
|
|
|
// @TODO: Strangely, this issue happens only here!!! |
|
|
|
|
24
|
|
|
// Duplicate trait usage to fire attached events for cache |
25
|
|
|
// flush before other events in other traits specially LogsActivity, |
26
|
|
|
// otherwise old cached queries used and no changelog recorded on update. |
27
|
|
|
use CacheableEloquent; |
28
|
|
|
use Auditable; |
29
|
|
|
use HashidsTrait; |
30
|
|
|
use LogsActivity; |
31
|
|
|
use Authorizable; |
32
|
|
|
use HasHashables; |
33
|
|
|
use Authenticatable; |
34
|
|
|
use ValidatingTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
protected $fillable = [ |
40
|
|
|
'username', |
41
|
|
|
'password', |
42
|
|
|
'email', |
43
|
|
|
'is_active', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
protected $casts = [ |
50
|
|
|
'username' => 'string', |
51
|
|
|
'password' => 'string', |
52
|
|
|
'email' => 'string', |
53
|
|
|
'is_active' => 'boolean', |
54
|
|
|
'deleted_at' => 'datetime', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
protected $hidden = [ |
61
|
|
|
'password', |
62
|
|
|
'remember_token', |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
protected $observables = [ |
69
|
|
|
'validating', |
70
|
|
|
'validated', |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The attributes to be encrypted before saving. |
75
|
|
|
* |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
protected $hashables = [ |
79
|
|
|
'password', |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The default rules that the model will validate against. |
84
|
|
|
* |
85
|
|
|
* @var array |
86
|
|
|
*/ |
87
|
|
|
protected $rules = []; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Whether the model should throw a |
91
|
|
|
* ValidationException if it fails validation. |
92
|
|
|
* |
93
|
|
|
* @var bool |
94
|
|
|
*/ |
95
|
|
|
protected $throwValidationExceptions = true; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Indicates whether to log only dirty attributes or all. |
99
|
|
|
* |
100
|
|
|
* @var bool |
101
|
|
|
*/ |
102
|
|
|
protected static $logOnlyDirty = true; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* The attributes that are logged on change. |
106
|
|
|
* |
107
|
|
|
* @var array |
108
|
|
|
*/ |
109
|
|
|
protected static $logFillable = true; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* The attributes that are ignored on change. |
113
|
|
|
* |
114
|
|
|
* @var array |
115
|
|
|
*/ |
116
|
|
|
protected static $ignoreChangedAttributes = [ |
117
|
|
|
'password', |
118
|
|
|
'created_at', |
119
|
|
|
'updated_at', |
120
|
|
|
'deleted_at', |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Create a new Eloquent model instance. |
125
|
|
|
* |
126
|
|
|
* @param array $attributes |
127
|
|
|
*/ |
128
|
|
|
public function __construct(array $attributes = []) |
129
|
|
|
{ |
130
|
|
|
parent::__construct($attributes); |
131
|
|
|
|
132
|
|
|
$this->setTable(config('cortex.auth.tables.guardians')); |
133
|
|
|
$this->setRules([ |
134
|
|
|
'username' => 'required|alpha_dash|min:3|max:150|unique:'.config('cortex.auth.tables.guardians').',username', |
|
|
|
|
135
|
|
|
'password' => 'sometimes|required|min:'.config('cortex.auth.password_min_chars'), |
136
|
|
|
'email' => 'required|email|min:3|max:150|unique:'.config('cortex.auth.tables.guardians').',email', |
137
|
|
|
'is_active' => 'sometimes|boolean', |
138
|
|
|
'tags' => 'nullable|array', |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
protected static function boot() |
146
|
|
|
{ |
147
|
|
|
parent::boot(); |
148
|
|
|
|
149
|
|
|
static::saving(function (self $user) { |
150
|
|
|
foreach (array_intersect($user->getHashables(), array_keys($user->getAttributes())) as $hashable) { |
151
|
|
|
if ($user->isDirty($hashable) && Hash::needsRehash($user->$hashable)) { |
152
|
|
|
$user->$hashable = Hash::make($user->$hashable); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
}); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* The user may have many sessions. |
160
|
|
|
* |
161
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
162
|
|
|
*/ |
163
|
|
|
public function sessions(): MorphMany |
164
|
|
|
{ |
165
|
|
|
return $this->morphMany(config('cortex.auth.models.session'), 'user'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Activate the user. |
170
|
|
|
* |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
public function makeActive() |
174
|
|
|
{ |
175
|
|
|
$this->update(['is_active' => true]); |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Deactivate the user. |
182
|
|
|
* |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function makeInactive() |
186
|
|
|
{ |
187
|
|
|
$this->update(['is_active' => false]); |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the route key for the model. |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function getRouteKeyName() |
198
|
|
|
{ |
199
|
|
|
return 'username'; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|