1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees; 19a25f0a04SGreg Roach 20e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 21e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyAccessDeniedException; 22e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException; 23e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualAccessDeniedException; 24e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 25e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaAccessDeniedException; 26e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException; 27e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteAccessDeniedException; 28e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException; 29e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\RecordAccessDeniedException; 30e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\RecordNotFoundException; 31e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryAccessDeniedException; 32e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException; 33e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceAccessDeniedException; 34e539f5c6SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException; 359867b2f0SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface; 36e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 379867b2f0SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 3879529c87SGreg Roach 39a25f0a04SGreg Roach/** 4076692c8bSGreg Roach * Authentication. 41a25f0a04SGreg Roach */ 42c1010edaSGreg Roachclass Auth 43c1010edaSGreg Roach{ 444b9ff166SGreg Roach // Privacy constants 4516d6367aSGreg Roach public const PRIV_PRIVATE = 2; // Allows visitors to view the item 4616d6367aSGreg Roach public const PRIV_USER = 1; // Allows members to access the item 4716d6367aSGreg Roach public const PRIV_NONE = 0; // Allows managers to access the item 4816d6367aSGreg Roach public const PRIV_HIDE = -1; // Hide the item to all users 494b9ff166SGreg Roach 50a25f0a04SGreg Roach /** 51a25f0a04SGreg Roach * Are we currently logged in? 52a25f0a04SGreg Roach * 53cbc1590aSGreg Roach * @return bool 54a25f0a04SGreg Roach */ 558f53f488SRico Sonntag public static function check(): bool 56c1010edaSGreg Roach { 574b9ff166SGreg Roach return self::id() !== null; 58a25f0a04SGreg Roach } 59a25f0a04SGreg Roach 60a25f0a04SGreg Roach /** 61a25f0a04SGreg Roach * Is the specified/current user an administrator? 62a25f0a04SGreg Roach * 63e5a6b4d4SGreg Roach * @param UserInterface|null $user 64a25f0a04SGreg Roach * 65cbc1590aSGreg Roach * @return bool 66a25f0a04SGreg Roach */ 67e5a6b4d4SGreg Roach public static function isAdmin(UserInterface $user = null): bool 68c1010edaSGreg Roach { 69cb923727SGreg Roach $user = $user ?? self::user(); 70a25f0a04SGreg Roach 71cb923727SGreg Roach return $user->getPreference('canadmin') === '1'; 72a25f0a04SGreg Roach } 73a25f0a04SGreg Roach 74a25f0a04SGreg Roach /** 754b9ff166SGreg Roach * Is the specified/current user a manager of a tree? 76a25f0a04SGreg Roach * 7784caa210SGreg Roach * @param Tree $tree 78e5a6b4d4SGreg Roach * @param UserInterface|null $user 79a25f0a04SGreg Roach * 80cbc1590aSGreg Roach * @return bool 81a25f0a04SGreg Roach */ 82e5a6b4d4SGreg Roach public static function isManager(Tree $tree, UserInterface $user = null): bool 83c1010edaSGreg Roach { 84cb923727SGreg Roach $user = $user ?? self::user(); 85a25f0a04SGreg Roach 86cb923727SGreg Roach return self::isAdmin($user) || $tree->getUserPreference($user, 'canedit') === 'admin'; 87a25f0a04SGreg Roach } 88a25f0a04SGreg Roach 89a25f0a04SGreg Roach /** 904b9ff166SGreg Roach * Is the specified/current user a moderator of a tree? 91a25f0a04SGreg Roach * 9284caa210SGreg Roach * @param Tree $tree 93e5a6b4d4SGreg Roach * @param UserInterface|null $user 94a25f0a04SGreg Roach * 95cbc1590aSGreg Roach * @return bool 96a25f0a04SGreg Roach */ 97e5a6b4d4SGreg Roach public static function isModerator(Tree $tree, UserInterface $user = null): bool 98c1010edaSGreg Roach { 99cb923727SGreg Roach $user = $user ?? self::user(); 100a25f0a04SGreg Roach 101cb923727SGreg Roach return self::isManager($tree, $user) || $tree->getUserPreference($user, 'canedit') === 'accept'; 102a25f0a04SGreg Roach } 103a25f0a04SGreg Roach 104a25f0a04SGreg Roach /** 1054b9ff166SGreg Roach * Is the specified/current user an editor of a tree? 106a25f0a04SGreg Roach * 10784caa210SGreg Roach * @param Tree $tree 108e5a6b4d4SGreg Roach * @param UserInterface|null $user 109a25f0a04SGreg Roach * 110cbc1590aSGreg Roach * @return bool 111a25f0a04SGreg Roach */ 112e5a6b4d4SGreg Roach public static function isEditor(Tree $tree, UserInterface $user = null): bool 113c1010edaSGreg Roach { 114cb923727SGreg Roach $user = $user ?? self::user(); 115a25f0a04SGreg Roach 116cb923727SGreg Roach return self::isModerator($tree, $user) || $tree->getUserPreference($user, 'canedit') === 'edit'; 117a25f0a04SGreg Roach } 118a25f0a04SGreg Roach 119a25f0a04SGreg Roach /** 1204b9ff166SGreg Roach * Is the specified/current user a member of a tree? 121a25f0a04SGreg Roach * 12284caa210SGreg Roach * @param Tree $tree 123e5a6b4d4SGreg Roach * @param UserInterface|null $user 124a25f0a04SGreg Roach * 125cbc1590aSGreg Roach * @return bool 126a25f0a04SGreg Roach */ 127e5a6b4d4SGreg Roach public static function isMember(Tree $tree, UserInterface $user = null): bool 128c1010edaSGreg Roach { 129cb923727SGreg Roach $user = $user ?? self::user(); 130a25f0a04SGreg Roach 131cb923727SGreg Roach return self::isEditor($tree, $user) || $tree->getUserPreference($user, 'canedit') === 'access'; 132a25f0a04SGreg Roach } 133a25f0a04SGreg Roach 134a25f0a04SGreg Roach /** 1354b9ff166SGreg Roach * What is the specified/current user's access level within a tree? 1364b9ff166SGreg Roach * 1374b9ff166SGreg Roach * @param Tree $tree 138e5a6b4d4SGreg Roach * @param UserInterface|null $user 1394b9ff166SGreg Roach * 140cbc1590aSGreg Roach * @return int 1414b9ff166SGreg Roach */ 142*e364afe4SGreg Roach public static function accessLevel(Tree $tree, UserInterface $user = null): int 143c1010edaSGreg Roach { 144cb923727SGreg Roach $user = $user ?? self::user(); 1454b9ff166SGreg Roach 1464b9ff166SGreg Roach if (self::isManager($tree, $user)) { 1474b9ff166SGreg Roach return self::PRIV_NONE; 1484b9ff166SGreg Roach } 149b2ce94c6SRico Sonntag 150b2ce94c6SRico Sonntag if (self::isMember($tree, $user)) { 151b2ce94c6SRico Sonntag return self::PRIV_USER; 152b2ce94c6SRico Sonntag } 153b2ce94c6SRico Sonntag 154b2ce94c6SRico Sonntag return self::PRIV_PRIVATE; 1554b9ff166SGreg Roach } 1564b9ff166SGreg Roach 1574b9ff166SGreg Roach /** 158a25f0a04SGreg Roach * The ID of the authenticated user, from the current session. 159a25f0a04SGreg Roach * 160c3ffc4cbSGreg Roach * @return int|null 161a25f0a04SGreg Roach */ 162*e364afe4SGreg Roach public static function id(): ?int 163c1010edaSGreg Roach { 16431bc7874SGreg Roach return Session::get('wt_user'); 165a25f0a04SGreg Roach } 166a25f0a04SGreg Roach 167a25f0a04SGreg Roach /** 168a25f0a04SGreg Roach * The authenticated user, from the current session. 169a25f0a04SGreg Roach * 170e5a6b4d4SGreg Roach * @return UserInterface 171a25f0a04SGreg Roach */ 172e5a6b4d4SGreg Roach public static function user(): UserInterface 173c1010edaSGreg Roach { 174e5a6b4d4SGreg Roach return (new UserService())->find(self::id()) ?? new GuestUser(); 175a25f0a04SGreg Roach } 176a25f0a04SGreg Roach 177a25f0a04SGreg Roach /** 178a25f0a04SGreg Roach * Login directly as an explicit user - for masquerading. 179a25f0a04SGreg Roach * 180e5a6b4d4SGreg Roach * @param UserInterface $user 181cb923727SGreg Roach * 182cb923727SGreg Roach * @return void 183a25f0a04SGreg Roach */ 184*e364afe4SGreg Roach public static function login(UserInterface $user): void 185c1010edaSGreg Roach { 186e988f922SGreg Roach Session::regenerate(false); 187895230eeSGreg Roach Session::put('wt_user', $user->id()); 188a25f0a04SGreg Roach } 189a25f0a04SGreg Roach 190a25f0a04SGreg Roach /** 191a25f0a04SGreg Roach * End the session for the current user. 192cb923727SGreg Roach * 193cb923727SGreg Roach * @return void 194a25f0a04SGreg Roach */ 195*e364afe4SGreg Roach public static function logout(): void 196c1010edaSGreg Roach { 19731bc7874SGreg Roach Session::regenerate(true); 198a25f0a04SGreg Roach } 199e539f5c6SGreg Roach 200e539f5c6SGreg Roach /** 2019867b2f0SGreg Roach * @param ModuleInterface $module 2029867b2f0SGreg Roach * @param string $component 2039867b2f0SGreg Roach * @param Tree $tree 204e5a6b4d4SGreg Roach * @param UserInterface $user 2059867b2f0SGreg Roach * 2069867b2f0SGreg Roach * @return void 2079867b2f0SGreg Roach */ 208*e364afe4SGreg Roach public static function checkComponentAccess(ModuleInterface $module, string $component, Tree $tree, UserInterface $user): void 2099867b2f0SGreg Roach { 210*e364afe4SGreg Roach if ($module->accessLevel($tree, $component) < self::accessLevel($tree, $user)) { 2119867b2f0SGreg Roach throw new AccessDeniedHttpException(''); 2129867b2f0SGreg Roach } 2139867b2f0SGreg Roach } 2149867b2f0SGreg Roach 2159867b2f0SGreg Roach /** 216e539f5c6SGreg Roach * @param Family|null $family 217e539f5c6SGreg Roach * @param bool|null $edit 218e539f5c6SGreg Roach * 219e539f5c6SGreg Roach * @return void 220e539f5c6SGreg Roach * @throws FamilyNotFoundException 221e539f5c6SGreg Roach * @throws FamilyAccessDeniedException 222e539f5c6SGreg Roach */ 223*e364afe4SGreg Roach public static function checkFamilyAccess(Family $family = null, $edit = false): void 224e539f5c6SGreg Roach { 225e539f5c6SGreg Roach if ($family === null) { 226e539f5c6SGreg Roach throw new FamilyNotFoundException(); 227e539f5c6SGreg Roach } 228e539f5c6SGreg Roach 229e539f5c6SGreg Roach if (!$family->canShow() || $edit && (!$family->canEdit() || $family->isPendingDeletion())) { 230e539f5c6SGreg Roach throw new FamilyAccessDeniedException(); 231e539f5c6SGreg Roach } 232e539f5c6SGreg Roach } 233e539f5c6SGreg Roach 234e539f5c6SGreg Roach /** 235e539f5c6SGreg Roach * @param Individual|null $individual 236e539f5c6SGreg Roach * @param bool|null $edit 237e539f5c6SGreg Roach * 238e539f5c6SGreg Roach * @return void 239e539f5c6SGreg Roach * @throws IndividualNotFoundException 240e539f5c6SGreg Roach * @throws IndividualAccessDeniedException 241e539f5c6SGreg Roach */ 242*e364afe4SGreg Roach public static function checkIndividualAccess(Individual $individual = null, $edit = false): void 243e539f5c6SGreg Roach { 244e539f5c6SGreg Roach if ($individual === null) { 245e539f5c6SGreg Roach throw new IndividualNotFoundException(); 246e539f5c6SGreg Roach } 247e539f5c6SGreg Roach 248e539f5c6SGreg Roach if (!$individual->canShow() || $edit && (!$individual->canEdit() || $individual->isPendingDeletion())) { 249e539f5c6SGreg Roach throw new IndividualAccessDeniedException(); 250e539f5c6SGreg Roach } 251e539f5c6SGreg Roach } 252e539f5c6SGreg Roach 253e539f5c6SGreg Roach /** 254e539f5c6SGreg Roach * @param Media|null $media 255e539f5c6SGreg Roach * @param bool|null $edit 256e539f5c6SGreg Roach * 257e539f5c6SGreg Roach * @return void 258e539f5c6SGreg Roach * @throws MediaNotFoundException 259e539f5c6SGreg Roach * @throws MediaAccessDeniedException 260e539f5c6SGreg Roach */ 261*e364afe4SGreg Roach public static function checkMediaAccess(Media $media = null, $edit = false): void 262e539f5c6SGreg Roach { 263e539f5c6SGreg Roach if ($media === null) { 264e539f5c6SGreg Roach throw new MediaNotFoundException(); 265e539f5c6SGreg Roach } 266e539f5c6SGreg Roach 267e539f5c6SGreg Roach if (!$media->canShow() || $edit && (!$media->canEdit() || $media->isPendingDeletion())) { 268e539f5c6SGreg Roach throw new MediaAccessDeniedException(); 269e539f5c6SGreg Roach } 270e539f5c6SGreg Roach } 271e539f5c6SGreg Roach 272e539f5c6SGreg Roach /** 273e539f5c6SGreg Roach * @param Note|null $note 274e539f5c6SGreg Roach * @param bool|null $edit 275e539f5c6SGreg Roach * 276e539f5c6SGreg Roach * @return void 277e539f5c6SGreg Roach * @throws NoteNotFoundException 278e539f5c6SGreg Roach * @throws NoteAccessDeniedException 279e539f5c6SGreg Roach */ 280*e364afe4SGreg Roach public static function checkNoteAccess(Note $note = null, $edit = false): void 281e539f5c6SGreg Roach { 282e539f5c6SGreg Roach if ($note === null) { 283e539f5c6SGreg Roach throw new NoteNotFoundException(); 284e539f5c6SGreg Roach } 285e539f5c6SGreg Roach 286e539f5c6SGreg Roach if (!$note->canShow() || $edit && (!$note->canEdit() || $note->isPendingDeletion())) { 287e539f5c6SGreg Roach throw new NoteAccessDeniedException(); 288e539f5c6SGreg Roach } 289e539f5c6SGreg Roach } 290e539f5c6SGreg Roach 291e539f5c6SGreg Roach /** 292e539f5c6SGreg Roach * @param GedcomRecord|null $record 293e539f5c6SGreg Roach * @param bool|null $edit 294e539f5c6SGreg Roach * 295e539f5c6SGreg Roach * @return void 296e539f5c6SGreg Roach * @throws RecordNotFoundException 297e539f5c6SGreg Roach * @throws RecordAccessDeniedException 298e539f5c6SGreg Roach */ 299*e364afe4SGreg Roach public static function checkRecordAccess(GedcomRecord $record = null, $edit = false): void 300e539f5c6SGreg Roach { 301e539f5c6SGreg Roach if ($record === null) { 302e539f5c6SGreg Roach throw new RecordNotFoundException(); 303e539f5c6SGreg Roach } 304e539f5c6SGreg Roach 305e539f5c6SGreg Roach if (!$record->canShow() || $edit && (!$record->canEdit() || $record->isPendingDeletion())) { 306e539f5c6SGreg Roach throw new RecordAccessDeniedException(); 307e539f5c6SGreg Roach } 308e539f5c6SGreg Roach } 309e539f5c6SGreg Roach 310e539f5c6SGreg Roach /** 311e539f5c6SGreg Roach * @param Repository|null $repository 312e539f5c6SGreg Roach * @param bool|null $edit 313e539f5c6SGreg Roach * 314e539f5c6SGreg Roach * @return void 315e539f5c6SGreg Roach * @throws RepositoryNotFoundException 316e539f5c6SGreg Roach * @throws RepositoryAccessDeniedException 317e539f5c6SGreg Roach */ 318*e364afe4SGreg Roach public static function checkRepositoryAccess(Repository $repository = null, $edit = false): void 319e539f5c6SGreg Roach { 320e539f5c6SGreg Roach if ($repository === null) { 321e539f5c6SGreg Roach throw new RepositoryNotFoundException(); 322e539f5c6SGreg Roach } 323e539f5c6SGreg Roach 324e539f5c6SGreg Roach if (!$repository->canShow() || $edit && (!$repository->canEdit() || $repository->isPendingDeletion())) { 325e539f5c6SGreg Roach throw new RepositoryAccessDeniedException(); 326e539f5c6SGreg Roach } 327e539f5c6SGreg Roach } 328e539f5c6SGreg Roach 329e539f5c6SGreg Roach /** 330e539f5c6SGreg Roach * @param Source|null $source 331e539f5c6SGreg Roach * @param bool|null $edit 332e539f5c6SGreg Roach * 333e539f5c6SGreg Roach * @return void 334e539f5c6SGreg Roach * @throws SourceNotFoundException 335e539f5c6SGreg Roach * @throws SourceAccessDeniedException 336e539f5c6SGreg Roach */ 337*e364afe4SGreg Roach public static function checkSourceAccess(Source $source = null, $edit = false): void 338e539f5c6SGreg Roach { 339e539f5c6SGreg Roach if ($source === null) { 340e539f5c6SGreg Roach throw new SourceNotFoundException(); 341e539f5c6SGreg Roach } 342e539f5c6SGreg Roach 343e539f5c6SGreg Roach if (!$source->canShow() || $edit && (!$source->canEdit() || $source->isPendingDeletion())) { 344e539f5c6SGreg Roach throw new SourceAccessDeniedException(); 345e539f5c6SGreg Roach } 346e539f5c6SGreg Roach } 347a25f0a04SGreg Roach} 348