|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Policies; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\User; |
|
6
|
|
|
use App\Models\JobPoster; |
|
7
|
|
|
use App\Policies\BasePolicy; |
|
8
|
|
|
use Illuminate\Support\Facades\Log; |
|
9
|
|
|
|
|
10
|
|
|
class JobPolicy extends BasePolicy |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Determine whether the user can view the job poster. |
|
15
|
|
|
* |
|
16
|
|
|
* @param \App\Models\?User $user User object making the request. |
|
17
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
18
|
|
|
* @return mixed |
|
19
|
|
|
*/ |
|
20
|
|
|
public function view(?User $user, JobPoster $jobPoster) |
|
21
|
|
|
{ |
|
22
|
|
|
// Anyone can view a published job past the open date |
|
23
|
|
|
// Managers can always view jobs they created. |
|
24
|
|
|
// Hr Advisors can view all jobs in their department. |
|
25
|
|
|
return $jobPoster->isPublic() || |
|
26
|
|
|
($user && |
|
27
|
|
|
$user->isManager() && |
|
28
|
|
|
$jobPoster->manager->user_id == $user->id) || |
|
29
|
|
|
($user && |
|
30
|
|
|
$user->isHrAdvisor() && |
|
31
|
|
|
$user->department_id === $jobPoster->department_id && |
|
32
|
|
|
$jobPoster->isVisibleToHr()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Any user is permitted to request a list of jobs, |
|
37
|
|
|
* but only the jobs they are permitted to *view* should be returned. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \App\Models\?User $user User object making the request. |
|
40
|
|
|
* @return boolean |
|
41
|
|
|
*/ |
|
42
|
|
|
public function viewAny(?User $user) |
|
43
|
|
|
{ |
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Determine whether the user can create job posters. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \App\Models\User $user User to test against. |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
*/ |
|
53
|
|
|
public function create(User $user) |
|
54
|
|
|
{ |
|
55
|
|
|
// Any manager can create a new job poster. |
|
56
|
|
|
return $user->isManager(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Determine whether the user can update the job poster. |
|
61
|
|
|
* |
|
62
|
|
|
* @param \App\Models\User $user User object making the request. |
|
63
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public function update(User $user, JobPoster $jobPoster) |
|
67
|
|
|
{ |
|
68
|
|
|
// Only managers can edit jobs, and only their own, managers can't publish jobs or edit published jobs. |
|
69
|
|
|
return $user->isManager() && |
|
70
|
|
|
$jobPoster->manager->user->id == $user->id && |
|
71
|
|
|
$jobPoster->isEditable(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Determine whether the user can delete the job poster. |
|
76
|
|
|
* |
|
77
|
|
|
* @param \App\Models\User $user User object making the request. |
|
78
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
79
|
|
|
* |
|
80
|
|
|
* @return boolean |
|
81
|
|
|
*/ |
|
82
|
|
|
public function delete(User $user, JobPoster $jobPoster): bool |
|
83
|
|
|
{ |
|
84
|
|
|
// Jobs can only be deleted when they're in the 'draft' |
|
85
|
|
|
// state, and only by managers that created them. |
|
86
|
|
|
return $user->isManager() && |
|
87
|
|
|
$jobPoster->manager->user->id == $user->id && |
|
88
|
|
|
$jobPoster->isEditable(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Determine whether the user can review applications to the job poster. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \App\Models\User $user User object making the request. |
|
95
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function reviewApplicationsFor(User $user, JobPoster $jobPoster) |
|
99
|
|
|
{ |
|
100
|
|
|
// Managers can only review applications their own jobs. |
|
101
|
|
|
// HR Advisors can review applications for jobs they manage. |
|
102
|
|
|
// The job must always be closed. |
|
103
|
|
|
$authManager = $user->isManager() && $jobPoster->manager->user->id == $user->id; |
|
104
|
|
|
$authHr = $user->isHrAdvisor() && $this->manage($user, $jobPoster); |
|
105
|
|
|
|
|
106
|
|
|
return $jobPoster->isClosed() && ($authManager || $authHr); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Determine whether the user is a Manager or an HR Advisor with permission to manage this job. |
|
111
|
|
|
* |
|
112
|
|
|
* @param \App\Models\User $user User object making the request. |
|
113
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
114
|
|
|
* @return boolean |
|
115
|
|
|
*/ |
|
116
|
|
|
public function manage(User $user, JobPoster $jobPoster) |
|
117
|
|
|
{ |
|
118
|
|
|
return ($user->isManager() && |
|
119
|
|
|
$jobPoster->manager->user->id == $user->id) || |
|
120
|
|
|
($user->isHrAdvisor() |
|
121
|
|
|
&& $this->view($user, $jobPoster) |
|
122
|
|
|
&& $user->hr_advisor->claimed_job_ids->contains($jobPoster->id)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Determine whether the user can view the comments. |
|
127
|
|
|
* |
|
128
|
|
|
* @param \App\Models\User $user User object making the request. |
|
129
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
130
|
|
|
* @return boolean |
|
131
|
|
|
*/ |
|
132
|
|
|
public function viewComments(User $user, JobPoster $jobPoster): bool |
|
133
|
|
|
{ |
|
134
|
|
|
// Only the manager that created the job can view the comment. |
|
135
|
|
|
// Only Hr advisors who have claimed a job can view the comments. |
|
136
|
|
|
return $this->manage($user, $jobPoster); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Determine whether the user can create a comment |
|
141
|
|
|
* |
|
142
|
|
|
* @param \App\Models\User $user User object making the request. |
|
143
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
144
|
|
|
* @return boolean |
|
145
|
|
|
*/ |
|
146
|
|
|
public function storeComment(User $user, JobPoster $jobPoster): bool |
|
147
|
|
|
{ |
|
148
|
|
|
// Only the manager that created the job can view the comment. |
|
149
|
|
|
// Only Hr advisors who have claimed a job can view the comments. |
|
150
|
|
|
return $this->viewComments($user, $jobPoster); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Determine whether the user can 'claim' this job. |
|
155
|
|
|
* |
|
156
|
|
|
* @param \App\Models\User $user User object making the request. |
|
157
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
158
|
|
|
* @return boolean |
|
159
|
|
|
*/ |
|
160
|
|
|
public function claim(User $user, JobPoster $jobPoster): bool |
|
161
|
|
|
{ |
|
162
|
|
|
return $user->isHrAdvisor() && $this->view($user, $jobPoster); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Determine whether the user can 'unclaim' this job. |
|
167
|
|
|
* |
|
168
|
|
|
* @param \App\Models\User $user User object making the request. |
|
169
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
170
|
|
|
* @return boolean |
|
171
|
|
|
*/ |
|
172
|
|
|
public function unClaim(User $user, JobPoster $jobPoster): bool |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->claim($user, $jobPoster); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Determine whether the user can view assessment plan. |
|
179
|
|
|
* |
|
180
|
|
|
* @param \App\Models\User $user User object making the request. |
|
181
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
182
|
|
|
* @return boolean |
|
183
|
|
|
*/ |
|
184
|
|
|
public function viewAssessmentPlan(User $user, JobPoster $jobPoster): bool |
|
185
|
|
|
{ |
|
186
|
|
|
return $user->isAdmin() || |
|
187
|
|
|
$user->isManager() && $jobPoster->manager->user_id === $user->id || |
|
188
|
|
|
$user->isHrAdvisor() && $jobPoster->hr_advisors->contains('user_id', $user->id); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Determine whether the user can download CSV file of applicants who have applied to job. |
|
193
|
|
|
* |
|
194
|
|
|
* @param \App\Models\User $user User object making the request. |
|
195
|
|
|
* @param \App\Models\JobPoster $jobPoster Job Poster object being acted upon. |
|
196
|
|
|
* @return boolean |
|
197
|
|
|
*/ |
|
198
|
|
|
public function downloadApplicants(User $user, JobPoster $jobPoster): bool |
|
199
|
|
|
{ |
|
200
|
|
|
return $user->isAdmin() && $jobPoster->isPublic(); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|