1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Policies; |
4
|
|
|
|
5
|
|
|
use App\Models\User; |
6
|
|
|
use App\Models\Applicant; |
7
|
|
|
use App\Models\JobApplication; |
8
|
|
|
use App\Models\JobPoster; |
9
|
|
|
use App\Models\Lookup\ApplicationStatus; |
10
|
|
|
use App\Policies\BasePolicy; |
11
|
|
|
|
12
|
|
|
class ApplicantPolicy extends BasePolicy |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Returns true if $user owns a job to which $applicant has applied. |
17
|
|
|
* |
18
|
|
|
* @param \App\Models\User $user Generic User object for checking Manager relationship to Job Poster. |
19
|
|
|
* @param \App\Models\Applicant $applicant Applicant object used within applications submitted to Job Poster. |
20
|
|
|
* @return boolean |
21
|
|
|
*/ |
22
|
|
|
protected function ownsJobApplicantAppliedTo(User $user, Applicant $applicant) |
23
|
|
|
{ |
24
|
|
|
$applicant_id = $applicant->id; |
25
|
|
|
$user_id = $user->id; |
26
|
|
|
return JobPoster::whereHas( |
27
|
|
|
'manager', |
28
|
|
|
function ($q) use ($user_id) { |
29
|
|
|
$q->where('user_id', $user_id); |
30
|
|
|
} |
31
|
|
|
)->whereHas( |
32
|
|
|
'submitted_applications', |
33
|
|
|
function ($q) use ($applicant_id) { |
34
|
|
|
$q->where('applicant_id', $applicant_id); |
35
|
|
|
} |
36
|
|
|
)->get()->isNotEmpty(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns true the $user is an hr_advisor which has claimed a job the applicant has applied to, where the job is closed. |
41
|
|
|
* |
42
|
|
|
* @param User $user |
43
|
|
|
* @param Applicant $applicant |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
protected function claimsJobApplicantAppliedTo(User $user, Applicant $applicant) |
47
|
|
|
{ |
48
|
|
|
if ($user->isHrAdvisor()) { |
49
|
|
|
$submittedApplications = JobApplication::where([ |
50
|
|
|
'applicant_id' => $applicant->id, |
51
|
|
|
'application_status_id' => ApplicationStatus::where('name', 'draft')->id |
52
|
|
|
]); |
53
|
|
|
return $submittedApplications->some(function ($application) use ($user) { |
54
|
|
|
return $user->can('manage', $application->job_poster) && $application->job_poster->isClosed(); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Determine whether the user can view the applicant. |
62
|
|
|
* |
63
|
|
|
* @param \App\Models\User $user User object making the view request. |
64
|
|
|
* @param \App\Models\Applicant $applicant Applicant object to be viewed. |
65
|
|
|
* @return boolean |
66
|
|
|
*/ |
67
|
|
|
public function view(User $user, Applicant $applicant) |
68
|
|
|
{ |
69
|
|
|
$authApplicant = $user->isApplicant() && |
70
|
|
|
$applicant->user->is($user); |
71
|
|
|
$authManager = $user->isManager() && $this->ownsJobApplicantAppliedTo($user, $applicant); |
72
|
|
|
$authHr = $user->isHrAdvisor() && $this->claimsJobApplicantAppliedTo($user, $applicant); |
|
|
|
|
73
|
|
|
return $authApplicant || $authManager || $authHr; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Determine whether the user can create applicants. |
78
|
|
|
* |
79
|
|
|
* @param \App\Models\User $user User object making the create request. |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
|
|
public function create(User $user) |
83
|
|
|
{ |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Determine whether the user can update the applicant. |
89
|
|
|
* |
90
|
|
|
* @param \App\Models\User $user User object making the update request. |
91
|
|
|
* @param \App\Models\Applicant $applicant Applicant object being updated. |
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
|
|
public function update(User $user, Applicant $applicant) |
95
|
|
|
{ |
96
|
|
|
return $user->isApplicant() && |
97
|
|
|
$applicant->user_id === $user->id; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Determine whether the user can delete the applicant. |
102
|
|
|
* |
103
|
|
|
* @param \App\Models\User $user User object making the delete request. |
104
|
|
|
* @param \App\Models\Applicant $applicant Applicant object being deleted. |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function delete(User $user, Applicant $applicant) |
108
|
|
|
{ |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Determine whether the user can restore the applicant. |
113
|
|
|
* |
114
|
|
|
* @param \App\Models\User $user User object making the restore request. |
115
|
|
|
* @param \App\Models\Applicant $applicant Applicant object being restored. |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
public function restore(User $user, Applicant $applicant) |
119
|
|
|
{ |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Determine whether the user can permanently delete the applicant. |
124
|
|
|
* |
125
|
|
|
* @param \App\Models\User $user User object making the forceDelete request. |
126
|
|
|
* @param \App\Models\Applicant $applicant Applicant object being forceDeleted. |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function forceDelete(User $user, Applicant $applicant) |
130
|
|
|
{ |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.