Total Complexity | 16 |
Total Lines | 163 |
Duplicated Lines | 0 % |
Coverage | 38.1% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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; |
||
|
|||
57 | // Trait for Backpack. |
||
58 | use CrudTrait; |
||
59 | |||
60 | protected $casts = [ |
||
1 ignored issue
–
show
|
|||
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
|
|||
68 | 'name', 'email', 'password', 'is_priority' |
||
69 | ]; |
||
70 | |||
71 | protected $with = ['user_role']; |
||
1 ignored issue
–
show
|
|||
72 | |||
73 | protected $hidden = [ |
||
1 ignored issue
–
show
|
|||
74 | 'password', 'remember_token', |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * The event map for the model. |
||
79 | * |
||
80 | * @var array |
||
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) |
||
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 |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * OVERRIDE |
||
194 | * Send the password reset notification. |
||
195 | * |
||
196 | * @param string $token |
||
2 ignored issues
–
show
|
|||
197 | * |
||
198 | * @return void |
||
199 | */ |
||
200 | public function sendPasswordResetNotification($token) |
||
1 ignored issue
–
show
|
|||
201 | { |
||
202 | $this->notify(new ResetPasswordNotification($token)); |
||
203 | } |
||
205 |