Passed
Push — task/user-api-endpoint ( 2803dd...45ddf1 )
by Chris
05:38
created

User::isHrAdvisor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Traits\TalentCloudCrudTrait as CrudTrait;
21
use App\Traits\RememberDeviceTrait;
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 \Jenssegers\Date\Date $recovery_codes_generation_date
39
 * @property \Jenssegers\Date\Date $created_at
40
 * @property \Jenssegers\Date\Date $updated_at
41
 *
42
 * @property \App\Models\Applicant $applicant
43
 * @property \App\Models\Manager $manager
44
 * @property \App\Models\HrAdvisor $hr_advisor
45
 * @property \App\Models\ProfilePic $profile_pic
46
 * @property \App\Models\UserRole $user_role
47
 */
48
class User extends BaseModel implements
49
    // Laravel contracts for native login.
50
    AuthenticatableContract,
51
    CanResetPasswordContract,
52
    // Contract for use with Gates and Policies.
53
    AuthorizableContract
54
    // Custom contract for use with openid login.
55
    // \App\Services\Auth\Contracts\OidcAuthenticatable.
56
{
57
58
    // Traits for Laravel basic authentication.
59
    use Authenticatable;
60
    use CanResetPassword;
61
    // Trait for working with Gates and Policies.
62
    use Authorizable;
63
    // Trait for notifications.
64
    use Notifiable;
65
    // Trait for Backpack.
66
    use CrudTrait;
67
    // Trait for 2FA device memory.
68
    use RememberDeviceTrait;
69
70
    protected $casts = [
71
        'is_confirmed' => 'boolean',
72
        'is_priority' => 'boolean',
73
        'user_role_id' => 'int',
74
        'email' => 'string',
75
        'gov_email' => 'string',
76
        'not_in_gov' => 'boolean',
77
    ];
78
79
    /**
80
     * @var string[] $dates
81
     */
82
    protected $dates = [
83
        'recovery_codes_generation_date',
84
    ];
85
86
    protected $fillable = [
87
        'first_name',
88
        'last_name',
89
        'email',
90
        'password',
91
        'is_priority',
92
        'gov_email',
93
        'not_in_gov',
94
        'google2fa_secret'
95
    ];
96
97
    protected $with = ['user_role'];
98
99
    protected $hidden = [
100
        'password',
101
        'remember_token',
102
        'remember_device_token',
103
        'recovery_codes_generation_date',
104
        'google2fa_secret',
105
        'recovery_codes',
106
    ];
107
108
    /**
109
     * The event map for the model.
110
     *
111
     * @var array
112
     */
113
    protected $dispatchesEvents = [
114
        'created' => UserCreated::class,
115
        'updated' => UserUpdated::class,
116
    ];
117
118
    public function applicant() //phpcs:ignore
119
    {
120
        return $this->hasOne(\App\Models\Applicant::class);
121
    }
122
123
    public function manager() //phpcs:ignore
124
    {
125
        return $this->hasOne(\App\Models\Manager::class);
126
    }
127
128
    public function hr_advisor() //phpcs:ignore
129
    {
130
        return $this->hasOne(\App\Models\HrAdvisor::class);
131
    }
132
133
    public function profile_pic() //phpcs:ignore
134
    {
135
        return $this->hasOne(\App\Models\ProfilePic::class);
136
    }
137
138
    public function user_role() //phpcs:ignore
139
    {
140
        return $this->belongsTo(\App\Models\UserRole::class);
141
    }
142
143
    public function setIsPriorityAttribute($value)
144
    {
145
        if ($value === null) {
146
            $value = false;
147
        }
148
        $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...
149
    }
150
151
    /**
152
     * Ecrypt the user's google_2fa secret.
153
     *
154
     * @param  string  $value
155
     * @return string
156
     */
157
    public function setGoogle2faSecretAttribute($value)
158
    {
159
        $this->attributes['google2fa_secret'] = encrypt($value);
0 ignored issues
show
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

159
        $this->attributes['google2fa_secret'] = /** @scrutinizer ignore-call */ encrypt($value);
Loading history...
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...
160
    }
161
162
    /**
163
     * Decrypt the user's google_2fa secret.
164
     *
165
     * @param  string  $value
166
     * @return string
167
     */
168
    public function getGoogle2faSecretAttribute($value)
169
    {
170
        if (!empty($value)) {
171
            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

171
            return /** @scrutinizer ignore-call */ decrypt($value);
Loading history...
172
        }
173
        return null;
174
    }
175
176
    /**
177
     * Ecrypt and serialize the user's recovery codes.
178
     *
179
     * @param  string[]  $value
180
     * @return void
181
     */
182
    public function setRecoveryCodesAttribute($value)
183
    {
184
        $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

184
        $this->attributes['recovery_codes'] = /** @scrutinizer ignore-call */ encrypt($value);
Loading history...
185
    }
186
187
    /**
188
     * Decrypt and deserialize the user's recovery codes.
189
     *
190
     * @param  string  $value
191
     * @return string[]
192
     */
193
    public function getRecoveryCodesAttribute($value)
194
    {
195
        if (!empty($value)) {
196
            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

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