Passed
Push — master ( 149f73...077d9b )
by Grant
06:13 queued 11s
created

User::department()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 3
ccs 0
cts 0
cp 0
rs 10
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
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 \Jenssegers\Date\Date $recovery_codes_generation_date
40
 * @property \Jenssegers\Date\Date $created_at
41
 * @property \Jenssegers\Date\Date $updated_at
42
 *
43
 * @property \App\Models\Applicant $applicant
44
 * @property \App\Models\Manager $manager
45
 * @property \App\Models\HrAdvisor $hr_advisor
46
 * @property \App\Models\ProfilePic $profile_pic
47
 * @property \App\Models\UserRole $user_role
48
 * @property \App\Models\Lookup\Department $department
49
 */
50
class User extends BaseModel implements
51
    // Laravel contracts for native login.
52
    AuthenticatableContract,
53
    CanResetPasswordContract,
54
    // Contract for use with Gates and Policies.
55
    AuthorizableContract
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
        'department_id' => 'int'
78
    ];
79
80
    /**
81
     * @var string[] $dates
82
     */
83
    protected $dates = [
84
        'recovery_codes_generation_date',
85
    ];
86 21
87
    protected $fillable = [
88 21
        'first_name',
89
        'last_name',
90
        'email',
91 34
        'password',
92
        'is_priority',
93 34
        'gov_email',
94
        'not_in_gov',
95
        'google2fa_secret',
96
        'department_id'
97
    ];
98
99
    protected $with = ['user_role'];
100
101 67
    protected $hidden = [
102
        'password',
103 67
        'remember_token',
104
        'remember_device_token',
105
        'recovery_codes_generation_date',
106
        'google2fa_secret',
107
        'recovery_codes',
108
    ];
109
110
    /**
111
     * The event map for the model.
112
     *
113
     * @var array
114
     */
115
    protected $dispatchesEvents = [
116
        'created' => UserCreated::class,
117
        'updated' => UserUpdated::class,
118
    ];
119
120
    public function applicant() //phpcs:ignore
121
    {
122
        return $this->hasOne(\App\Models\Applicant::class);
123
    }
124
125
    public function manager() //phpcs:ignore
126
    {
127
        return $this->hasOne(\App\Models\Manager::class);
128
    }
129
130
    public function hr_advisor() //phpcs:ignore
131
    {
132
        return $this->hasOne(\App\Models\HrAdvisor::class);
133
    }
134
135
    public function profile_pic() //phpcs:ignore
136 43
    {
137
        return $this->hasOne(\App\Models\ProfilePic::class);
138 43
    }
139
140
    public function user_role() //phpcs:ignore
141
    {
142
        return $this->belongsTo(\App\Models\UserRole::class);
143
    }
144
145
    public function department()
146
    {
147
        return $this->belongsTo(\App\Models\Lookup\Department::class);
148
    }
149
150
    public function setIsPriorityAttribute($value)
151
    {
152
        if ($value === null) {
153
            $value = false;
154
        }
155
        $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...
156
    }
157
158
    /**
159
     * Encrypt the user's google_2fa secret.
160
     *
161
     * @param  string  $value
162
     * @return string
163
     */
164
    public function setGoogle2faSecretAttribute($value)
165
    {
166
        $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

166
        $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...
167
    }
168
169
    /**
170
     * Decrypt the user's google_2fa secret.
171
     *
172
     * @param  string  $value
173
     * @return string
174
     */
175
    public function getGoogle2faSecretAttribute($value)
176
    {
177
        if (!empty($value)) {
178
            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

178
            return /** @scrutinizer ignore-call */ decrypt($value);
Loading history...
179
        }
180
        return null;
181
    }
182
183
    /**
184
     * Encrypt and serialize the user's recovery codes.
185
     *
186
     * @param  string[]  $value
187
     * @return void
188
     */
189
    public function setRecoveryCodesAttribute($value)
190
    {
191
        $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

191
        $this->attributes['recovery_codes'] = /** @scrutinizer ignore-call */ encrypt($value);
Loading history...
192
    }
193
194
    /**
195
     * Decrypt and deserialize the user's recovery codes.
196
     *
197
     * @param  string  $value
198
     * @return string[]
199
     */
200
    public function getRecoveryCodesAttribute($value)
201
    {
202
        if (!empty($value)) {
203
            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

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