Passed
Pull Request — dev (#1344)
by Chris
05:58
created

User::hasAnyRole()   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
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 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 \Jenssegers\Date\Date $created_at
33
 * @property \Jenssegers\Date\Date $updated_at
34
 *
35
 * @property \App\Models\Applicant $applicant
36
 * @property \App\Models\Manager $manager
37
 * @property \App\Models\ProfilePic $profile_pic
38
 * @property \App\Models\UserRole $user_role
39
 */
40
class User extends BaseModel implements
41
    // Laravel contracts for native login.
42
    AuthenticatableContract,
43
    CanResetPasswordContract,
44
    // Contract for use with Gates and Policies.
45
    AuthorizableContract
46
    // Custom contract for use with openid login.
47
    // \App\Services\Auth\Contracts\OidcAuthenticatable.
48
{
49
50
    // Traits for Laravel basic authentication.
51
    use Authenticatable;
52
    use CanResetPassword;
53
    // Trait for working with Gates and Policies.
54
    use Authorizable;
55
    // Trait for notifications.
56
    use Notifiable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Notifications\Notifiable requires the property $phone_number which is not provided by App\Models\User.
Loading history...
57
    // Trait for Backpack.
58
    use CrudTrait;
59
60
    protected $casts = [
1 ignored issue
show
introduced by
Property \App\Models\User::$casts does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
61
        'is_confirmed' => 'boolean',
62
        'is_priority' => 'boolean',
63
        'user_role_id' => 'int',
64
        'email' => 'string',
65
    ];
66
67
    protected $fillable = [
1 ignored issue
show
introduced by
Property \App\Models\User::$fillable does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
68
        'name', 'email', 'password', 'is_priority'
69
    ];
70
71
    protected $with = ['user_role'];
1 ignored issue
show
introduced by
Property \App\Models\User::$with does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
72
73
    protected $hidden = [
1 ignored issue
show
introduced by
Property \App\Models\User::$hidden does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
74
        'password', 'remember_token',
75
    ];
76
77
    /**
78
     * The event map for the model.
79
     *
80
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \App\Models\User::$dispatchesEvents does not specify type hint for its items.
Loading history...
81
     */
82
    protected $dispatchesEvents = [
83
        'created' => UserCreated::class,
84
        'updated' => UserUpdated::class,
85
    ];
86 22
87
    public function applicant() //phpcs:ignore
88 22
    {
89
        return $this->hasOne(\App\Models\Applicant::class);
90
    }
91 37
92
    public function manager() //phpcs:ignore
93 37
    {
94
        return $this->hasOne(\App\Models\Manager::class);
95
    }
96
97
    public function profile_pic() //phpcs:ignore
98
    {
99
        return $this->hasOne(\App\Models\ProfilePic::class);
100
    }
101 72
102
    public function user_role() //phpcs:ignore
103 72
    {
104
        return $this->belongsTo(\App\Models\UserRole::class);
105
    }
106
107
    // Role related functions
108
109
    /**
110
     * Returns true if this user has the Applicant role.
111
     *
112
     * @return boolean
113
     */
114
    public function isApplicant(): bool
115
    {
116
        // Currently, every user can create an Applicant profile and apply to jobs.
117
        return true;
118
    }
119
120
    /**
121
     * Returns true if this user has the upgradedManager role.
122
     *
123
     * @return boolean
124
     */
125
    public function isUpgradedManager(): bool
126
    {
127
        return $this->user_role->name === 'upgradedManager';
128
    }
129
130
    /**
131
     * Returns true this user has the demoManager role.
132
     *
133
     * @return boolean
134
     */
135
    public function isDemoManager(): bool
136 47
    {
137
        // Currently, every non-upgradedManager user can be considered a demoManager.
138 47
        return !$this->isUpgradedManager();
139
    }
140
141
    /**
142
     * Returns true if this user has the demoManager or upgradedManager role.
143
     *
144
     * @return boolean
145
     */
146
    public function isManager(): bool
147
    {
148
        // Currently, every user can use the Manager portal as a demoManager.
149
        return $this->isDemoManager() || $this->isUpgradedManager();
150
    }
151
152
    /**
153
     * Returns true if this user has the Admin role.
154
     *
155
     * @return boolean
156
     */
157
    public function isAdmin(): bool
158
    {
159
        return $this->user_role->name === 'admin';
160
    }
161
162
    /**
163
    * Check if the user has the specified role.
164
    * @param string $role This may be either 'applicant', 'manager' or 'admin'.
165
    * @return boolean
166
    */
167
    public function hasRole($role)
1 ignored issue
show
introduced by
Method \App\Models\User::hasRole() does not have parameter type hint for its parameter $role but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \App\Models\User::hasRole() does not have return type hint for its return value but it should be possible to add it based on @return annotation "boolean".
Loading history...
Coding Style introduced by
Type hint "string" missing for $role
Loading history...
168
    {
169
        switch ($role) {
170
            case 'applicant':
171
                return $this->isApplicant();
172
            case 'manager':
173
                return $this->isManager();
174
            case 'admin':
175
                return $this->isAdmin();
176
            default:
177
                return false;
178
        }
179
    }
180
181
    /**
182
     * Set this user to the specified role.
183
     *
184
     * @param string $role Must be either 'applicant', 'manager' or 'admin.
185
    * @return void
186
    */
187
    public function setRole(string $role): void
188
    {
189
        $this->user_role()->associate(UserRole::where('name', $role)->firstOrFail());
190
    }
191
192
    /**
193
     * OVERRIDE
194
     * Send the password reset notification.
195
     *
196
     * @param  string  $token
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
197
     *
198
     * @return void
199
     */
200
    public function sendPasswordResetNotification($token)
1 ignored issue
show
introduced by
Method \App\Models\User::sendPasswordResetNotification() does not have parameter type hint for its parameter $token but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \App\Models\User::sendPasswordResetNotification() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
Coding Style introduced by
Type hint "string" missing for $token
Loading history...
201
    {
202
        $this->notify(new ResetPasswordNotification($token));
203
    }
204
}
205