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