|
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 Illuminate\Auth\Authenticatable; |
|
11
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword; |
|
12
|
|
|
use Illuminate\Foundation\Auth\Access\Authorizable; |
|
13
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
|
14
|
|
|
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; |
|
15
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
|
16
|
|
|
use Illuminate\Notifications\Notifiable; |
|
17
|
|
|
use App\Events\UserCreated; |
|
18
|
|
|
use App\Events\UserUpdated; |
|
19
|
|
|
use App\Notifications\ResetPasswordNotification; |
|
20
|
|
|
use App\CRUD\TalentCloudCrudTrait as CrudTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class User |
|
24
|
|
|
* |
|
25
|
|
|
* @property int $id |
|
26
|
|
|
* @property string $email |
|
27
|
|
|
* @property string $first_name |
|
28
|
|
|
* @property string $last_name |
|
29
|
|
|
* @property string $password |
|
30
|
|
|
* @property boolean $is_confirmed |
|
31
|
|
|
* @property boolean $is_priority |
|
32
|
|
|
* @property int $user_role_id |
|
33
|
|
|
* @property string $gov_email |
|
34
|
|
|
* @property boolean $not_in_gov |
|
35
|
|
|
* @property \Jenssegers\Date\Date $created_at |
|
36
|
|
|
* @property \Jenssegers\Date\Date $updated_at |
|
37
|
|
|
* |
|
38
|
|
|
* @property \App\Models\Applicant $applicant |
|
39
|
|
|
* @property \App\Models\Manager $manager |
|
40
|
|
|
* @property \App\Models\HrAdvisor $hr_advisor |
|
41
|
|
|
* @property \App\Models\ProfilePic $profile_pic |
|
42
|
|
|
* @property \App\Models\UserRole $user_role |
|
43
|
|
|
*/ |
|
44
|
|
|
class User extends BaseModel implements |
|
45
|
|
|
// Laravel contracts for native login. |
|
46
|
|
|
AuthenticatableContract, |
|
47
|
|
|
CanResetPasswordContract, |
|
48
|
|
|
// Contract for use with Gates and Policies. |
|
49
|
|
|
AuthorizableContract |
|
50
|
|
|
// Custom contract for use with openid login. |
|
51
|
|
|
// \App\Services\Auth\Contracts\OidcAuthenticatable. |
|
52
|
|
|
{ |
|
53
|
|
|
|
|
54
|
|
|
// Traits for Laravel basic authentication. |
|
55
|
|
|
use Authenticatable; |
|
56
|
|
|
use CanResetPassword; |
|
57
|
|
|
// Trait for working with Gates and Policies. |
|
58
|
|
|
use Authorizable; |
|
59
|
|
|
// Trait for notifications. |
|
60
|
|
|
use Notifiable; |
|
61
|
|
|
// Trait for Backpack. |
|
62
|
|
|
use CrudTrait; |
|
63
|
|
|
|
|
64
|
|
|
protected $casts = [ |
|
|
|
|
|
|
65
|
|
|
'is_confirmed' => 'boolean', |
|
66
|
|
|
'is_priority' => 'boolean', |
|
67
|
|
|
'user_role_id' => 'int', |
|
68
|
|
|
'email' => 'string', |
|
69
|
|
|
'gov_email' => 'string', |
|
70
|
|
|
'not_in_gov' => 'boolean', |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
protected $fillable = [ |
|
|
|
|
|
|
74
|
|
|
'first_name', 'last_name', 'email', 'password', 'is_priority', 'gov_email', 'not_in_gov' |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
protected $with = ['user_role']; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
protected $hidden = [ |
|
|
|
|
|
|
80
|
|
|
'password', 'remember_token', |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* The event map for the model. |
|
85
|
|
|
* |
|
86
|
|
|
* @var array |
|
87
|
|
|
*/ |
|
88
|
|
|
protected $dispatchesEvents = [ |
|
89
|
|
|
'created' => UserCreated::class, |
|
90
|
|
|
'updated' => UserUpdated::class, |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
public function applicant() //phpcs:ignore |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->hasOne(\App\Models\Applicant::class); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function manager() //phpcs:ignore |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->hasOne(\App\Models\Manager::class); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function hr_advisor() //phpcs:ignore |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->hasOne(\App\Models\HrAdvisor::class); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function profile_pic() //phpcs:ignore |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->hasOne(\App\Models\ProfilePic::class); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function user_role() //phpcs:ignore |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->belongsTo(\App\Models\UserRole::class); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function setIsPriorityAttribute($value) |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
if ($value === null) { |
|
121
|
|
|
$value = false; |
|
122
|
|
|
} |
|
123
|
|
|
$this->attributes['is_priority'] = $value; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Role related functions. |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns true if this user has the Applicant role. |
|
130
|
|
|
* |
|
131
|
|
|
* @return boolean |
|
|
|
|
|
|
132
|
|
|
*/ |
|
133
|
|
|
public function isApplicant(): bool |
|
134
|
|
|
{ |
|
135
|
|
|
// Currently, every user can create an Applicant profile and apply to jobs. |
|
136
|
|
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns true if this user has the upgradedManager role. |
|
141
|
|
|
* |
|
142
|
|
|
* @return boolean |
|
|
|
|
|
|
143
|
|
|
*/ |
|
144
|
|
|
public function isUpgradedManager(): bool |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->isAdmin() || $this->user_role->name === 'upgradedManager'; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Returns true this user has the demoManager role. |
|
151
|
|
|
* |
|
152
|
|
|
* @return boolean |
|
|
|
|
|
|
153
|
|
|
*/ |
|
154
|
|
|
public function isDemoManager(): bool |
|
155
|
|
|
{ |
|
156
|
|
|
// Currently, every non-upgradedManager user can be considered a demoManager. |
|
157
|
|
|
return !$this->isUpgradedManager(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Returns true if this user has the demoManager or upgradedManager role. |
|
162
|
|
|
* |
|
163
|
|
|
* @return boolean |
|
|
|
|
|
|
164
|
|
|
*/ |
|
165
|
|
|
public function isManager(): bool |
|
166
|
|
|
{ |
|
167
|
|
|
// Currently, every user can use the Manager portal as a demoManager. |
|
168
|
|
|
return $this->isDemoManager() || $this->isUpgradedManager(); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns true if this user has the hr_advisor role. |
|
173
|
|
|
* |
|
174
|
|
|
* @return boolean |
|
|
|
|
|
|
175
|
|
|
*/ |
|
176
|
|
|
public function isHrAdvisor(): bool |
|
177
|
|
|
{ |
|
178
|
|
|
// Currently, every user can use the Manager portal as a demoManager. |
|
179
|
|
|
return $this->user_role->name === 'hr_advisor'; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Returns true if this user has the Admin role. |
|
184
|
|
|
* |
|
185
|
|
|
* @return boolean |
|
|
|
|
|
|
186
|
|
|
*/ |
|
187
|
|
|
public function isAdmin(): bool |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->user_role->name === 'admin'; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Check if the user has the specified role. |
|
194
|
|
|
* @param string $role This may be either 'applicant', 'manager', 'hr_advisor' or 'admin'. |
|
195
|
|
|
* @return boolean |
|
196
|
|
|
*/ |
|
197
|
|
|
public function hasRole($role) |
|
|
|
|
|
|
198
|
|
|
{ |
|
199
|
|
|
switch ($role) { |
|
200
|
|
|
case 'applicant': |
|
201
|
|
|
return $this->isApplicant(); |
|
202
|
|
|
case 'manager': |
|
203
|
|
|
return $this->isManager(); |
|
204
|
|
|
case 'hr_advisor': |
|
205
|
|
|
return $this->isHrAdvisor(); |
|
206
|
|
|
case 'admin': |
|
207
|
|
|
return $this->isAdmin(); |
|
208
|
|
|
default: |
|
209
|
|
|
return false; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Set this user to the specified role. |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $role Must be either 'applicant', 'manager', 'hr_advisor' or 'admin'. |
|
217
|
|
|
* @return void |
|
|
|
|
|
|
218
|
|
|
*/ |
|
219
|
|
|
public function setRole(string $role): void |
|
220
|
|
|
{ |
|
221
|
|
|
$this->user_role()->associate(UserRole::where('name', $role)->firstOrFail()); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* OVERRIDE |
|
226
|
|
|
* Send the password reset notification. |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $token |
|
|
|
|
|
|
229
|
|
|
* |
|
230
|
|
|
* @return void |
|
|
|
|
|
|
231
|
|
|
*/ |
|
232
|
|
|
public function sendPasswordResetNotification($token): void |
|
|
|
|
|
|
233
|
|
|
{ |
|
234
|
|
|
$this->notify(new ResetPasswordNotification($token)); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Gov identity has been confirmed either if: |
|
239
|
|
|
* - they have confirmed to NOT be in government, |
|
240
|
|
|
* - OR they've added a gov email. |
|
241
|
|
|
* |
|
242
|
|
|
* @return boolean |
|
|
|
|
|
|
243
|
|
|
*/ |
|
244
|
|
|
public function isGovIdentityConfirmed(): bool |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->not_in_gov || !empty($this->gov_email); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Returns a user's full name. |
|
251
|
|
|
* |
|
252
|
|
|
* @return string |
|
|
|
|
|
|
253
|
|
|
*/ |
|
254
|
|
|
public function getFullNameAttribute(): string |
|
255
|
|
|
{ |
|
256
|
|
|
return $this->first_name . ' ' . $this->last_name; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|