Passed
Push — feature/experience-skills-api ( 61a84a...f678ab )
by Tristan
07:17 queued 03:18
created

AuthServiceProvider   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 62
dl 0
loc 133
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 2 1
D defineGates() 0 79 24
A boot() 0 5 1
1
<?php
2
3
namespace App\Providers;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
7
use Illuminate\Support\Facades\Gate;
8
use App\Models\Applicant;
9
use App\Models\Course;
10
use App\Models\Degree;
11
use App\Models\Manager;
12
use App\Models\JobPoster;
13
use App\Models\Reference;
14
use App\Models\WorkSample;
15
use App\Models\JobApplication;
16
use App\Models\WorkExperience;
17
use App\Models\SkillDeclaration;
18
use App\Models\Assessment;
19
use App\Models\RatingGuideQuestion;
20
use App\Models\RatingGuideAnswer;
21
use App\Models\AssessmentPlanNotification;
22
use App\Models\ExperienceAward;
23
use App\Models\ExperienceCommunity;
24
use App\Models\ExperienceEducation;
25
use App\Models\ExperiencePersonal;
26
use App\Models\ExperienceSkill;
27
use App\Models\ExperienceWork;
28
use App\Models\HrAdvisor;
29
use App\Models\User;
30
use App\Policies\UserPolicy;
31
use App\Policies\JobPolicy;
32
use App\Policies\CoursePolicy;
33
use App\Policies\DegreePolicy;
34
use App\Policies\ManagerPolicy;
35
use App\Policies\ApplicantPolicy;
36
use App\Policies\ReferencePolicy;
37
use App\Policies\ApplicationPolicy;
38
use App\Policies\SkillDeclarationPolicy;
39
use App\Policies\WorkExperiencePolicy;
40
use App\Policies\WorkSamplePolicy;
41
use App\Policies\AssessmentPolicy;
42
use App\Policies\RatingGuideQuestionPolicy;
43
use App\Policies\RatingGuideAnswerPolicy;
44
use App\Policies\AssessmentPlanNotificationPolicy;
45
use App\Policies\ExperiencePolicy;
46
use App\Policies\ExperienceSkillPolicy;
47
use App\Policies\HrAdvisorPolicy;
48
49
class AuthServiceProvider extends ServiceProvider
50
{
51
    /**
52
     * The policy mappings for the application.
53
     *
54
     * @var array
55
     */
56
    protected $policies = [
57
        User::class => UserPolicy::class,
58
        Applicant::class => ApplicantPolicy::class,
59
        Manager::class => ManagerPolicy::class,
60
        JobPoster::class => JobPolicy::class,
61
        JobApplication::class => ApplicationPolicy::class,
62
        Course::class => CoursePolicy::class,
63
        Degree::class => DegreePolicy::class,
64
        Reference::class => ReferencePolicy::class,
65
        SkillDeclaration::class => SkillDeclarationPolicy::class,
66
        WorkExperience::class => WorkExperiencePolicy::class,
67
        WorkSample::class => WorkSamplePolicy::class,
68
        Assessment::class => AssessmentPolicy::class,
69
        RatingGuideQuestion::class => RatingGuideQuestionPolicy::class,
70
        RatingGuideAnswer::class => RatingGuideAnswerPolicy::class,
71
        AssessmentPlanNotification::class =>  AssessmentPlanNotificationPolicy::class,
72
        HrAdvisor::class => HrAdvisorPolicy::class,
73
        ExperienceWork::class => ExperiencePolicy::class,
74
        ExperienceAward::class => ExperiencePolicy::class,
75
        ExperiencePersonal::class => ExperiencePolicy::class,
76
        ExperienceCommunity::class => ExperiencePolicy::class,
77
        ExperienceEducation::class => ExperiencePolicy::class,
78
        ExperienceSkill::class => ExperienceSkillPolicy::class
79
    ];
80
81
    /**
82
     * Define any authorization gates
83
     *
84
     * @return void
85
     */
86
    protected function defineGates(): void
87
    {
88
        Gate::define('view-assessment-plan', function ($user, $jobPoster) {
89
            return $user->isAdmin() ||
90
                $user->isManager() && $jobPoster->manager->user_id === $user->id;
91
        });
92
93
        /*
94
         * Returns true if $user owns a job to which $applicant has applied.
95
         */
96
        Gate::define('owns-job-applicant-applied-to', function ($user, $applicant) {
97
            $applicant_id = $applicant->id;
98
            $user_id = $user->id;
99
            return JobPoster::whereHas(
100
                'manager',
101
                function ($q) use ($user_id): void {
102
                    $q->where('user_id', $user_id);
103
                }
104
            )->whereHas(
105
                'submitted_applications',
106
                function ($q) use ($applicant_id): void {
107
                    $q->where('applicant_id', $applicant_id);
108
                }
109
            )->get()->isNotEmpty();
110
        });
111
112
        /*
113
         * Returns true if the $user is an hr_advisor which has claimed a job the applicant has applied to,
114
         * where the job is closed.
115
         */
116
        Gate::define('claims-job-applicant-applied-to', function ($user, $applicant) {
117
            if ($user->isHrAdvisor()) {
118
                return $applicant->submitted_applications->some(function ($application) use ($user) {
119
                    return $user->can('manage', $application->job_poster) && $application->job_poster->isClosed();
120
                });
121
            }
122
            return false;
123
        });
124
125
        /* Logged-in Users can view themselves. Admins can view everyone. Managers can view
126
         * Applicants of their Job Posters. HR Advisors can view Managers
127
         * within their department, and any Applicants of Job Posters created
128
         * by those managers.
129
         */
130
131
        /* TODO: User roles/permissions are getting a little unruly. I needed to add an
132
         * additional check alongside isUpgradedManager() because we have an isAdmin()
133
         * passthrough on that method, which was causing issues on the hr_advisor/manager
134
         * reference.
135
         */
136
        Gate::define('view-user', function ($user, $userProfile) {
137
            return (
138
                // Any user can view themselves.
139
                $user->id === $userProfile->id) ||
140
                (
141
                    // Admins can view anyone.
142
                    $user->isAdmin()) ||
143
                (
144
                    // Managers should be able to view HR Advisors within their department.
145
                    $user->isUpgradedManager() && ($user->manager !== null)
146
                    && !$user->isAdmin()
147
                    && $userProfile->isHrAdvisor() && ($userProfile->hr_advisor !== null)
148
                    && !$userProfile->isAdmin()
149
                    && ($user->manager->department_id === $userProfile->hr_advisor->department_id)) ||
150
                (
151
                    // HR Advisors can view applicants that have applied to Job Posters that have been claimed.
152
                    ($user->isHrAdvisor() && $userProfile->applicant !== null) &&
153
                    Gate::forUser($user)->allows('claims-job-applicant-applied-to', $userProfile->applicant)) ||
154
                (
155
                    // Managers can view Applicants who have applied to their Job Posters.
156
                    (!$user->isAdmin() && $user->isUpgradedManager() && $userProfile->applicant !== null) &&
157
                    Gate::forUser($user)->allows('owns-job-applicant-applied-to', $userProfile->applicant)) ||
158
                (
159
                    // Manager profiles are viewable by any logged in User.
160
                    $user !== null && !$userProfile->isAdmin() && $userProfile->isUpgradedManager());
161
        });
162
163
        Gate::define('view-resources', function ($user) {
164
            return $user->isUpgradedManager() || $user->isHrAdvisor();
165
        });
166
    }
167
168
    public function register(): void
169
    {
170
    }
171
172
    /**
173
     * Register any authentication / authorization services.
174
     *
175
     * @return void
176
     */
177
    public function boot(): void
178
    {
179
        $this->registerPolicies();
180
181
        $this->defineGates();
182
    }
183
}
184