Passed
Push — task/common-translation-packag... ( dbb970...52324d )
by Tristan
06:50 queued 11s
created

User::hr_advisor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
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
        'google2fa_secret',
103
        'recovery_codes',
104
    ];
105
106
    /**
107
     * The event map for the model.
108
     *
109
     * @var array
110
     */
111
    protected $dispatchesEvents = [
112
        'created' => UserCreated::class,
113
        'updated' => UserUpdated::class,
114
    ];
115
116
    public function applicant() //phpcs:ignore
117
    {
118
        return $this->hasOne(\App\Models\Applicant::class);
119
    }
120
121
    public function manager() //phpcs:ignore
122
    {
123
        return $this->hasOne(\App\Models\Manager::class);
124
    }
125
126
    public function hr_advisor() //phpcs:ignore
127
    {
128
        return $this->hasOne(\App\Models\HrAdvisor::class);
129
    }
130
131
    public function profile_pic() //phpcs:ignore
132
    {
133
        return $this->hasOne(\App\Models\ProfilePic::class);
134
    }
135
136
    public function user_role() //phpcs:ignore
137
    {
138
        return $this->belongsTo(\App\Models\UserRole::class);
139
    }
140
141
    public function setIsPriorityAttribute($value)
142
    {
143
        if ($value === null) {
144
            $value = false;
145
        }
146
        $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...
147
    }
148
149
    /**
150
     * Ecrypt the user's google_2fa secret.
151
     *
152
     * @param  string  $value
153
     * @return string
154
     */
155
    public function setGoogle2faSecretAttribute($value)
156
    {
157
        $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

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

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

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

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