User::isApplicant()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 2
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
use Illuminate\Support\Facades\App;
23
24
/**
25
 * Class User
26
 *
27
 * @property int $id
28
 * @property string $email
29
 * @property string $first_name
30
 * @property string $last_name
31
 * @property string $password
32
 * @property boolean $is_confirmed
33
 * @property boolean $is_priority
34
 * @property int $user_role_id
35
 * @property string $gov_email
36
 * @property boolean $not_in_gov
37
 * @property string $google2fa_secret
38
 * @property array $recovery_codes
39
 * @property int $department_id
40
 * @property string $contact_language
41
 * @property boolean $job_alerts
42
 * @property \Jenssegers\Date\Date $recovery_codes_generation_date
43
 * @property \Jenssegers\Date\Date $created_at
44
 * @property \Jenssegers\Date\Date $updated_at
45
 *
46
 * @property \App\Models\Applicant $applicant
47
 * @property \App\Models\Manager $manager
48
 * @property \App\Models\HrAdvisor $hr_advisor
49
 * @property \App\Models\ProfilePic $profile_pic
50
 * @property \App\Models\UserRole $user_role
51
 * @property \App\Models\Lookup\Department $department
52
 */
53
class User extends BaseModel implements
54
    // Laravel contracts for native login.
55
    AuthenticatableContract,
56
    CanResetPasswordContract,
57
    // Contract for use with Gates and Policies.
58
    AuthorizableContract
