1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 4*8fcd0d32SGreg 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 2079529c87SGreg Roachuse stdClass; 2179529c87SGreg Roach 22a25f0a04SGreg Roach/** 2376692c8bSGreg Roach * Authentication. 24a25f0a04SGreg Roach */ 25c1010edaSGreg Roachclass Auth 26c1010edaSGreg Roach{ 274b9ff166SGreg Roach // Privacy constants 284b9ff166SGreg Roach const PRIV_PRIVATE = 2; // Allows visitors to view the item 294b9ff166SGreg Roach const PRIV_USER = 1; // Allows members to access the item 304b9ff166SGreg Roach const PRIV_NONE = 0; // Allows managers to access the item 314b9ff166SGreg Roach const PRIV_HIDE = -1; // Hide the item to all users 324b9ff166SGreg Roach 33a25f0a04SGreg Roach /** 34a25f0a04SGreg Roach * Are we currently logged in? 35a25f0a04SGreg Roach * 36cbc1590aSGreg Roach * @return bool 37a25f0a04SGreg Roach */ 388f53f488SRico Sonntag public static function check(): bool 39c1010edaSGreg Roach { 404b9ff166SGreg Roach return self::id() !== null; 41a25f0a04SGreg Roach } 42a25f0a04SGreg Roach 43a25f0a04SGreg Roach /** 44a25f0a04SGreg Roach * Is the specified/current user an administrator? 45a25f0a04SGreg Roach * 46a25f0a04SGreg Roach * @param User|null $user 47a25f0a04SGreg Roach * 48cbc1590aSGreg Roach * @return bool 49a25f0a04SGreg Roach */ 508f53f488SRico Sonntag public static function isAdmin(User $user = null): bool 51c1010edaSGreg Roach { 52cb923727SGreg Roach $user = $user ?? self::user(); 53a25f0a04SGreg Roach 54cb923727SGreg Roach return $user->getPreference('canadmin') === '1'; 55a25f0a04SGreg Roach } 56a25f0a04SGreg Roach 57a25f0a04SGreg Roach /** 584b9ff166SGreg Roach * Is the specified/current user a manager of a tree? 59a25f0a04SGreg Roach * 6084caa210SGreg Roach * @param Tree $tree 61a25f0a04SGreg Roach * @param User|null $user 62a25f0a04SGreg Roach * 63cbc1590aSGreg Roach * @return bool 64a25f0a04SGreg Roach */ 658f53f488SRico Sonntag public static function isManager(Tree $tree, User $user = null): bool 66c1010edaSGreg Roach { 67cb923727SGreg Roach $user = $user ?? self::user(); 68a25f0a04SGreg Roach 69cb923727SGreg Roach return self::isAdmin($user) || $tree->getUserPreference($user, 'canedit') === 'admin'; 70a25f0a04SGreg Roach } 71a25f0a04SGreg Roach 72a25f0a04SGreg Roach /** 734b9ff166SGreg Roach * Is the specified/current user a moderator of a tree? 74a25f0a04SGreg Roach * 7584caa210SGreg Roach * @param Tree $tree 76a25f0a04SGreg Roach * @param User|null $user 77a25f0a04SGreg Roach * 78cbc1590aSGreg Roach * @return bool 79a25f0a04SGreg Roach */ 808f53f488SRico Sonntag public static function isModerator(Tree $tree, User $user = null): bool 81c1010edaSGreg Roach { 82cb923727SGreg Roach $user = $user ?? self::user(); 83a25f0a04SGreg Roach 84cb923727SGreg Roach return self::isManager($tree, $user) || $tree->getUserPreference($user, 'canedit') === 'accept'; 85a25f0a04SGreg Roach } 86a25f0a04SGreg Roach 87a25f0a04SGreg Roach /** 884b9ff166SGreg Roach * Is the specified/current user an editor of a tree? 89a25f0a04SGreg Roach * 9084caa210SGreg Roach * @param Tree $tree 91a25f0a04SGreg Roach * @param User|null $user 92a25f0a04SGreg Roach * 93cbc1590aSGreg Roach * @return bool 94a25f0a04SGreg Roach */ 958f53f488SRico Sonntag public static function isEditor(Tree $tree, User $user = null): bool 96c1010edaSGreg Roach { 97cb923727SGreg Roach $user = $user ?? self::user(); 98a25f0a04SGreg Roach 99cb923727SGreg Roach return self::isModerator($tree, $user) || $tree->getUserPreference($user, 'canedit') === 'edit'; 100a25f0a04SGreg Roach } 101a25f0a04SGreg Roach 102a25f0a04SGreg Roach /** 1034b9ff166SGreg Roach * Is the specified/current user a member of a tree? 104a25f0a04SGreg Roach * 10584caa210SGreg Roach * @param Tree $tree 106a25f0a04SGreg Roach * @param User|null $user 107a25f0a04SGreg Roach * 108cbc1590aSGreg Roach * @return bool 109a25f0a04SGreg Roach */ 1108f53f488SRico Sonntag public static function isMember(Tree $tree, User $user = null): bool 111c1010edaSGreg Roach { 112cb923727SGreg Roach $user = $user ?? self::user(); 113a25f0a04SGreg Roach 114cb923727SGreg Roach return self::isEditor($tree, $user) || $tree->getUserPreference($user, 'canedit') === 'access'; 115a25f0a04SGreg Roach } 116a25f0a04SGreg Roach 117a25f0a04SGreg Roach /** 1184b9ff166SGreg Roach * What is the specified/current user's access level within a tree? 1194b9ff166SGreg Roach * 1204b9ff166SGreg Roach * @param Tree $tree 1214b9ff166SGreg Roach * @param User|null $user 1224b9ff166SGreg Roach * 123cbc1590aSGreg Roach * @return int 1244b9ff166SGreg Roach */ 125c1010edaSGreg Roach public static function accessLevel(Tree $tree, User $user = null) 126c1010edaSGreg Roach { 127cb923727SGreg Roach $user = $user ?? self::user(); 1284b9ff166SGreg Roach 1294b9ff166SGreg Roach if (self::isManager($tree, $user)) { 1304b9ff166SGreg Roach return self::PRIV_NONE; 1314b9ff166SGreg Roach } 132b2ce94c6SRico Sonntag 133b2ce94c6SRico Sonntag if (self::isMember($tree, $user)) { 134b2ce94c6SRico Sonntag return self::PRIV_USER; 135b2ce94c6SRico Sonntag } 136b2ce94c6SRico Sonntag 137b2ce94c6SRico Sonntag return self::PRIV_PRIVATE; 1384b9ff166SGreg Roach } 1394b9ff166SGreg Roach 1404b9ff166SGreg Roach /** 141a25f0a04SGreg Roach * The ID of the authenticated user, from the current session. 142a25f0a04SGreg Roach * 143c3ffc4cbSGreg Roach * @return int|null 144a25f0a04SGreg Roach */ 145c1010edaSGreg Roach public static function id() 146c1010edaSGreg Roach { 14731bc7874SGreg Roach return Session::get('wt_user'); 148a25f0a04SGreg Roach } 149a25f0a04SGreg Roach 150a25f0a04SGreg Roach /** 151a25f0a04SGreg Roach * The authenticated user, from the current session. 152a25f0a04SGreg Roach * 153a25f0a04SGreg Roach * @return User 154a25f0a04SGreg Roach */ 155c1010edaSGreg Roach public static function user() 156c1010edaSGreg Roach { 1574b9ff166SGreg Roach $user = User::find(self::id()); 158cb923727SGreg Roach 159a25f0a04SGreg Roach if ($user === null) { 16079529c87SGreg Roach $visitor = new stdClass(); 161a25f0a04SGreg Roach $visitor->user_id = ''; 162a25f0a04SGreg Roach $visitor->user_name = ''; 163a25f0a04SGreg Roach $visitor->real_name = ''; 164a25f0a04SGreg Roach $visitor->email = ''; 165a25f0a04SGreg Roach 166a25f0a04SGreg Roach return new User($visitor); 167a25f0a04SGreg Roach } 168b2ce94c6SRico Sonntag 169b2ce94c6SRico Sonntag return $user; 170a25f0a04SGreg Roach } 171a25f0a04SGreg Roach 172a25f0a04SGreg Roach /** 173a25f0a04SGreg Roach * Login directly as an explicit user - for masquerading. 174a25f0a04SGreg Roach * 175a25f0a04SGreg Roach * @param User $user 176cb923727SGreg Roach * 177cb923727SGreg Roach * @return void 178a25f0a04SGreg Roach */ 179c1010edaSGreg Roach public static function login(User $user) 180c1010edaSGreg Roach { 181e988f922SGreg Roach Session::regenerate(false); 18231bc7874SGreg Roach Session::put('wt_user', $user->getUserId()); 183a25f0a04SGreg Roach } 184a25f0a04SGreg Roach 185a25f0a04SGreg Roach /** 186a25f0a04SGreg Roach * End the session for the current user. 187cb923727SGreg Roach * 188cb923727SGreg Roach * @return void 189a25f0a04SGreg Roach */ 190c1010edaSGreg Roach public static function logout() 191c1010edaSGreg Roach { 19231bc7874SGreg Roach Session::regenerate(true); 193a25f0a04SGreg Roach } 194a25f0a04SGreg Roach} 195