Passed
Push — feature/experience-api ( 5f417f...bbfe27 )
by Tristan
04:24 queued 22s
created

AuthServiceProvider   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 61
c 0
b 0
f 0
dl 0
loc 132
rs 10

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