59
{
60
61
    // Traits for Laravel basic authentication.
62
    use Authenticatable;
63
    use CanResetPassword;
64
    // Trait for working with Gates and Policies.
65
    use Authorizable;
66
    // Trait for notifications.
67
    use Notifiable;
68
    // Trait for Backpack.
69
    use CrudTrait;
70
    // Trait for 2FA device memory.
71
    use RememberDeviceTrait;
72
73
    protected $casts = [
74
        'is_confirmed' => 'boolean',
75
        'is_priority' => 'boolean',
76
        'user_role_id' => 'int',
77
        'email' => 'string',
78
        'gov_email' => 'string',
79
        'not_in_gov' => 'boolean',
80
        'department_id' => 'int',
81
        'contact_language' => 'string',
82
        'job_alerts' => 'boolean',
83
    ];
84
85
    /**
86 22
     * @var string[] $dates
87
     */
88 22
    protected $dates = [
89
        'recovery_codes_generation_date',
90
    ];
91 37
92
    protected $fillable = [
93 37
        'first_name',
94
        'last_name',
95
        'email',
96
        'password',
97
        'is_priority',
98
        'gov_email',
99
        'not_in_gov',
100
        'google2fa_secret',
101 72
        'department_id',
102
        'contact_language',
103 72
        'job_alerts',
104
    ];
105
106
    protected $with = ['user_role'];
107
108
    protected $hidden = [
109
        'password',
110
        'remember_token',
111
        'remember_device_token',
112
        'recovery_codes_generation_date',
113
        'google2fa_secret',
114
        'recovery_codes',
115
    ];
116
117
    /**
118
     * The event map for the model.
119
     *
120
     * @var array
121
     */
122
    protected $dispatchesEvents = [
123
        'created' => UserCreated::class,
124
        'updated' => UserUpdated::class,
125
    ];
126
127
    public function applicant() //phpcs:ignore
128
    {
129
        return $this->hasOne(\App\Models\Applicant::class);
130
    }
131
132
    public function manager() //phpcs:ignore
133
    {
134
        return $this->hasOne(\App\Models\Manager::class);
135
    }
136 47
137
    public function hr_advisor() //phpcs:ignore
138 47
    {
139
        return $this->hasOne(\App\Models\HrAdvisor::class);
140
    }
141
142
    public function profile_pic() //phpcs:ignore
143
    {
144
        return $this->hasOne(\App\Models\ProfilePic::class);
145
    }
146
147
    public function user_role() //phpcs:ignore
148
    {
149
        return $this->belongsTo(\App\Models\UserRole::class);
150
    }
151
152
    public function department()
153
    {
154
        return $this->belongsTo(\App\Models\Lookup\Department::class);
155
    }
156
157
    public function setIsPriorityAttribute($value)
158
    {
159
        if ($value === null) {
160
            $value = false;
161
        }
162
        $this->attributes['is_priority'] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
163
    }
164
165
    /**
166
     * Encrypt the user's google_2fa secret.
167
     *
168
     * @param  string  $value
169
     * @return string
170
     */
171
    public function setGoogle2faSecretAttribute($value)
172
    {
173
        $this->attributes['google2fa_secret'] = encrypt($value);
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The function encrypt was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

173
        $this->attributes['google2fa_secret'] = /** @scrutinizer ignore-call */ encrypt($value);
Loading history...
174
    }
175
176
    /**
177
     * Decrypt the user's google_2fa secret.
178
     *
179
     * @param  string  $value
180
     * @return string
181
     */
182
    public function getGoogle2faSecretAttribute($value)
183
    {
184
        if (!empty($value)) {
185
            return decrypt($value);
0 ignored issues
show
Bug introduced by
The function decrypt was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
            return /** @scrutinizer ignore-call */ decrypt($value);
Loading history...
186
        }
187
        return null;
188
    }
189
190
    /**
191
     * Encrypt and serialize the user's recovery codes.
192
     *
193
     * @param  string[]  $value
194
     * @return void
195
     */
196
    public function setRecoveryCodesAttribute($value)
197
    {
198
        $this->attributes['recovery_codes'] = encrypt($value);
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The function encrypt was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
        $this->attributes['recovery_codes'] = /** @scrutinizer ignore-call */ encrypt($value);
Loading history...
199
    }
200
201
    /**
202
     * Decrypt and deserialize the user's recovery codes.
203
     *
204
     * @param  string  $value
205
     * @return string[]
206
     */
207
    public function getRecoveryCodesAttribute($value)
208
    {
209
        if (!empty($value)) {
210
            return decrypt($value);
0 ignored issues
show
Bug introduced by
The function decrypt was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

210
            return /** @scrutinizer ignore-call */ decrypt($value);
Loading history...
211
        }
212
        return null;
213
    }
214
215
    // Role related functions
216
217
    /**
218
     * Returns true if this user has the Applicant role.
219
     *
220
     * @return boolean
221
     */
222
    public function isApplicant(): bool
223
    {
224
        // Currently, every user can create an Applicant profile and apply to jobs.
225
        return true;
226
    }
227
228
    /**
229
     * Returns true if this user has the upgradedManager role.
230
     *
231
     * @return boolean
232
     */
233
    public function isUpgradedManager(): bool
234
    {
235
        return $this->isAdmin() || $this->user_role->key === 'upgradedManager';
236
    }
237
238
    /**
239
     * Returns true this user has the demoManager role.
240
     *
241
     * @return boolean
242
     */
243
    public function isDemoManager(): bool
244
    {
245
        // Currently, every non-upgradedManager user can be considered a demoManager.
246
        return !$this->isUpgradedManager();
247
    }
248
249
    /**
250
     * Returns true if this user has the demoManager or upgradedManager role.
251
     *
252
     * @return boolean
253
     */
254
    public function isManager(): bool
255
    {
256
        // Currently, every user can use the Manager portal as a demoManager.
257
        return $this->isDemoManager() || $this->isUpgradedManager();
258
    }
259
260
    /**
261
     * Returns true if this user has the hr_advisor role.
262
     *
263
     * @return boolean
264
     */
265
    public function isHrAdvisor(): bool
266
    {
267
        return $this->user_role->key === 'hr_advisor' || $this->isAdmin();
268
    }
269
270
    /**
271
     * Returns true if this user has the Admin role.
272
     *
273
     * @return boolean
274
     */
275
    public function isAdmin(): bool
276
    {
277
        return $this->user_role->key === 'admin';
278
    }
279
280
    /**
281
     * Check if the user has the specified role.
282
     * @param string $role This may be either 'applicant', 'manager', 'hr_advisor' or 'admin'.
283
     * @return boolean
284
     */
285
    public function hasRole($role)
286
    {
287
        switch ($role) {
288
            case 'applicant':
289
                return $this->isApplicant();
290
            case 'manager':
291
                return $this->isManager();
292
            case 'hr_advisor':
293
                return $this->isHrAdvisor();
294
            case 'admin':
295
                return $this->isAdmin();
296
            default:
297
                return false;
298
        }
299
    }
300
301
    /**
302
     * Set this user to the specified role.
303
     *
304
     * @param string $role Must be either 'applicant', 'manager', 'hr_advisor' or 'admin'.
305
     * @return void
306
     */
307
    public function setRole(string $role): void
308
    {
309
        $this->user_role()->associate(UserRole::where('key', $role)->firstOrFail());
310
    }
311
312
    /**
313
     * OVERRIDE
314
     * Send the password reset notification.
315
     *
316
     * @param  string  $token
317
     *
318
     * @return void
319
     */
320
    public function sendPasswordResetNotification($token): void
321
    {
322
        $locale = App::getLocale();
323
        $notification = new ResetPasswordNotification($token);
324
        $this->notify($notification->locale($locale));
325
    }
326
327
    /**
328
     * Gov identity has been confirmed either if:
329
     *  - they have confirmed to NOT be in government,
330
     *  - OR they've added a gov email.
331
     *
332
     * @return boolean
333
     */
334
    public function isGovIdentityConfirmed(): bool
335
    {
336
        return $this->not_in_gov || !empty($this->gov_email);
337
    }
338
339
    /**
340
     * Returns a user's full name.
341
     *
342
     * @return string
343
     */
344
    public function getFullNameAttribute(): string
345
    {
346
        return $this->first_name . ' ' . $this->last_name;
347
    }
348
}
349