xref: /webtrees/app/Auth.php (revision 1792ff1cf1956b41f3e3c853cfb279a803a71ed2)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Fisharebest\Webtrees\Contracts\UserInterface;
23use Fisharebest\Webtrees\Exceptions\FamilyAccessDeniedException;
24use Fisharebest\Webtrees\Exceptions\FamilyNotFoundException;
25use Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
26use Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException;
27use Fisharebest\Webtrees\Exceptions\IndividualNotFoundException;
28use Fisharebest\Webtrees\Exceptions\MediaAccessDeniedException;
29use Fisharebest\Webtrees\Exceptions\MediaNotFoundException;
30use Fisharebest\Webtrees\Exceptions\NoteAccessDeniedException;
31use Fisharebest\Webtrees\Exceptions\NoteNotFoundException;
32use Fisharebest\Webtrees\Exceptions\RecordAccessDeniedException;
33use Fisharebest\Webtrees\Exceptions\RecordNotFoundException;
34use Fisharebest\Webtrees\Exceptions\RepositoryAccessDeniedException;
35use Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException;
36use Fisharebest\Webtrees\Exceptions\SourceAccessDeniedException;
37use Fisharebest\Webtrees\Exceptions\SourceNotFoundException;
38use Fisharebest\Webtrees\Module\ModuleInterface;
39use Fisharebest\Webtrees\Services\UserService;
40
41/**
42 * Authentication.
43 */
44class Auth
45{
46    // Privacy constants
47    public const PRIV_PRIVATE = 2; // Allows visitors to view the item
48    public const PRIV_USER    = 1; // Allows members to access the item
49    public const PRIV_NONE    = 0; // Allows managers to access the item
50    public const PRIV_HIDE    = -1; // Hide the item to all users
51
52    /**
53     * Are we currently logged in?
54     *
55     * @return bool
56     */
57    public static function check(): bool
58    {
59        return self::id() !== null;
60    }
61
62    /**
63     * Is the specified/current user an administrator?
64     *
65     * @param UserInterface|null $user
66     *
67     * @return bool
68     */
69    public static function isAdmin(UserInterface $user = null): bool
70    {
71        $user = $user ?? self::user();
72
73        return $user->getPreference(User::PREF_IS_ADMINISTRATOR) === '1';
74    }
75
76    /**
77     * Is the specified/current user a manager of a tree?
78     *
79     * @param Tree               $tree
80     * @param UserInterface|null $user
81     *
82     * @return bool
83     */
84    public static function isManager(Tree $tree, UserInterface $user = null): bool
85    {
86        $user = $user ?? self::user();
87
88        return self::isAdmin($user) || $tree->getUserPreference($user, User::PREF_TREE_ROLE) === User::ROLE_MANAGER;
89    }
90
91    /**
92     * Is the specified/current user a moderator of a tree?
93     *
94     * @param Tree               $tree
95     * @param UserInterface|null $user
96     *
97     * @return bool
98     */
99    public static function isModerator(Tree $tree, UserInterface $user = null): bool
100    {
101        $user = $user ?? self::user();
102
103        return self::isManager($tree, $user) || $tree->getUserPreference($user, User::PREF_TREE_ROLE) === User::ROLE_MODERATOR;
104    }
105
106    /**
107     * Is the specified/current user an editor of a tree?
108     *
109     * @param Tree               $tree
110     * @param UserInterface|null $user
111     *
112     * @return bool
113     */
114    public static function isEditor(Tree $tree, UserInterface $user = null): bool
115    {
116        $user = $user ?? self::user();
117
118        return self::isModerator($tree, $user) || $tree->getUserPreference($user, User::PREF_TREE_ROLE) === 'edit';
119    }
120
121    /**
122     * Is the specified/current user a member of a tree?
123     *
124     * @param Tree               $tree
125     * @param UserInterface|null $user
126     *
127     * @return bool
128     */
129    public static function isMember(Tree $tree, UserInterface $user = null): bool
130    {
131        $user = $user ?? self::user();
132
133        return self::isEditor($tree, $user) || $tree->getUserPreference($user, User::PREF_TREE_ROLE) === 'access';
134    }
135
136    /**
137     * What is the specified/current user's access level within a tree?
138     *
139     * @param Tree               $tree
140     * @param UserInterface|null $user
141     *
142     * @return int
143     */
144    public static function accessLevel(Tree $tree, UserInterface $user = null): int
145    {
146        $user = $user ?? self::user();
147
148        if (self::isManager($tree, $user)) {
149            return self::PRIV_NONE;
150        }
151
152        if (self::isMember($tree, $user)) {
153            return self::PRIV_USER;
154        }
155
156        return self::PRIV_PRIVATE;
157    }
158
159    /**
160     * The ID of the authenticated user, from the current session.
161     *
162     * @return int|null
163     */
164    public static function id(): ?int
165    {
166        return Session::get('wt_user');
167    }
168
169    /**
170     * The authenticated user, from the current session.
171     *
172     * @return UserInterface
173     */
174    public static function user(): UserInterface
175    {
176        return app(UserService::class)->find(self::id()) ?? new GuestUser();
177    }
178
179    /**
180     * Login directly as an explicit user - for masquerading.
181     *
182     * @param UserInterface $user
183     *
184     * @return void
185     */
186    public static function login(UserInterface $user): void
187    {
188        Session::regenerate(false);
189        Session::put('wt_user', $user->id());
190    }
191
192    /**
193     * End the session for the current user.
194     *
195     * @return void
196     */
197    public static function logout(): void
198    {
199        Session::regenerate(true);
200    }
201
202    /**
203     * @param ModuleInterface $module
204     * @param string          $component
205     * @param Tree            $tree
206     * @param UserInterface   $user
207     *
208     * @return void
209     */
210    public static function checkComponentAccess(ModuleInterface $module, string $component, Tree $tree, UserInterface $user): void
211    {
212        if ($module->accessLevel($tree, $component) < self::accessLevel($tree, $user)) {
213            throw new HttpAccessDeniedException();
214        }
215    }
216
217    /**
218     * @param Family|null $family
219     * @param bool        $edit
220     *
221     * @return Family
222     * @throws FamilyNotFoundException
223     * @throws FamilyAccessDeniedException
224     */
225    public static function checkFamilyAccess(?Family $family, bool $edit = false): Family
226    {
227        if ($family === null) {
228            throw new FamilyNotFoundException();
229        }
230
231        if ($edit && $family->canEdit()) {
232            return $family;
233        }
234
235        if ($family->canShow()) {
236            return $family;
237        }
238
239        throw new FamilyAccessDeniedException();
240    }
241
242    /**
243     * @param Individual|null $individual
244     * @param bool            $edit
245     * @param bool            $chart      For some charts, we can show private records
246     *
247     * @return Individual
248     * @throws IndividualNotFoundException
249     * @throws IndividualAccessDeniedException
250     */
251    public static function checkIndividualAccess(?Individual $individual, bool $edit = false, $chart = false): Individual
252    {
253        if ($individual === null) {
254            throw new IndividualNotFoundException();
255        }
256
257        if ($edit && $individual->canEdit()) {
258            return $individual;
259        }
260
261        if ($chart && $individual->tree()->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') {
262            return $individual;
263        }
264
265        if ($individual->canShow()) {
266            return $individual;
267        }
268
269        throw new IndividualAccessDeniedException();
270    }
271
272    /**
273     * @param Media|null $media
274     * @param bool       $edit
275     *
276     * @return Media
277     * @throws MediaNotFoundException
278     * @throws MediaAccessDeniedException
279     */
280    public static function checkMediaAccess(?Media $media, bool $edit = false): Media
281    {
282        if ($media === null) {
283            throw new MediaNotFoundException();
284        }
285
286        if ($edit && $media->canEdit()) {
287            return $media;
288        }
289
290        if ($media->canShow()) {
291            return $media;
292        }
293
294        throw new MediaAccessDeniedException();
295    }
296
297    /**
298     * @param Note|null $note
299     * @param bool      $edit
300     *
301     * @return Note
302     * @throws NoteNotFoundException
303     * @throws NoteAccessDeniedException
304     */
305    public static function checkNoteAccess(?Note $note, bool $edit = false): Note
306    {
307        if ($note === null) {
308            throw new NoteNotFoundException();
309        }
310
311        if ($edit && $note->canEdit()) {
312            return $note;
313        }
314
315        if ($note->canShow()) {
316            return $note;
317        }
318
319        throw new NoteAccessDeniedException();
320    }
321
322    /**
323     * @param GedcomRecord|null $record
324     * @param bool              $edit
325     *
326     * @return GedcomRecord
327     * @throws RecordNotFoundException
328     * @throws RecordAccessDeniedException
329     */
330    public static function checkRecordAccess(?GedcomRecord $record, bool $edit = false): GedcomRecord
331    {
332        if ($record === null) {
333            throw new RecordNotFoundException();
334        }
335
336        if ($edit && $record->canEdit()) {
337            return $record;
338        }
339
340        if ($record->canShow()) {
341            return $record;
342        }
343
344        throw new RecordAccessDeniedException();
345    }
346
347    /**
348     * @param Repository|null $repository
349     * @param bool            $edit
350     *
351     * @return Repository
352     * @throws RepositoryNotFoundException
353     * @throws RepositoryAccessDeniedException
354     */
355    public static function checkRepositoryAccess(?Repository $repository, bool $edit = false): Repository
356    {
357        if ($repository === null) {
358            throw new RepositoryNotFoundException();
359        }
360
361        if ($edit && $repository->canEdit()) {
362            return $repository;
363        }
364
365        if ($repository->canShow()) {
366            return $repository;
367        }
368
369        throw new RepositoryAccessDeniedException();
370    }
371
372    /**
373     * @param Source|null $source
374     * @param bool        $edit
375     *
376     * @return Source
377     * @throws SourceNotFoundException
378     * @throws SourceAccessDeniedException
379     */
380    public static function checkSourceAccess(?Source $source, bool $edit = false): Source
381    {
382        if ($source === null) {
383            throw new SourceNotFoundException();
384        }
385
386        if ($edit && $source->canEdit()) {
387            return $source;
388        }
389
390        if ($source->canShow()) {
391            return $source;
392        }
393
394        throw new SourceAccessDeniedException();
395    }
396
397    /*
398     * @param Submitter|null $submitter
399     * @param bool           $edit
400     *
401     * @return Submitter
402     * @throws RecordNotFoundException
403     * @throws RecordAccessDeniedException
404     */
405    public static function checkSubmitterAccess(?Submitter $submitter, bool $edit = false): Submitter
406    {
407        if ($submitter === null) {
408            throw new RecordNotFoundException();
409        }
410
411        if ($edit && $submitter->canEdit()) {
412            return $submitter;
413        }
414
415        if ($submitter->canShow()) {
416            return $submitter;
417        }
418
419        throw new RecordAccessDeniedException();
420    }
421}
422