Passed
Push — task/user-api-endpoint ( 2803dd...45ddf1 )
by Chris
05:38
created

AuthServiceProvider::defineGates()   D

Complexity

Conditions 20
Paths 1

Size

Total Lines 42
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 42
rs 4.1666
cc 20
nc 1
nop 0

How to fix   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\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\HrAdvisor;
23
use App\Models\User;
24
use App\Policies\UserPolicy;
25
use App\Policies\JobPolicy;
26
use App\Policies\CoursePolicy;
27
use App\Policies\DegreePolicy;
28
use App\Policies\ManagerPolicy;
29
use App\Policies\ApplicantPolicy;
30
use App\Policies\ReferencePolicy;
31
use App\Policies\ApplicationPolicy;
32
use App\Policies\SkillDeclarationPolicy;
33
use App\Policies\WorkExperiencePolicy;
34
use App\Policies\WorkSamplePolicy;
35
use App\Policies\AssessmentPolicy;
36
use App\Policies\RatingGuideQuestionPolicy;
37
use App\Policies\RatingGuideAnswerPolicy;
38
use App\Policies\AssessmentPlanNotificationPolicy;
39
use App\Policies\HrAdvisorPolicy;
40
41
class AuthServiceProvider extends ServiceProvider
42
{
43
    /**
44
     * The policy mappings for the application.
45
     *
46
     * @var array
47
     */
48
    protected $policies = [
49
        User::class => UserPolicy::class,
50
        Applicant::class => ApplicantPolicy::class,
51
        Manager::class => ManagerPolicy::class,
52
        JobPoster::class => JobPolicy::class,
53
        JobApplication::class => ApplicationPolicy::class,
54
        Course::class => CoursePolicy::class,
55
        Degree::class => DegreePolicy::class,
56
        Reference::class => ReferencePolicy::class,
57
        SkillDeclaration::class => SkillDeclarationPolicy::class,
58
        WorkExperience::class => WorkExperiencePolicy::class,
59
        WorkSample::class => WorkSamplePolicy::class,
60
        Assessment::class => AssessmentPolicy::class,
61
        RatingGuideQuestion::class => RatingGuideQuestionPolicy::class,
62
        RatingGuideAnswer::class => RatingGuideAnswerPolicy::class,
63
        AssessmentPlanNotification::class =>  AssessmentPlanNotificationPolicy::class,
64
        HrAdvisor::class => HrAdvisorPolicy::class,
65
    ];
66
67
    /**
68
     * Define any authorization gates
69
     *
70
     * @return void
71
     */
72
    protected function defineGates(): void
73
    {
74
        Gate::define('view-assessment-plan', function ($user, $jobPoster) {
75
            return $user->isAdmin() ||
76
                $user->isManager() && $jobPoster->manager->user_id === $user->id;
77
        });
78
79
        /* Logged-in Users can view themselves. Admins can view themselves,
80
         * Managers/HR Advisors and Applicants but not other Admins. Managers can view
81
         * Applicants of their Job Posters. HR Advisors can view Managers
82
         * within their department, and any Applicants of Job Posters created
83
         * by those managers.
84
         */
85
86
        /* TODO: User roles/permissions are getting a little unruly. I needed to add an
87
         * additional check alongside isUpgradedManager() because we have an isAdmin()
88
         * passthrough on that method, which was causing issues on the hr_advisor/manager
89
         * reference.
90
         */
91
        Gate::define('view-user', function ($user, $userProfile) {
92
            return (
93
                    $user->id === $userProfile->id
94
                ) ||
95
                (
96
                    $user->isAdmin() &&
97
                    !$userProfile->isAdmin()
98
                ) ||
99
                (
100
                    ($user->isHrAdvisor() && !$userProfile->isAdmin() && $userProfile->isUpgradedManager()) &&
101
                        ($user->hr_advisor->department_id === $userProfile->manager->department_id)
102
                ) ||
103
                (
104
                    ($user->isHrAdvisor() && $userProfile->isApplicant()) &&
105
                    $user->can('claimsJobApplicantAppliedTo', $userProfile->applicant)
106
                ) ||
107
                (
108
                    (!$user->isAdmin() && $user->isUpgradedManager() && $userProfile->isApplicant()) &&
109
                    $user->can('ownsJobApplicantAppliedTo', $userProfile->applicant)
110
                ) ||
111
                (
112
                    ($user->isApplicant() && !$userProfile->isAdmin() && $userProfile->isUpgradedManager()) &&
113
                    $userProfile->can('ownsJobApplicantAppliedTo', $user->applicant)
114
                );
115
        });
116
    }
117
118
    public function register(): void
119
    {
120
    }
121
122
    /**
123
     * Register any authentication / authorization services.
124
     *
125
     * @return void
126
     */
127
    public function boot(): void
128
    {
129
        $this->registerPolicies();
130
131
        $this->defineGates();
132
    }
133
}
134