1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by Reliese Model. |
5
|
|
|
* Date: Thu, 12 Jul 2018 22:39:28 +0000. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace App\Models; |
9
|
|
|
|
10
|
|
|
use App\Events\UserCreated; |
11
|
|
|
use App\Events\UserUpdated; |
12
|
|
|
use App\Notifications\ResetPasswordNotification; |
13
|
|
|
use App\Traits\RememberDeviceTrait; |
14
|
|
|
use App\Traits\TalentCloudCrudTrait as CrudTrait; |
15
|
|
|
use Illuminate\Auth\Authenticatable; |
16
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
17
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
18
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
19
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
20
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
21
|
|
|
use Illuminate\Notifications\Notifiable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class User |
25
|
|
|
* |
26
|
|
|
* @property int $id |
27
|
|
|
* @property string $email |
28
|
|
|
* @property string $first_name |
29
|
|
|
* @property string $last_name |
30
|
|
|
* @property string $password |
31
|
|
|
* @property boolean $is_confirmed |
32
|
|
|
* @property boolean $is_priority |
33
|
|
|
* @property int $user_role_id |
34
|
|
|
* @property string $gov_email |
35
|
|
|
* @property boolean $not_in_gov |
36
|
|
|
* @property string $google2fa_secret |
37
|
|
|
* @property array $recovery_codes |
38
|
|
|
* @property int $department_id |
39
|
|
|
* @property string $contact_language |
40
|
|
|
* @property boolean $job_alerts |
41
|
|
|
* @property \Jenssegers\Date\Date $recovery_codes_generation_date |
42
|
|
|
* @property \Jenssegers\Date\Date $created_at |
43
|
|
|
* @property \Jenssegers\Date\Date $updated_at |
44
|
|
|
* |
45
|
|
|
* @property \App\Models\Applicant $applicant |
46
|
|
|
* @property \App\Models\Manager $manager |
47
|
|
|
* @property \App\Models\HrAdvisor $hr_advisor |
48
|
|
|
* @property \App\Models\ProfilePic $profile_pic |
49
|
|
|
* @property \App\Models\UserRole $user_role |
50
|
|
|
* @property \App\Models\Lookup\Department $department |
51
|
|
|
*/ |
52
|
|
|
class User extends BaseModel implements |
53
|
|
|
// Laravel contracts for native login. |
54
|
|
|
AuthenticatableContract, |
55
|
|
|
CanResetPasswordContract, |
56
|
|
|
// Contract for use with Gates and Policies. |
57
|
|
|
AuthorizableContract |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
// Traits for Laravel basic authentication. |
61
|
|
|
use Authenticatable; |
62
|
|
|
use CanResetPassword; |
63
|
|
|
// Trait for working with Gates and Policies. |
64
|
|
|
use Authorizable; |
65
|
|
|
// Trait for notifications. |
66
|
|
|
use Notifiable; |
67
|
|
|
// Trait for Backpack. |
68
|
|
|
use CrudTrait; |
69
|
|
|
// Trait for 2FA device memory. |
70
|
|
|
use RememberDeviceTrait; |
71
|
|
|
|
72
|
|
|
protected $casts = [ |
73
|
|
|
'is_confirmed' => 'boolean', |
74
|
|
|
'is_priority' => 'boolean', |
75
|
|
|
'user_role_id' => 'int', |
76
|
|
|
'email' => 'string', |
77
|
|
|
'gov_email' => 'string', |
78
|
|
|
'not_in_gov' => 'boolean', |
79
|
|
|
'department_id' => 'int', |
80
|
|
|
'contact_language' => 'string', |
81
|
|
|
'job_alerts' => 'boolean', |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var string[] $dates |
86
|
|
|
*/ |
87
|
|
|
protected $dates = [ |
88
|
|
|
'recovery_codes_generation_date', |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
protected $fillable = [ |
92
|
|
|
'first_name', |
93
|
|
|
'last_name', |
94
|
|
|
'email', |
95
|
|
|
'password', |
96
|
|
|
'is_priority', |
97
|
|
|
'gov_email', |
98
|
|
|
'not_in_gov', |
99
|
|
|
'google2fa_secret', |
100
|
|
|
'department_id', |
101
|
|
|
'contact_language', |
102
|
|
|
'job_alerts', |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
protected $with = ['user_role']; |
106
|
|
|
|
107
|
|
|
protected $hidden = [ |
108
|
|
|
'password', |
109
|
|
|
'remember_token', |
110
|
|
|
'remember_device_token', |
111
|
|
|
'recovery_codes_generation_date', |
112
|
|
|
'google2fa_secret', |
113
|
|
|
'recovery_codes', |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* The event map for the model. |
118
|
|
|
* |
119
|
|
|
* @var array |
120
|
|
|
*/ |
121
|
|
|
protected $dispatchesEvents = [ |
122
|
|
|
'created' => UserCreated::class, |
123
|
|
|
'updated' => UserUpdated::class, |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
public function applicant() //phpcs:ignore |
127
|
|
|
{ |
128
|
|
|
return $this->hasOne(\App\Models\Applicant::class); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function manager() //phpcs:ignore |
132
|
|
|
{ |
133
|
|
|
return $this->hasOne(\App\Models\Manager::class); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function hr_advisor() //phpcs:ignore |
137
|
|
|
{ |
138
|
|
|
return $this->hasOne(\App\Models\HrAdvisor::class); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function profile_pic() //phpcs:ignore |
142
|
|
|
{ |
143
|
|
|
return $this->hasOne(\App\Models\ProfilePic::class); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function user_role() //phpcs:ignore |
147
|
|
|
{ |
148
|
|
|
return $this->belongsTo(\App\Models\UserRole::class); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function department() |
152
|
|
|
{ |
153
|
|
|
return $this->belongsTo(\App\Models\Lookup\Department::class); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function setIsPriorityAttribute($value) |
157
|
|
|
{ |
158
|
|
|
if ($value === null) { |
159
|
|
|
$value = false; |
160
|
|
|
} |
161
|
|
|
$this->attributes['is_priority'] = $value; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Encrypt the user's google_2fa secret. |
166
|
|
|
* |
167
|
|
|
* @param string $value |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function setGoogle2faSecretAttribute($value) |
171
|
|
|
{ |
172
|
|
|
$this->attributes['google2fa_secret'] = encrypt($value); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Decrypt the user's google_2fa secret. |
177
|
|
|
* |
178
|
|
|
* @param string $value |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function getGoogle2faSecretAttribute($value) |
182
|
|
|
{ |
183
|
|
|
if (!empty($value)) { |
184
|
|
|
return decrypt($value); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
return null; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Encrypt and serialize the user's recovery codes. |
191
|
|
|
* |
192
|
|
|
* @param string[] $value |
193
|
|
|
* @return void |
194
|
|
|
*/ |
195
|
|
|
public function setRecoveryCodesAttribute($value) |
196
|
|
|
{ |
197
|
|
|
$this->attributes['recovery_codes'] = encrypt($value); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Decrypt and deserialize the user's recovery codes. |
202
|
|
|
* |
203
|
|
|
* @param string $value |
204
|
|
|
* @return string[] |
205
|
|
|
*/ |
206
|
|
|
public function getRecoveryCodesAttribute($value) |
207
|
|
|
{ |
208
|
|
|
if (!empty($value)) { |
209
|
|
|
return decrypt($value); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
return null; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// Role related functions |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns true if this user has the Applicant role. |
218
|
|
|
* |
219
|
|
|
* @return boolean |
220
|
|
|
*/ |
221
|
|
|
public function isApplicant(): bool |
222
|
|
|
{ |
223
|
|
|
// Currently, every user can create an Applicant profile and apply to jobs. |
224
|
|
|
return true; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns true if this user has the upgradedManager role. |
229
|
|
|
* |
230
|
|
|
* @return boolean |
231
|
|
|
*/ |
232
|
|
|
public function isUpgradedManager(): bool |
233
|
|
|
{ |
234
|
|
|
return $this->isAdmin() || $this->user_role->key === 'upgradedManager'; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Returns true this user has the demoManager role. |
239
|
|
|
* |
240
|
|
|
* @return boolean |
241
|
|
|
*/ |
242
|
|
|
public function isDemoManager(): bool |
243
|
|
|
{ |
244
|
|
|
// Currently, every non-upgradedManager user can be considered a demoManager. |
245
|
|
|
return !$this->isUpgradedManager(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Returns true if this user has the demoManager or upgradedManager role. |
250
|
|
|
* |
251
|
|
|
* @return boolean |
252
|
|
|
*/ |
253
|
|
|
public function isManager(): bool |
254
|
|
|
{ |
255
|
|
|
// Currently, every user can use the Manager portal as a demoManager. |
256
|
|
|
return $this->isDemoManager() || $this->isUpgradedManager(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Returns true if this user has the hr_advisor role. |
261
|
|
|
* |
262
|
|
|
* @return boolean |
263
|
|
|
*/ |
264
|
|
|
public function isHrAdvisor(): bool |
265
|
|
|
{ |
266
|
|
|
return $this->user_role->key === 'hr_advisor' || $this->isAdmin(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns true if this user has the Admin role. |
271
|
|
|
* |
272
|
|
|
* @return boolean |
273
|
|
|
*/ |
274
|
|
|
public function isAdmin(): bool |
275
|
|
|
{ |
276
|
|
|
return $this->user_role->key === 'admin'; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Check if the user has the specified role. |
281
|
|
|
* @param string $role This may be either 'applicant', 'manager', 'hr_advisor' or 'admin'. |
282
|
|
|
* @return boolean |
283
|
|
|
*/ |
284
|
|
|
public function hasRole($role) |
285
|
|
|
{ |
286
|
|
|
switch ($role) { |
287
|
|
|
case 'applicant': |
288
|
|
|
return $this->isApplicant(); |
289
|
|
|
case 'manager': |
290
|
|
|
return $this->isManager(); |
291
|
|
|
case 'hr_advisor': |
292
|
|
|
return $this->isHrAdvisor(); |
293
|
|
|
case 'admin': |
294
|
|
|
return $this->isAdmin(); |
295
|
|
|
default: |
296
|
|
|
return false; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Set this user to the specified role. |
302
|
|
|
* |
303
|
|
|
* @param string $role Must be either 'applicant', 'manager', 'hr_advisor' or 'admin'. |
304
|
|
|
* @return void |
305
|
|
|
*/ |
306
|
|
|
public function setRole(string $role): void |
307
|
|
|
{ |
308
|
|
|
$this->user_role()->associate(UserRole::where('key', $role)->firstOrFail()); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* OVERRIDE |
313
|
|
|
* Send the password reset notification. |
314
|
|
|
* |
315
|
|
|
* @param string $token |
316
|
|
|
* |
317
|
|
|
* @return void |
318
|
|
|
*/ |
319
|
|
|
public function sendPasswordResetNotification($token): void |
320
|
|
|
{ |
321
|
|
|
$this->notify(new ResetPasswordNotification($token)); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Gov identity has been confirmed either if: |
326
|
|
|
* - they have confirmed to NOT be in government, |
327
|
|
|
* - OR they've added a gov email. |
328
|
|
|
* |
329
|
|
|
* @return boolean |
330
|
|
|
*/ |
331
|
|
|
public function isGovIdentityConfirmed(): bool |
332
|
|
|
{ |
333
|
|
|
return $this->not_in_gov || !empty($this->gov_email); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Returns a user's full name. |
338
|
|
|
* |
339
|
|
|
* @return string |
340
|
|
|
*/ |
341
|
|
|
public function getFullNameAttribute(): string |
342
|
|
|
{ |
343
|
|
|
return $this->first_name . ' ' . $this->last_name; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|