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 { 166*9683b471SGreg 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 /** 243e539f5c6SGreg Roach * @param Individual|null $individual 244ffc0a61fSGreg Roach * @param bool $edit 2453c3fd0a5SGreg Roach * @param bool $chart For some charts, we can show private records 246e539f5c6SGreg Roach * 247ddeb3354SGreg Roach * @return Individual 248e539f5c6SGreg Roach * @throws IndividualNotFoundException 249e539f5c6SGreg Roach * @throws IndividualAccessDeniedException 250e539f5c6SGreg Roach */ 2513c3fd0a5SGreg Roach public static function checkIndividualAccess(?Individual $individual, bool $edit = false, $chart = false): Individual 252e539f5c6SGreg Roach { 253e539f5c6SGreg Roach if ($individual === null) { 254e539f5c6SGreg Roach throw new IndividualNotFoundException(); 255e539f5c6SGreg Roach } 256e539f5c6SGreg Roach 2573c3fd0a5SGreg Roach if ($edit && $individual->canEdit()) { 258ddeb3354SGreg Roach return $individual; 259e539f5c6SGreg Roach } 260e539f5c6SGreg Roach 2613c3fd0a5SGreg Roach if ($chart && $individual->tree()->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { 2623c3fd0a5SGreg Roach return $individual; 2633c3fd0a5SGreg Roach } 2643c3fd0a5SGreg Roach 2653c3fd0a5SGreg Roach if ($individual->canShow()) { 2663c3fd0a5SGreg Roach return $individual; 2673c3fd0a5SGreg Roach } 2683c3fd0a5SGreg Roach 2693c3fd0a5SGreg Roach throw new IndividualAccessDeniedException(); 2703c3fd0a5SGreg Roach } 2713c3fd0a5SGreg Roach 272e539f5c6SGreg Roach /** 273e539f5c6SGreg Roach * @param Media|null $media 274ffc0a61fSGreg Roach * @param bool $edit 275e539f5c6SGreg Roach * 276ddeb3354SGreg Roach * @return Media 277e539f5c6SGreg Roach * @throws MediaNotFoundException 278e539f5c6SGreg Roach * @throws MediaAccessDeniedException 279e539f5c6SGreg Roach */ 2803c3fd0a5SGreg Roach public static function checkMediaAccess(?Media $media, bool $edit = false): Media 281e539f5c6SGreg Roach { 282e539f5c6SGreg Roach if ($media === null) { 283e539f5c6SGreg Roach throw new MediaNotFoundException(); 284e539f5c6SGreg Roach } 285e539f5c6SGreg Roach 2863c3fd0a5SGreg Roach if ($edit && $media->canEdit()) { 287ddeb3354SGreg Roach return $media; 288e539f5c6SGreg Roach } 289e539f5c6SGreg Roach 2903c3fd0a5SGreg Roach if ($media->canShow()) { 2913c3fd0a5SGreg Roach return $media; 2923c3fd0a5SGreg Roach } 2933c3fd0a5SGreg Roach 2943c3fd0a5SGreg Roach throw new MediaAccessDeniedException(); 2953c3fd0a5SGreg Roach } 2963c3fd0a5SGreg Roach 297e539f5c6SGreg Roach /** 298e539f5c6SGreg Roach * @param Note|null $note 299ffc0a61fSGreg Roach * @param bool $edit 300e539f5c6SGreg Roach * 301ddeb3354SGreg Roach * @return Note 302e539f5c6SGreg Roach * @throws NoteNotFoundException 303e539f5c6SGreg Roach * @throws NoteAccessDeniedException 304e539f5c6SGreg Roach */ 3053c3fd0a5SGreg Roach public static function checkNoteAccess(?Note $note, bool $edit = false): Note 306e539f5c6SGreg Roach { 307e539f5c6SGreg Roach if ($note === null) { 308e539f5c6SGreg Roach throw new NoteNotFoundException(); 309e539f5c6SGreg Roach } 310e539f5c6SGreg Roach 3113c3fd0a5SGreg Roach if ($edit && $note->canEdit()) { 312ddeb3354SGreg Roach return $note; 313e539f5c6SGreg Roach } 314e539f5c6SGreg Roach 3153c3fd0a5SGreg Roach if ($note->canShow()) { 3163c3fd0a5SGreg Roach return $note; 3173c3fd0a5SGreg Roach } 3183c3fd0a5SGreg Roach 3193c3fd0a5SGreg Roach throw new NoteAccessDeniedException(); 3203c3fd0a5SGreg Roach } 3213c3fd0a5SGreg Roach 322e539f5c6SGreg Roach /** 323e539f5c6SGreg Roach * @param GedcomRecord|null $record 324ffc0a61fSGreg Roach * @param bool $edit 325e539f5c6SGreg Roach * 326ddeb3354SGreg Roach * @return GedcomRecord 327e539f5c6SGreg Roach * @throws RecordNotFoundException 328e539f5c6SGreg Roach * @throws RecordAccessDeniedException 329e539f5c6SGreg Roach */ 3303c3fd0a5SGreg Roach public static function checkRecordAccess(?GedcomRecord $record, bool $edit = false): GedcomRecord 331e539f5c6SGreg Roach { 332e539f5c6SGreg Roach if ($record === null) { 333e539f5c6SGreg Roach throw new RecordNotFoundException(); 334e539f5c6SGreg Roach } 335e539f5c6SGreg Roach 3363c3fd0a5SGreg Roach if ($edit && $record->canEdit()) { 337ddeb3354SGreg Roach return $record; 338e539f5c6SGreg Roach } 339e539f5c6SGreg Roach 3403c3fd0a5SGreg Roach if ($record->canShow()) { 3413c3fd0a5SGreg Roach return $record; 3423c3fd0a5SGreg Roach } 3433c3fd0a5SGreg Roach 3443c3fd0a5SGreg Roach throw new RecordAccessDeniedException(); 3453c3fd0a5SGreg Roach } 3463c3fd0a5SGreg Roach 347e539f5c6SGreg Roach /** 348e539f5c6SGreg Roach * @param Repository|null $repository 349ffc0a61fSGreg Roach * @param bool $edit 350e539f5c6SGreg Roach * 351ddeb3354SGreg Roach * @return Repository 352e539f5c6SGreg Roach * @throws RepositoryNotFoundException 353e539f5c6SGreg Roach * @throws RepositoryAccessDeniedException 354e539f5c6SGreg Roach */ 3553c3fd0a5SGreg Roach public static function checkRepositoryAccess(?Repository $repository, bool $edit = false): Repository 356e539f5c6SGreg Roach { 357e539f5c6SGreg Roach if ($repository === null) { 358e539f5c6SGreg Roach throw new RepositoryNotFoundException(); 359e539f5c6SGreg Roach } 360e539f5c6SGreg Roach 3613c3fd0a5SGreg Roach if ($edit && $repository->canEdit()) { 362ddeb3354SGreg Roach return $repository; 363e539f5c6SGreg Roach } 364e539f5c6SGreg Roach 3653c3fd0a5SGreg Roach if ($repository->canShow()) { 3663c3fd0a5SGreg Roach return $repository; 3673c3fd0a5SGreg Roach } 3683c3fd0a5SGreg Roach 3693c3fd0a5SGreg Roach throw new RepositoryAccessDeniedException(); 3703c3fd0a5SGreg Roach } 3713c3fd0a5SGreg Roach 372e539f5c6SGreg Roach /** 373e539f5c6SGreg Roach * @param Source|null $source 374ffc0a61fSGreg Roach * @param bool $edit 375e539f5c6SGreg Roach * 376ddeb3354SGreg Roach * @return Source 377e539f5c6SGreg Roach * @throws SourceNotFoundException 378e539f5c6SGreg Roach * @throws SourceAccessDeniedException 379e539f5c6SGreg Roach */ 3803c3fd0a5SGreg Roach public static function checkSourceAccess(?Source $source, bool $edit = false): Source 381e539f5c6SGreg Roach { 382e539f5c6SGreg Roach if ($source === null) { 383e539f5c6SGreg Roach throw new SourceNotFoundException(); 384e539f5c6SGreg Roach } 385e539f5c6SGreg Roach 3863c3fd0a5SGreg Roach if ($edit && $source->canEdit()) { 387ddeb3354SGreg Roach return $source; 388e539f5c6SGreg Roach } 389ffc0a61fSGreg Roach 3903c3fd0a5SGreg Roach if ($source->canShow()) { 3913c3fd0a5SGreg Roach return $source; 3923c3fd0a5SGreg Roach } 3933c3fd0a5SGreg Roach 3943c3fd0a5SGreg Roach throw new SourceAccessDeniedException(); 3953c3fd0a5SGreg Roach } 3963c3fd0a5SGreg Roach 397ffc0a61fSGreg Roach /* 398ffc0a61fSGreg Roach * @param Submitter|null $submitter 399ffc0a61fSGreg Roach * @param bool $edit 400ffc0a61fSGreg Roach * 401ffc0a61fSGreg Roach * @return Submitter 402ffc0a61fSGreg Roach * @throws RecordNotFoundException 403ffc0a61fSGreg Roach * @throws RecordAccessDeniedException 404ffc0a61fSGreg Roach */ 4053c3fd0a5SGreg Roach public static function checkSubmitterAccess(?Submitter $submitter, bool $edit = false): Submitter 406ffc0a61fSGreg Roach { 407ffc0a61fSGreg Roach if ($submitter === null) { 408ffc0a61fSGreg Roach throw new RecordNotFoundException(); 409ffc0a61fSGreg Roach } 410ffc0a61fSGreg Roach 4113c3fd0a5SGreg Roach if ($edit && $submitter->canEdit()) { 412ffc0a61fSGreg Roach return $submitter; 413ffc0a61fSGreg Roach } 4143c3fd0a5SGreg Roach 4153c3fd0a5SGreg Roach if ($submitter->canShow()) { 4163c3fd0a5SGreg Roach return $submitter; 4173c3fd0a5SGreg Roach } 4183c3fd0a5SGreg Roach 4193c3fd0a5SGreg Roach throw new RecordAccessDeniedException(); 4203c3fd0a5SGreg Roach } 421a25f0a04SGreg Roach} 422