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