xref: /webtrees/app/Auth.php (revision 138139c24b30d9bfcf87bbf2c44e9b9c3dd5686d)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
22e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
23e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyAccessDeniedException;
24e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
25d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
26e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
27e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
28e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaAccessDeniedException;
29e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
30e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteAccessDeniedException;
31e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
32e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\RecordAccessDeniedException;
33e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\RecordNotFoundException;
34e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryAccessDeniedException;
35e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
36e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceAccessDeniedException;
37e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
389867b2f0SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
39e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
4079529c87SGreg Roach
41a25f0a04SGreg Roach/**
4276692c8bSGreg Roach * Authentication.
43a25f0a04SGreg Roach */
44c1010edaSGreg Roachclass Auth
45c1010edaSGreg Roach{
464b9ff166SGreg Roach    // Privacy constants
4716d6367aSGreg Roach    public const PRIV_PRIVATE = 2; // Allows visitors to view the item
4816d6367aSGreg Roach    public const PRIV_USER    = 1; // Allows members to access the item
4916d6367aSGreg Roach    public const PRIV_NONE    = 0; // Allows managers to access the item
5016d6367aSGreg Roach    public const PRIV_HIDE    = -1; // Hide the item to all users
514b9ff166SGreg Roach
52a25f0a04SGreg Roach    /**
53a25f0a04SGreg Roach     * Are we currently logged in?
54a25f0a04SGreg Roach     *
55cbc1590aSGreg Roach     * @return bool
56a25f0a04SGreg Roach     */
578f53f488SRico Sonntag    public static function check(): bool
58c1010edaSGreg Roach    {
594b9ff166SGreg Roach        return self::id() !== null;
60a25f0a04SGreg Roach    }
61a25f0a04SGreg Roach
62a25f0a04SGreg Roach    /**
63a25f0a04SGreg Roach     * Is the specified/current user an administrator?
64a25f0a04SGreg Roach     *
65e5a6b4d4SGreg Roach     * @param UserInterface|null $user
66a25f0a04SGreg Roach     *
67cbc1590aSGreg Roach     * @return bool
68a25f0a04SGreg Roach     */
69e5a6b4d4SGreg Roach    public static function isAdmin(UserInterface $user = null): bool
70c1010edaSGreg Roach    {
71cb923727SGreg Roach        $user = $user ?? self::user();
72a25f0a04SGreg Roach
737c4add84SGreg Roach        return $user->getPreference(User::PREF_IS_ADMINISTRATOR) === '1';
74a25f0a04SGreg Roach    }
75a25f0a04SGreg Roach
76a25f0a04SGreg Roach    /**
774b9ff166SGreg Roach     * Is the specified/current user a manager of a tree?
78a25f0a04SGreg Roach     *
7984caa210SGreg Roach     * @param Tree               $tree
80e5a6b4d4SGreg Roach     * @param UserInterface|null $user
81a25f0a04SGreg Roach     *
82cbc1590aSGreg Roach     * @return bool
83a25f0a04SGreg Roach     */
84e5a6b4d4SGreg Roach    public static function isManager(Tree $tree, UserInterface $user = null): bool
85c1010edaSGreg Roach    {
86cb923727SGreg Roach        $user = $user ?? self::user();
87a25f0a04SGreg Roach
887c4add84SGreg Roach        return self::isAdmin($user) || $tree->getUserPreference($user, User::PREF_TREE_ROLE) === User::ROLE_MANAGER;
89a25f0a04SGreg Roach    }
90a25f0a04SGreg Roach
91a25f0a04SGreg Roach    /**
924b9ff166SGreg Roach     * Is the specified/current user a moderator of a tree?
93a25f0a04SGreg Roach     *
9484caa210SGreg Roach     * @param Tree               $tree
95e5a6b4d4SGreg Roach     * @param UserInterface|null $user
96a25f0a04SGreg Roach     *
97cbc1590aSGreg Roach     * @return bool
98a25f0a04SGreg Roach     */
99e5a6b4d4SGreg Roach    public static function isModerator(Tree $tree, UserInterface $user = null): bool
100c1010edaSGreg Roach    {
101cb923727SGreg Roach        $user = $user ?? self::user();
102a25f0a04SGreg Roach
1037c4add84SGreg Roach        return self::isManager($tree, $user) || $tree->getUserPreference($user, User::PREF_TREE_ROLE) === User::ROLE_MODERATOR;
104a25f0a04SGreg Roach    }
105a25f0a04SGreg Roach
106a25f0a04SGreg Roach    /**
1074b9ff166SGreg Roach     * Is the specified/current user an editor of a tree?
108a25f0a04SGreg Roach     *
10984caa210SGreg Roach     * @param Tree               $tree
110e5a6b4d4SGreg Roach     * @param UserInterface|null $user
111a25f0a04SGreg Roach     *
112cbc1590aSGreg Roach     * @return bool
113a25f0a04SGreg Roach     */
114e5a6b4d4SGreg Roach    public static function isEditor(Tree $tree, UserInterface $user = null): bool
115c1010edaSGreg Roach    {
116cb923727SGreg Roach        $user = $user ?? self::user();
117a25f0a04SGreg Roach
1187c4add84SGreg Roach        return self::isModerator($tree, $user) || $tree->getUserPreference($user, User::PREF_TREE_ROLE) === 'edit';
119a25f0a04SGreg Roach    }
120a25f0a04SGreg Roach
121a25f0a04SGreg Roach    /**
1224b9ff166SGreg Roach     * Is the specified/current user a member of a tree?
123a25f0a04SGreg Roach     *
12484caa210SGreg Roach     * @param Tree               $tree
125e5a6b4d4SGreg Roach     * @param UserInterface|null $user
126a25f0a04SGreg Roach     *
127cbc1590aSGreg Roach     * @return bool
128a25f0a04SGreg Roach     */
129e5a6b4d4SGreg Roach    public static function isMember(Tree $tree, UserInterface $user = null): bool
130c1010edaSGreg Roach    {
131cb923727SGreg Roach        $user = $user ?? self::user();
132a25f0a04SGreg Roach
1337c4add84SGreg Roach        return self::isEditor($tree, $user) || $tree->getUserPreference($user, User::PREF_TREE_ROLE) === 'access';
134a25f0a04SGreg Roach    }
135a25f0a04SGreg Roach
136a25f0a04SGreg Roach    /**
1374b9ff166SGreg Roach     * What is the specified/current user's access level within a tree?
1384b9ff166SGreg Roach     *
1394b9ff166SGreg Roach     * @param Tree               $tree
140e5a6b4d4SGreg Roach     * @param UserInterface|null $user
1414b9ff166SGreg Roach     *
142cbc1590aSGreg Roach     * @return int
1434b9ff166SGreg Roach     */
144e364afe4SGreg Roach    public static function accessLevel(Tree $tree, UserInterface $user = null): int
145c1010edaSGreg Roach    {
146cb923727SGreg Roach        $user = $user ?? self::user();
1474b9ff166SGreg Roach
1484b9ff166SGreg Roach        if (self::isManager($tree, $user)) {
1494b9ff166SGreg Roach            return self::PRIV_NONE;
1504b9ff166SGreg Roach        }
151b2ce94c6SRico Sonntag
152b2ce94c6SRico Sonntag        if (self::isMember($tree, $user)) {
153b2ce94c6SRico Sonntag            return self::PRIV_USER;
154b2ce94c6SRico Sonntag        }
155b2ce94c6SRico Sonntag
156b2ce94c6SRico Sonntag        return self::PRIV_PRIVATE;
1574b9ff166SGreg Roach    }
1584b9ff166SGreg Roach
1594b9ff166SGreg Roach    /**
160a25f0a04SGreg Roach     * The ID of the authenticated user, from the current session.
161a25f0a04SGreg Roach     *
162c3ffc4cbSGreg Roach     * @return int|null
163a25f0a04SGreg Roach     */
164e364afe4SGreg Roach    public static function id(): ?int
165c1010edaSGreg Roach    {
1669683b471SGreg Roach        return Session::get('wt_user');
167a25f0a04SGreg Roach    }
168a25f0a04SGreg Roach
169a25f0a04SGreg Roach    /**
170a25f0a04SGreg Roach     * The authenticated user, from the current session.
171a25f0a04SGreg Roach     *
172e5a6b4d4SGreg Roach     * @return UserInterface
173a25f0a04SGreg Roach     */
174e5a6b4d4SGreg Roach    public static function user(): UserInterface
175c1010edaSGreg Roach    {
17615674e31SGreg Roach        return app(UserService::class)->find(self::id()) ?? new GuestUser();
177a25f0a04SGreg Roach    }
178a25f0a04SGreg Roach
179a25f0a04SGreg Roach    /**
180a25f0a04SGreg Roach     * Login directly as an explicit user - for masquerading.
181a25f0a04SGreg Roach     *
182e5a6b4d4SGreg Roach     * @param UserInterface $user
183cb923727SGreg Roach     *
184cb923727SGreg Roach     * @return void
185a25f0a04SGreg Roach     */
186e364afe4SGreg Roach    public static function login(UserInterface $user): void
187c1010edaSGreg Roach    {
188e988f922SGreg Roach        Session::regenerate(false);
189895230eeSGreg Roach        Session::put('wt_user', $user->id());
190a25f0a04SGreg Roach    }
191a25f0a04SGreg Roach
192a25f0a04SGreg Roach    /**
193a25f0a04SGreg Roach     * End the session for the current user.
194cb923727SGreg Roach     *
195cb923727SGreg Roach     * @return void
196a25f0a04SGreg Roach     */
197e364afe4SGreg Roach    public static function logout(): void
198c1010edaSGreg Roach    {
19931bc7874SGreg Roach        Session::regenerate(true);
200a25f0a04SGreg Roach    }
201e539f5c6SGreg Roach
202e539f5c6SGreg Roach    /**
2039867b2f0SGreg Roach     * @param ModuleInterface $module
2049867b2f0SGreg Roach     * @param string          $component
2059867b2f0SGreg Roach     * @param Tree            $tree
206e5a6b4d4SGreg Roach     * @param UserInterface   $user
2079867b2f0SGreg Roach     *
2089867b2f0SGreg Roach     * @return void
2099867b2f0SGreg Roach     */
210e364afe4SGreg Roach    public static function checkComponentAccess(ModuleInterface $module, string $component, Tree $tree, UserInterface $user): void
2119867b2f0SGreg Roach    {
212e364afe4SGreg Roach        if ($module->accessLevel($tree, $component) < self::accessLevel($tree, $user)) {
213d501c45dSGreg Roach            throw new HttpAccessDeniedException();
2149867b2f0SGreg Roach        }
2159867b2f0SGreg Roach    }
2169867b2f0SGreg Roach
2179867b2f0SGreg Roach    /**
218e539f5c6SGreg Roach     * @param Family|null $family
219ffc0a61fSGreg Roach     * @param bool        $edit
220e539f5c6SGreg Roach     *
221ddeb3354SGreg Roach     * @return Family
222e539f5c6SGreg Roach     * @throws FamilyNotFoundException
223e539f5c6SGreg Roach     * @throws FamilyAccessDeniedException
224e539f5c6SGreg Roach     */
2253c3fd0a5SGreg Roach    public static function checkFamilyAccess(?Family $family, bool $edit = false): Family
226e539f5c6SGreg Roach    {
227e539f5c6SGreg Roach        if ($family === null) {
228e539f5c6SGreg Roach            throw new FamilyNotFoundException();
229e539f5c6SGreg Roach        }
230e539f5c6SGreg Roach
2313c3fd0a5SGreg Roach        if ($edit && $family->canEdit()) {
232ddeb3354SGreg Roach            return $family;
233e539f5c6SGreg Roach        }
234e539f5c6SGreg Roach
2353c3fd0a5SGreg Roach        if ($family->canShow()) {
2363c3fd0a5SGreg Roach            return $family;
2373c3fd0a5SGreg Roach        }
2383c3fd0a5SGreg Roach
2393c3fd0a5SGreg Roach        throw new FamilyAccessDeniedException();
2403c3fd0a5SGreg Roach    }
2413c3fd0a5SGreg Roach
242e539f5c6SGreg Roach    /**
2431635452cSGreg Roach     * @param Header|null $header
2441635452cSGreg Roach     * @param bool        $edit
2451635452cSGreg Roach     *
2461635452cSGreg Roach     * @return Header
2471635452cSGreg Roach     * @throws RecordNotFoundException
2481635452cSGreg Roach     * @throws RecordAccessDeniedException
2491635452cSGreg Roach     */
2501635452cSGreg Roach    public static function checkHeaderAccess(?Header $header, bool $edit = false): Header
2511635452cSGreg Roach    {
2521635452cSGreg Roach        if ($header === null) {
2531635452cSGreg Roach            throw new RecordNotFoundException();
2541635452cSGreg Roach        }
2551635452cSGreg Roach
2561635452cSGreg Roach        if ($edit && $header->canEdit()) {
2571635452cSGreg Roach            return $header;
2581635452cSGreg Roach        }
2591635452cSGreg Roach
2601635452cSGreg Roach        if ($header->canShow()) {
2611635452cSGreg Roach            return $header;
2621635452cSGreg Roach        }
2631635452cSGreg Roach
2641635452cSGreg Roach        throw new RecordAccessDeniedException();
2651635452cSGreg Roach    }
2661635452cSGreg Roach
2671635452cSGreg Roach    /**
268e539f5c6SGreg Roach     * @param Individual|null $individual
269ffc0a61fSGreg Roach     * @param bool            $edit
2703c3fd0a5SGreg Roach     * @param bool            $chart      For some charts, we can show private records
271e539f5c6SGreg Roach     *
272ddeb3354SGreg Roach     * @return Individual
273e539f5c6SGreg Roach     * @throws IndividualNotFoundException
274e539f5c6SGreg Roach     * @throws IndividualAccessDeniedException
275e539f5c6SGreg Roach     */
2763c3fd0a5SGreg Roach    public static function checkIndividualAccess(?Individual $individual, bool $edit = false, $chart = false): Individual
277e539f5c6SGreg Roach    {
278e539f5c6SGreg Roach        if ($individual === null) {
279e539f5c6SGreg Roach            throw new IndividualNotFoundException();
280e539f5c6SGreg Roach        }
281e539f5c6SGreg Roach
2823c3fd0a5SGreg Roach        if ($edit && $individual->canEdit()) {
283ddeb3354SGreg Roach            return $individual;
284e539f5c6SGreg Roach        }
285e539f5c6SGreg Roach
2863c3fd0a5SGreg Roach        if ($chart && $individual->tree()->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
2873c3fd0a5SGreg Roach            return $individual;
2883c3fd0a5SGreg Roach        }
2893c3fd0a5SGreg Roach
2903c3fd0a5SGreg Roach        if ($individual->canShow()) {
2913c3fd0a5SGreg Roach            return $individual;
2923c3fd0a5SGreg Roach        }
2933c3fd0a5SGreg Roach
2943c3fd0a5SGreg Roach        throw new IndividualAccessDeniedException();
2953c3fd0a5SGreg Roach    }
2963c3fd0a5SGreg Roach
297e539f5c6SGreg Roach    /**
298e539f5c6SGreg Roach     * @param Media|null $media
299ffc0a61fSGreg Roach     * @param bool       $edit
300e539f5c6SGreg Roach     *
301ddeb3354SGreg Roach     * @return Media
302e539f5c6SGreg Roach     * @throws MediaNotFoundException
303e539f5c6SGreg Roach     * @throws MediaAccessDeniedException
304e539f5c6SGreg Roach     */
3053c3fd0a5SGreg Roach    public static function checkMediaAccess(?Media $media, bool $edit = false): Media
306e539f5c6SGreg Roach    {
307e539f5c6SGreg Roach        if ($media === null) {
308e539f5c6SGreg Roach            throw new MediaNotFoundException();
309e539f5c6SGreg Roach        }
310e539f5c6SGreg Roach
3113c3fd0a5SGreg Roach        if ($edit && $media->canEdit()) {
312ddeb3354SGreg Roach            return $media;
313e539f5c6SGreg Roach        }
314e539f5c6SGreg Roach
3153c3fd0a5SGreg Roach        if ($media->canShow()) {
3163c3fd0a5SGreg Roach            return $media;
3173c3fd0a5SGreg Roach        }
3183c3fd0a5SGreg Roach
3193c3fd0a5SGreg Roach        throw new MediaAccessDeniedException();
3203c3fd0a5SGreg Roach    }
3213c3fd0a5SGreg Roach
322e539f5c6SGreg Roach    /**
323e539f5c6SGreg Roach     * @param Note|null $note
324ffc0a61fSGreg Roach     * @param bool      $edit
325e539f5c6SGreg Roach     *
326ddeb3354SGreg Roach     * @return Note
327e539f5c6SGreg Roach     * @throws NoteNotFoundException
328e539f5c6SGreg Roach     * @throws NoteAccessDeniedException
329e539f5c6SGreg Roach     */
3303c3fd0a5SGreg Roach    public static function checkNoteAccess(?Note $note, bool $edit = false): Note
331e539f5c6SGreg Roach    {
332e539f5c6SGreg Roach        if ($note === null) {
333e539f5c6SGreg Roach            throw new NoteNotFoundException();
334e539f5c6SGreg Roach        }
335e539f5c6SGreg Roach
3363c3fd0a5SGreg Roach        if ($edit && $note->canEdit()) {
337ddeb3354SGreg Roach            return $note;
338e539f5c6SGreg Roach        }
339e539f5c6SGreg Roach
3403c3fd0a5SGreg Roach        if ($note->canShow()) {
3413c3fd0a5SGreg Roach            return $note;
3423c3fd0a5SGreg Roach        }
3433c3fd0a5SGreg Roach
3443c3fd0a5SGreg Roach        throw new NoteAccessDeniedException();
3453c3fd0a5SGreg Roach    }
3463c3fd0a5SGreg Roach
347e539f5c6SGreg Roach    /**
348e539f5c6SGreg Roach     * @param GedcomRecord|null $record
349ffc0a61fSGreg Roach     * @param bool              $edit
350e539f5c6SGreg Roach     *
351ddeb3354SGreg Roach     * @return GedcomRecord
352e539f5c6SGreg Roach     * @throws RecordNotFoundException
353e539f5c6SGreg Roach     * @throws RecordAccessDeniedException
354e539f5c6SGreg Roach     */
3553c3fd0a5SGreg Roach    public static function checkRecordAccess(?GedcomRecord $record, bool $edit = false): GedcomRecord
356e539f5c6SGreg Roach    {
357e539f5c6SGreg Roach        if ($record === null) {
358e539f5c6SGreg Roach            throw new RecordNotFoundException();
359e539f5c6SGreg Roach        }
360e539f5c6SGreg Roach
3613c3fd0a5SGreg Roach        if ($edit && $record->canEdit()) {
362ddeb3354SGreg Roach            return $record;
363e539f5c6SGreg Roach        }
364e539f5c6SGreg Roach
3653c3fd0a5SGreg Roach        if ($record->canShow()) {
3663c3fd0a5SGreg Roach            return $record;
3673c3fd0a5SGreg Roach        }
3683c3fd0a5SGreg Roach
3693c3fd0a5SGreg Roach        throw new RecordAccessDeniedException();
3703c3fd0a5SGreg Roach    }
3713c3fd0a5SGreg Roach
372e539f5c6SGreg Roach    /**
373e539f5c6SGreg Roach     * @param Repository|null $repository
374ffc0a61fSGreg Roach     * @param bool            $edit
375e539f5c6SGreg Roach     *
376ddeb3354SGreg Roach     * @return Repository
377e539f5c6SGreg Roach     * @throws RepositoryNotFoundException
378e539f5c6SGreg Roach     * @throws RepositoryAccessDeniedException
379e539f5c6SGreg Roach     */
3803c3fd0a5SGreg Roach    public static function checkRepositoryAccess(?Repository $repository, bool $edit = false): Repository
381e539f5c6SGreg Roach    {
382e539f5c6SGreg Roach        if ($repository === null) {
383e539f5c6SGreg Roach            throw new RepositoryNotFoundException();
384e539f5c6SGreg Roach        }
385e539f5c6SGreg Roach
3863c3fd0a5SGreg Roach        if ($edit && $repository->canEdit()) {
387ddeb3354SGreg Roach            return $repository;
388e539f5c6SGreg Roach        }
389e539f5c6SGreg Roach
3903c3fd0a5SGreg Roach        if ($repository->canShow()) {
3913c3fd0a5SGreg Roach            return $repository;
3923c3fd0a5SGreg Roach        }
3933c3fd0a5SGreg Roach
3943c3fd0a5SGreg Roach        throw new RepositoryAccessDeniedException();
3953c3fd0a5SGreg Roach    }
3963c3fd0a5SGreg Roach
397e539f5c6SGreg Roach    /**
398e539f5c6SGreg Roach     * @param Source|null $source
399ffc0a61fSGreg Roach     * @param bool        $edit
400e539f5c6SGreg Roach     *
401ddeb3354SGreg Roach     * @return Source
402e539f5c6SGreg Roach     * @throws SourceNotFoundException
403e539f5c6SGreg Roach     * @throws SourceAccessDeniedException
404e539f5c6SGreg Roach     */
4053c3fd0a5SGreg Roach    public static function checkSourceAccess(?Source $source, bool $edit = false): Source
406e539f5c6SGreg Roach    {
407e539f5c6SGreg Roach        if ($source === null) {
408e539f5c6SGreg Roach            throw new SourceNotFoundException();
409e539f5c6SGreg Roach        }
410e539f5c6SGreg Roach
4113c3fd0a5SGreg Roach        if ($edit && $source->canEdit()) {
412ddeb3354SGreg Roach            return $source;
413e539f5c6SGreg Roach        }
414ffc0a61fSGreg Roach
4153c3fd0a5SGreg Roach        if ($source->canShow()) {
4163c3fd0a5SGreg Roach            return $source;
4173c3fd0a5SGreg Roach        }
4183c3fd0a5SGreg Roach
4193c3fd0a5SGreg Roach        throw new SourceAccessDeniedException();
4203c3fd0a5SGreg Roach    }
4213c3fd0a5SGreg Roach
422ffc0a61fSGreg Roach    /*
423ffc0a61fSGreg Roach     * @param Submitter|null $submitter
424ffc0a61fSGreg Roach     * @param bool           $edit
425ffc0a61fSGreg Roach     *
426ffc0a61fSGreg Roach     * @return Submitter
427ffc0a61fSGreg Roach     * @throws RecordNotFoundException
428ffc0a61fSGreg Roach     * @throws RecordAccessDeniedException
429ffc0a61fSGreg Roach     */
4303c3fd0a5SGreg Roach    public static function checkSubmitterAccess(?Submitter $submitter, bool $edit = false): Submitter
431ffc0a61fSGreg Roach    {
432ffc0a61fSGreg Roach        if ($submitter === null) {
433ffc0a61fSGreg Roach            throw new RecordNotFoundException();
434ffc0a61fSGreg Roach        }
435ffc0a61fSGreg Roach
4363c3fd0a5SGreg Roach        if ($edit && $submitter->canEdit()) {
437ffc0a61fSGreg Roach            return $submitter;
438ffc0a61fSGreg Roach        }
4393c3fd0a5SGreg Roach
4403c3fd0a5SGreg Roach        if ($submitter->canShow()) {
4413c3fd0a5SGreg Roach            return $submitter;
4423c3fd0a5SGreg Roach        }
4433c3fd0a5SGreg Roach
4443c3fd0a5SGreg Roach        throw new RecordAccessDeniedException();
4453c3fd0a5SGreg Roach    }
4461635452cSGreg Roach
4471635452cSGreg Roach    /*
4481635452cSGreg Roach     * @param Submission|null $submission
4491635452cSGreg Roach     * @param bool            $edit
4501635452cSGreg Roach     *
4511635452cSGreg Roach     * @return Submission
4521635452cSGreg Roach     * @throws RecordNotFoundException
4531635452cSGreg Roach     * @throws RecordAccessDeniedException
4541635452cSGreg Roach     */
4551635452cSGreg Roach    public static function checkSubmissionAccess(?Submission $submission, bool $edit = false): Submission
4561635452cSGreg Roach    {
4571635452cSGreg Roach        if ($submission === null) {
4581635452cSGreg Roach            throw new RecordNotFoundException();
4591635452cSGreg Roach        }
4601635452cSGreg Roach
4611635452cSGreg Roach        if ($edit && $submission->canEdit()) {
4621635452cSGreg Roach            return $submission;
4631635452cSGreg Roach        }
4641635452cSGreg Roach
4651635452cSGreg Roach        if ($submission->canShow()) {
4661635452cSGreg Roach            return $submission;
4671635452cSGreg Roach        }
4681635452cSGreg Roach
4691635452cSGreg Roach        throw new RecordAccessDeniedException();
4701635452cSGreg Roach    }
471870365fbSGreg Roach
472870365fbSGreg Roach    /**
473870365fbSGreg Roach     * @return array<int,string>
474870365fbSGreg Roach     */
475870365fbSGreg Roach    public static function accessLevelNames(): array
476870365fbSGreg Roach    {
477870365fbSGreg Roach        return [
478870365fbSGreg Roach            Auth::PRIV_PRIVATE => I18N::translate('Show to visitors'),
479870365fbSGreg Roach            Auth::PRIV_USER    => I18N::translate('Show to members'),
480870365fbSGreg Roach            Auth::PRIV_NONE    => I18N::translate('Show to managers'),
481870365fbSGreg Roach            Auth::PRIV_HIDE    => I18N::translate('Hide from everyone'),
482870365fbSGreg Roach        ];
483870365fbSGreg Roach    }
484*138139c2SGreg Roach
485*138139c2SGreg Roach    /**
486*138139c2SGreg Roach     * @return array<string,string>
487*138139c2SGreg Roach     */
488*138139c2SGreg Roach    public static function privacyRuleNames(): array
489*138139c2SGreg Roach    {
490*138139c2SGreg Roach        return [
491*138139c2SGreg Roach            'none'         => I18N::translate('Show to visitors'),
492*138139c2SGreg Roach            'privacy'      => I18N::translate('Show to members'),
493*138139c2SGreg Roach            'confidential' => I18N::translate('Show to managers'),
494*138139c2SGreg Roach            'hidden'       => I18N::translate('Hide from everyone'),
495*138139c2SGreg Roach        ];
496*138139c2SGreg Roach    }
497a25f0a04SGreg Roach}
498