AuthServiceProvider::defineGates()   D
last analyzed

Complexity

Conditions 24
Paths 1

Size

Total Lines 79
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 600

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 79
ccs 0
cts 0
cp 0
rs 4.1666
c 0
b 0
f 0
cc 24
nc 1
nop 0
crap 600

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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