1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees; 17a25f0a04SGreg Roach 1879529c87SGreg Roachuse stdClass; 1979529c87SGreg Roach 20a25f0a04SGreg Roach/** 2176692c8bSGreg Roach * Authentication. 22a25f0a04SGreg Roach */ 23c1010edaSGreg Roachclass Auth 24c1010edaSGreg Roach{ 254b9ff166SGreg Roach // Privacy constants 264b9ff166SGreg Roach const PRIV_PRIVATE = 2; // Allows visitors to view the item 274b9ff166SGreg Roach const PRIV_USER = 1; // Allows members to access the item 284b9ff166SGreg Roach const PRIV_NONE = 0; // Allows managers to access the item 294b9ff166SGreg Roach const PRIV_HIDE = -1; // Hide the item to all users 304b9ff166SGreg Roach 31a25f0a04SGreg Roach /** 32a25f0a04SGreg Roach * Are we currently logged in? 33a25f0a04SGreg Roach * 34cbc1590aSGreg Roach * @return bool 35a25f0a04SGreg Roach */ 36*8f53f488SRico Sonntag public static function check(): bool 37c1010edaSGreg Roach { 384b9ff166SGreg Roach return self::id() !== null; 39a25f0a04SGreg Roach } 40a25f0a04SGreg Roach 41a25f0a04SGreg Roach /** 42a25f0a04SGreg Roach * Is the specified/current user an administrator? 43a25f0a04SGreg Roach * 44a25f0a04SGreg Roach * @param User|null $user 45a25f0a04SGreg Roach * 46cbc1590aSGreg Roach * @return bool 47a25f0a04SGreg Roach */ 48*8f53f488SRico Sonntag public static function isAdmin(User $user = null): bool 49c1010edaSGreg Roach { 50a25f0a04SGreg Roach if ($user === null) { 51a25f0a04SGreg Roach $user = self::user(); 52a25f0a04SGreg Roach } 53a25f0a04SGreg Roach 54a25f0a04SGreg Roach return $user && $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 */ 65*8f53f488SRico Sonntag public static function isManager(Tree $tree, User $user = null): bool 66c1010edaSGreg Roach { 67a25f0a04SGreg Roach if ($user === null) { 68a25f0a04SGreg Roach $user = self::user(); 69a25f0a04SGreg Roach } 70a25f0a04SGreg Roach 71a25f0a04SGreg Roach return self::isAdmin($user) || $user && $tree->getUserPreference($user, 'canedit') === 'admin'; 72a25f0a04SGreg Roach } 73a25f0a04SGreg Roach 74a25f0a04SGreg Roach /** 754b9ff166SGreg Roach * Is the specified/current user a moderator of a tree? 76a25f0a04SGreg Roach * 7784caa210SGreg Roach * @param Tree $tree 78a25f0a04SGreg Roach * @param User|null $user 79a25f0a04SGreg Roach * 80cbc1590aSGreg Roach * @return bool 81a25f0a04SGreg Roach */ 82*8f53f488SRico Sonntag public static function isModerator(Tree $tree, User $user = null): bool 83c1010edaSGreg Roach { 84a25f0a04SGreg Roach if ($user === null) { 85a25f0a04SGreg Roach $user = self::user(); 86a25f0a04SGreg Roach } 87a25f0a04SGreg Roach 88a25f0a04SGreg Roach return self::isManager($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'accept'; 89a25f0a04SGreg Roach } 90a25f0a04SGreg Roach 91a25f0a04SGreg Roach /** 924b9ff166SGreg Roach * Is the specified/current user an editor of a tree? 93a25f0a04SGreg Roach * 9484caa210SGreg Roach * @param Tree $tree 95a25f0a04SGreg Roach * @param User|null $user 96a25f0a04SGreg Roach * 97cbc1590aSGreg Roach * @return bool 98a25f0a04SGreg Roach */ 99*8f53f488SRico Sonntag public static function isEditor(Tree $tree, User $user = null): bool 100c1010edaSGreg Roach { 101a25f0a04SGreg Roach if ($user === null) { 102a25f0a04SGreg Roach $user = self::user(); 103a25f0a04SGreg Roach } 104a25f0a04SGreg Roach 105a25f0a04SGreg Roach return self::isModerator($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'edit'; 106a25f0a04SGreg Roach } 107a25f0a04SGreg Roach 108a25f0a04SGreg Roach /** 1094b9ff166SGreg Roach * Is the specified/current user a member of a tree? 110a25f0a04SGreg Roach * 11184caa210SGreg Roach * @param Tree $tree 112a25f0a04SGreg Roach * @param User|null $user 113a25f0a04SGreg Roach * 114cbc1590aSGreg Roach * @return bool 115a25f0a04SGreg Roach */ 116*8f53f488SRico Sonntag public static function isMember(Tree $tree, User $user = null): bool 117c1010edaSGreg Roach { 118a25f0a04SGreg Roach if ($user === null) { 119a25f0a04SGreg Roach $user = self::user(); 120a25f0a04SGreg Roach } 121a25f0a04SGreg Roach 122a25f0a04SGreg Roach return self::isEditor($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'access'; 123a25f0a04SGreg Roach } 124a25f0a04SGreg Roach 125a25f0a04SGreg Roach /** 1264b9ff166SGreg Roach * What is the specified/current user's access level within a tree? 1274b9ff166SGreg Roach * 1284b9ff166SGreg Roach * @param Tree $tree 1294b9ff166SGreg Roach * @param User|null $user 1304b9ff166SGreg Roach * 131cbc1590aSGreg Roach * @return int 1324b9ff166SGreg Roach */ 133c1010edaSGreg Roach public static function accessLevel(Tree $tree, User $user = null) 134c1010edaSGreg Roach { 1354b9ff166SGreg Roach if ($user === null) { 1364b9ff166SGreg Roach $user = self::user(); 1374b9ff166SGreg Roach } 1384b9ff166SGreg Roach 1394b9ff166SGreg Roach if (self::isManager($tree, $user)) { 1404b9ff166SGreg Roach return self::PRIV_NONE; 1414b9ff166SGreg Roach } elseif (self::isMember($tree, $user)) { 1424b9ff166SGreg Roach return self::PRIV_USER; 1434b9ff166SGreg Roach } else { 1444b9ff166SGreg Roach return self::PRIV_PRIVATE; 1454b9ff166SGreg Roach } 1464b9ff166SGreg Roach } 1474b9ff166SGreg Roach 1484b9ff166SGreg Roach /** 149a25f0a04SGreg Roach * The ID of the authenticated user, from the current session. 150a25f0a04SGreg Roach * 151c3ffc4cbSGreg Roach * @return int|null 152a25f0a04SGreg Roach */ 153c1010edaSGreg Roach public static function id() 154c1010edaSGreg Roach { 15531bc7874SGreg Roach return Session::get('wt_user'); 156a25f0a04SGreg Roach } 157a25f0a04SGreg Roach 158a25f0a04SGreg Roach /** 159a25f0a04SGreg Roach * The authenticated user, from the current session. 160a25f0a04SGreg Roach * 161a25f0a04SGreg Roach * @return User 162a25f0a04SGreg Roach */ 163c1010edaSGreg Roach public static function user() 164c1010edaSGreg Roach { 1654b9ff166SGreg Roach $user = User::find(self::id()); 166a25f0a04SGreg Roach if ($user === null) { 16779529c87SGreg Roach $visitor = new stdClass(); 168a25f0a04SGreg Roach $visitor->user_id = ''; 169a25f0a04SGreg Roach $visitor->user_name = ''; 170a25f0a04SGreg Roach $visitor->real_name = ''; 171a25f0a04SGreg Roach $visitor->email = ''; 172a25f0a04SGreg Roach 173a25f0a04SGreg Roach return new User($visitor); 174a25f0a04SGreg Roach } else { 175a25f0a04SGreg Roach return $user; 176a25f0a04SGreg Roach } 177a25f0a04SGreg Roach } 178a25f0a04SGreg Roach 179a25f0a04SGreg Roach /** 180a25f0a04SGreg Roach * Login directly as an explicit user - for masquerading. 181a25f0a04SGreg Roach * 182a25f0a04SGreg Roach * @param User $user 183a25f0a04SGreg Roach */ 184c1010edaSGreg Roach public static function login(User $user) 185c1010edaSGreg Roach { 186e988f922SGreg Roach Session::regenerate(false); 18731bc7874SGreg Roach Session::put('wt_user', $user->getUserId()); 188a25f0a04SGreg Roach } 189a25f0a04SGreg Roach 190a25f0a04SGreg Roach /** 191a25f0a04SGreg Roach * End the session for the current user. 192a25f0a04SGreg Roach */ 193c1010edaSGreg Roach public static function logout() 194c1010edaSGreg Roach { 19531bc7874SGreg Roach Session::regenerate(true); 196a25f0a04SGreg Roach } 197a25f0a04SGreg Roach} 198