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 */ 142e364afe4SGreg 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 */ 162e364afe4SGreg 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 */ 184e364afe4SGreg 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 */ 195e364afe4SGreg 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 */ 208e364afe4SGreg Roach public static function checkComponentAccess(ModuleInterface $module, string $component, Tree $tree, UserInterface $user): void 2099867b2f0SGreg Roach { 210e364afe4SGreg 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 */ 223e364afe4SGreg 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 229*1450f098SGreg Roach if (!$family->canShow()) { 230*1450f098SGreg Roach throw new FamilyAccessDeniedException(); 231*1450f098SGreg Roach } 232*1450f098SGreg Roach 233*1450f098SGreg Roach if ($edit && !$family->canEdit()) { 234e539f5c6SGreg Roach throw new FamilyAccessDeniedException(); 235e539f5c6SGreg Roach } 236e539f5c6SGreg Roach } 237e539f5c6SGreg Roach 238e539f5c6SGreg Roach /** 239e539f5c6SGreg Roach * @param Individual|null $individual 240e539f5c6SGreg Roach * @param bool|null $edit 241e539f5c6SGreg Roach * 242e539f5c6SGreg Roach * @return void 243e539f5c6SGreg Roach * @throws IndividualNotFoundException 244e539f5c6SGreg Roach * @throws IndividualAccessDeniedException 245e539f5c6SGreg Roach */ 246e364afe4SGreg Roach public static function checkIndividualAccess(Individual $individual = null, $edit = false): void 247e539f5c6SGreg Roach { 248e539f5c6SGreg Roach if ($individual === null) { 249e539f5c6SGreg Roach throw new IndividualNotFoundException(); 250e539f5c6SGreg Roach } 251e539f5c6SGreg Roach 252*1450f098SGreg Roach if (!$individual->canShow()) { 253*1450f098SGreg Roach throw new IndividualAccessDeniedException(); 254*1450f098SGreg Roach } 255*1450f098SGreg Roach 256*1450f098SGreg Roach if ($edit && !$individual->canEdit()) { 257e539f5c6SGreg Roach throw new IndividualAccessDeniedException(); 258e539f5c6SGreg Roach } 259e539f5c6SGreg Roach } 260e539f5c6SGreg Roach 261e539f5c6SGreg Roach /** 262e539f5c6SGreg Roach * @param Media|null $media 263e539f5c6SGreg Roach * @param bool|null $edit 264e539f5c6SGreg Roach * 265e539f5c6SGreg Roach * @return void 266e539f5c6SGreg Roach * @throws MediaNotFoundException 267e539f5c6SGreg Roach * @throws MediaAccessDeniedException 268e539f5c6SGreg Roach */ 269e364afe4SGreg Roach public static function checkMediaAccess(Media $media = null, $edit = false): void 270e539f5c6SGreg Roach { 271e539f5c6SGreg Roach if ($media === null) { 272e539f5c6SGreg Roach throw new MediaNotFoundException(); 273e539f5c6SGreg Roach } 274e539f5c6SGreg Roach 275*1450f098SGreg Roach if (!$media->canShow()) { 276*1450f098SGreg Roach throw new MediaAccessDeniedException(); 277*1450f098SGreg Roach } 278*1450f098SGreg Roach 279*1450f098SGreg Roach if ($edit && !$media->canEdit()) { 280e539f5c6SGreg Roach throw new MediaAccessDeniedException(); 281e539f5c6SGreg Roach } 282e539f5c6SGreg Roach } 283e539f5c6SGreg Roach 284e539f5c6SGreg Roach /** 285e539f5c6SGreg Roach * @param Note|null $note 286e539f5c6SGreg Roach * @param bool|null $edit 287e539f5c6SGreg Roach * 288e539f5c6SGreg Roach * @return void 289e539f5c6SGreg Roach * @throws NoteNotFoundException 290e539f5c6SGreg Roach * @throws NoteAccessDeniedException 291e539f5c6SGreg Roach */ 292e364afe4SGreg Roach public static function checkNoteAccess(Note $note = null, $edit = false): void 293e539f5c6SGreg Roach { 294e539f5c6SGreg Roach if ($note === null) { 295e539f5c6SGreg Roach throw new NoteNotFoundException(); 296e539f5c6SGreg Roach } 297e539f5c6SGreg Roach 298*1450f098SGreg Roach if (!$note->canShow()) { 299*1450f098SGreg Roach throw new NoteAccessDeniedException(); 300*1450f098SGreg Roach } 301*1450f098SGreg Roach 302*1450f098SGreg Roach if ($edit && !$note->canEdit()) { 303e539f5c6SGreg Roach throw new NoteAccessDeniedException(); 304e539f5c6SGreg Roach } 305e539f5c6SGreg Roach } 306e539f5c6SGreg Roach 307e539f5c6SGreg Roach /** 308e539f5c6SGreg Roach * @param GedcomRecord|null $record 309e539f5c6SGreg Roach * @param bool|null $edit 310e539f5c6SGreg Roach * 311e539f5c6SGreg Roach * @return void 312e539f5c6SGreg Roach * @throws RecordNotFoundException 313e539f5c6SGreg Roach * @throws RecordAccessDeniedException 314e539f5c6SGreg Roach */ 315e364afe4SGreg Roach public static function checkRecordAccess(GedcomRecord $record = null, $edit = false): void 316e539f5c6SGreg Roach { 317e539f5c6SGreg Roach if ($record === null) { 318e539f5c6SGreg Roach throw new RecordNotFoundException(); 319e539f5c6SGreg Roach } 320e539f5c6SGreg Roach 321*1450f098SGreg Roach if (!$record->canShow()) { 322*1450f098SGreg Roach throw new RecordAccessDeniedException(); 323*1450f098SGreg Roach } 324*1450f098SGreg Roach 325*1450f098SGreg Roach if ($edit && !$record->canEdit()) { 326e539f5c6SGreg Roach throw new RecordAccessDeniedException(); 327e539f5c6SGreg Roach } 328e539f5c6SGreg Roach } 329e539f5c6SGreg Roach 330e539f5c6SGreg Roach /** 331e539f5c6SGreg Roach * @param Repository|null $repository 332e539f5c6SGreg Roach * @param bool|null $edit 333e539f5c6SGreg Roach * 334e539f5c6SGreg Roach * @return void 335e539f5c6SGreg Roach * @throws RepositoryNotFoundException 336e539f5c6SGreg Roach * @throws RepositoryAccessDeniedException 337e539f5c6SGreg Roach */ 338e364afe4SGreg Roach public static function checkRepositoryAccess(Repository $repository = null, $edit = false): void 339e539f5c6SGreg Roach { 340e539f5c6SGreg Roach if ($repository === null) { 341e539f5c6SGreg Roach throw new RepositoryNotFoundException(); 342e539f5c6SGreg Roach } 343e539f5c6SGreg Roach 344*1450f098SGreg Roach if (!$repository->canShow()) { 345*1450f098SGreg Roach throw new RepositoryAccessDeniedException(); 346*1450f098SGreg Roach } 347*1450f098SGreg Roach 348*1450f098SGreg Roach if ($edit && !$repository->canEdit()) { 349e539f5c6SGreg Roach throw new RepositoryAccessDeniedException(); 350e539f5c6SGreg Roach } 351e539f5c6SGreg Roach } 352e539f5c6SGreg Roach 353e539f5c6SGreg Roach /** 354e539f5c6SGreg Roach * @param Source|null $source 355e539f5c6SGreg Roach * @param bool|null $edit 356e539f5c6SGreg Roach * 357e539f5c6SGreg Roach * @return void 358e539f5c6SGreg Roach * @throws SourceNotFoundException 359e539f5c6SGreg Roach * @throws SourceAccessDeniedException 360e539f5c6SGreg Roach */ 361e364afe4SGreg Roach public static function checkSourceAccess(Source $source = null, $edit = false): void 362e539f5c6SGreg Roach { 363e539f5c6SGreg Roach if ($source === null) { 364e539f5c6SGreg Roach throw new SourceNotFoundException(); 365e539f5c6SGreg Roach } 366e539f5c6SGreg Roach 367*1450f098SGreg Roach if (!$source->canShow()) { 368*1450f098SGreg Roach throw new SourceAccessDeniedException(); 369*1450f098SGreg Roach } 370*1450f098SGreg Roach 371*1450f098SGreg Roach if ($edit && !$source->canEdit()) { 372e539f5c6SGreg Roach throw new SourceAccessDeniedException(); 373e539f5c6SGreg Roach } 374e539f5c6SGreg Roach } 375a25f0a04SGreg Roach} 376