ApiAuthorizer.php 3.98 KB
Newer Older
1
2
3
4
5
6
7
<?php


namespace Source\Authorization;

use Illuminate\Contracts\Auth\Guard;
use Source\Exceptions\AuthorizationException;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\Exceptions\EntityNotFoundException;
9
use Source\Gateways\GroupUser\GroupUserRepository;
10
11
12

class ApiAuthorizer implements Authorizer
{
13
14
15
    /**
     * @var \Illuminate\Contracts\Auth\Guard
     */
16
17
    protected Guard $guard;

18
19
20
21
22
    /**
     * @var \Source\Gateways\GroupUser\GroupUserRepository
     */
    protected GroupUserRepository $groupUserRepository;

23
    public function __construct(Guard $guard, GroupUserRepository $groupUserRepository)
24
25
    {
        $this->guard = $guard;
26
        $this->groupUserRepository = $groupUserRepository;
27
28
29
    }

    /**
30
     * @inheritDoc
31
     */
32
    public function protectAll(array $permissions): void
33
    {
34
35
        if (!$this->allowsAll($permissions)) {
            throw new AuthorizationException();
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        }
    }

    /**
     * @inheritDoc
     */
    public function allowsAll(array $permissions): bool
    {
        $groups = $this->getGroupsForCurrentUser();

        if (in_array(Permissions::ADMIN, $groups, true)) {
            return true;
        }

        foreach ($permissions as $permission) {
            if (!in_array($permission, $groups, true)) {
                return false;
            }
54
        }
55
56

        return true;
57
    }
58
59

    /**
60
61
     * @return string[]
     * @throws \Source\Exceptions\EntityNotFoundException
62
     */
63
    protected function getGroupsForCurrentUser(): array
64
    {
65
        $user = $this->guard->id();
66

67
68
        if (!$user) {
            return [];
69
70
        }

Jacob Priddy's avatar
Jacob Priddy committed
71
72
73
74
75
76
77
78
79
80
81
        return $this->getGroupsForUser($user);
    }

    /**
     * @param string $userId
     * @return string[]
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    protected function getGroupsForUser(string $userId): array
    {
        return static::groupNames($this->groupUserRepository->getGroupsForUser($userId));
82
83
84
85
86
87
88
89
90
91
    }

    /**
     * @param \Source\Entities\Group[] $groups
     * @return string[]
     */
    protected static function groupNames(array $groups): array
    {
        $groupNames = [];

92
        foreach ($groups as $group) {
93
            $groupNames[] = $group->getTitle();
94
95
        }

96
        return $groupNames;
97
98
99
100
101
    }

    /**
     * @inheritDoc
     */
102
    public function protectOne(array $permissions): void
103
    {
104
        if (!$this->allowsOne($permissions)) {
105
106
107
108
109
110
111
            throw new AuthorizationException();
        }
    }

    /**
     * @inheritDoc
     */
112
    public function allowsOne(array $permissions): bool
113
    {
114
115
116
117
        $groups = $this->getGroupsForCurrentUser();

        if (in_array(Permissions::ADMIN, $groups, true)) {
            return true;
118
        }
119
120
121
122
123
124
125
126

        foreach ($groups as $group) {
            if (in_array($group, $permissions, true)) {
                return true;
            }
        }

        return false;
127
    }
Jacob Priddy's avatar
Jacob Priddy committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

    /**
     * @inheritDoc
     */
    public function protect(string $permission): void
    {
        $this->protectAll([$permission]);
    }

    /**
     * @inheritDoc
     */
    public function protectAdminRights(string $userId, ?string $groupId = null): void
    {
        $user = $this->groupUserRepository->getUser($userId);

        if (!$user) {
            throw new EntityNotFoundException('User not found.');
        }

        $groups = $this->getGroupsForUser($userId);

        // Only admins have access to other admins
        if (in_array(Permissions::ADMIN, $groups, true)) {
            $this->protect(Permissions::ADMIN);
        }

        if ($groupId) {
            $group = $this->groupUserRepository->getGroup($groupId);
            if (!$group) {
                throw new EntityNotFoundException('Group not found.');
            }
            // Only admins can create or remove other admins
            if ($group->hasTitleOf(Permissions::ADMIN)) {
                $this->protect(Permissions::ADMIN);
            }
        }

        if ($user->hasFirstNameOf('admin')) {
            throw new AuthorizationException('You cannot modify the admin user.');
        }
    }
170
}