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 18a25f0a04SGreg Roach/** 1976692c8bSGreg Roach * Authentication. 20a25f0a04SGreg Roach */ 21c1010edaSGreg Roachclass Auth 22c1010edaSGreg Roach{ 234b9ff166SGreg Roach // Privacy constants 244b9ff166SGreg Roach const PRIV_PRIVATE = 2; // Allows visitors to view the item 254b9ff166SGreg Roach const PRIV_USER = 1; // Allows members to access the item 264b9ff166SGreg Roach const PRIV_NONE = 0; // Allows managers to access the item 274b9ff166SGreg Roach const PRIV_HIDE = -1; // Hide the item to all users 284b9ff166SGreg Roach 29a25f0a04SGreg Roach /** 30a25f0a04SGreg Roach * Are we currently logged in? 31a25f0a04SGreg Roach * 32cbc1590aSGreg Roach * @return bool 33a25f0a04SGreg Roach */ 34c1010edaSGreg Roach public static function check() 35c1010edaSGreg Roach { 364b9ff166SGreg Roach return self::id() !== null; 37a25f0a04SGreg Roach } 38a25f0a04SGreg Roach 39a25f0a04SGreg Roach /** 40a25f0a04SGreg Roach * Is the specified/current user an administrator? 41a25f0a04SGreg Roach * 42a25f0a04SGreg Roach * @param User|null $user 43a25f0a04SGreg Roach * 44cbc1590aSGreg Roach * @return bool 45a25f0a04SGreg Roach */ 46c1010edaSGreg Roach public static function isAdmin(User $user = null) 47c1010edaSGreg Roach { 48a25f0a04SGreg Roach if ($user === null) { 49a25f0a04SGreg Roach $user = self::user(); 50a25f0a04SGreg Roach } 51a25f0a04SGreg Roach 52a25f0a04SGreg Roach return $user && $user->getPreference('canadmin') === '1'; 53a25f0a04SGreg Roach } 54a25f0a04SGreg Roach 55a25f0a04SGreg Roach /** 564b9ff166SGreg Roach * Is the specified/current user a manager of a tree? 57a25f0a04SGreg Roach * 5884caa210SGreg Roach * @param Tree $tree 59a25f0a04SGreg Roach * @param User|null $user 60a25f0a04SGreg Roach * 61cbc1590aSGreg Roach * @return bool 62a25f0a04SGreg Roach */ 63c1010edaSGreg Roach public static function isManager(Tree $tree, User $user = null) 64c1010edaSGreg Roach { 65a25f0a04SGreg Roach if ($user === null) { 66a25f0a04SGreg Roach $user = self::user(); 67a25f0a04SGreg Roach } 68a25f0a04SGreg Roach 69a25f0a04SGreg Roach return self::isAdmin($user) || $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 */ 80c1010edaSGreg Roach public static function isModerator(Tree $tree, User $user = null) 81c1010edaSGreg Roach { 82a25f0a04SGreg Roach if ($user === null) { 83a25f0a04SGreg Roach $user = self::user(); 84a25f0a04SGreg Roach } 85a25f0a04SGreg Roach 86a25f0a04SGreg Roach return self::isManager($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'accept'; 87a25f0a04SGreg Roach } 88a25f0a04SGreg Roach 89a25f0a04SGreg Roach /** 904b9ff166SGreg Roach * Is the specified/current user an editor of a tree? 91a25f0a04SGreg Roach * 9284caa210SGreg Roach * @param Tree $tree 93a25f0a04SGreg Roach * @param User|null $user 94a25f0a04SGreg Roach * 95cbc1590aSGreg Roach * @return bool 96a25f0a04SGreg Roach */ 97c1010edaSGreg Roach public static function isEditor(Tree $tree, User $user = null) 98c1010edaSGreg Roach { 99a25f0a04SGreg Roach if ($user === null) { 100a25f0a04SGreg Roach $user = self::user(); 101a25f0a04SGreg Roach } 102a25f0a04SGreg Roach 103a25f0a04SGreg Roach return self::isModerator($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'edit'; 104a25f0a04SGreg Roach } 105a25f0a04SGreg Roach 106a25f0a04SGreg Roach /** 1074b9ff166SGreg Roach * Is the specified/current user a member of a tree? 108a25f0a04SGreg Roach * 10984caa210SGreg Roach * @param Tree $tree 110a25f0a04SGreg Roach * @param User|null $user 111a25f0a04SGreg Roach * 112cbc1590aSGreg Roach * @return bool 113a25f0a04SGreg Roach */ 114c1010edaSGreg Roach public static function isMember(Tree $tree, User $user = null) 115c1010edaSGreg Roach { 116a25f0a04SGreg Roach if ($user === null) { 117a25f0a04SGreg Roach $user = self::user(); 118a25f0a04SGreg Roach } 119a25f0a04SGreg Roach 120a25f0a04SGreg Roach return self::isEditor($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'access'; 121a25f0a04SGreg Roach } 122a25f0a04SGreg Roach 123a25f0a04SGreg Roach /** 1244b9ff166SGreg Roach * What is the specified/current user's access level within a tree? 1254b9ff166SGreg Roach * 1264b9ff166SGreg Roach * @param Tree $tree 1274b9ff166SGreg Roach * @param User|null $user 1284b9ff166SGreg Roach * 129cbc1590aSGreg Roach * @return int 1304b9ff166SGreg Roach */ 131c1010edaSGreg Roach public static function accessLevel(Tree $tree, User $user = null) 132c1010edaSGreg Roach { 1334b9ff166SGreg Roach if ($user === null) { 1344b9ff166SGreg Roach $user = self::user(); 1354b9ff166SGreg Roach } 1364b9ff166SGreg Roach 1374b9ff166SGreg Roach if (self::isManager($tree, $user)) { 1384b9ff166SGreg Roach return self::PRIV_NONE; 1394b9ff166SGreg Roach } elseif (self::isMember($tree, $user)) { 1404b9ff166SGreg Roach return self::PRIV_USER; 1414b9ff166SGreg Roach } else { 1424b9ff166SGreg Roach return self::PRIV_PRIVATE; 1434b9ff166SGreg Roach } 1444b9ff166SGreg Roach } 1454b9ff166SGreg Roach 1464b9ff166SGreg Roach /** 147a25f0a04SGreg Roach * The ID of the authenticated user, from the current session. 148a25f0a04SGreg Roach * 149*c3ffc4cbSGreg Roach * @return int|null 150a25f0a04SGreg Roach */ 151c1010edaSGreg Roach public static function id() 152c1010edaSGreg Roach { 15331bc7874SGreg Roach return Session::get('wt_user'); 154a25f0a04SGreg Roach } 155a25f0a04SGreg Roach 156a25f0a04SGreg Roach /** 157a25f0a04SGreg Roach * The authenticated user, from the current session. 158a25f0a04SGreg Roach * 159a25f0a04SGreg Roach * @return User 160a25f0a04SGreg Roach */ 161c1010edaSGreg Roach public static function user() 162c1010edaSGreg Roach { 1634b9ff166SGreg Roach $user = User::find(self::id()); 164a25f0a04SGreg Roach if ($user === null) { 165a25f0a04SGreg Roach $visitor = new \stdClass; 166a25f0a04SGreg Roach $visitor->user_id = ''; 167a25f0a04SGreg Roach $visitor->user_name = ''; 168a25f0a04SGreg Roach $visitor->real_name = ''; 169a25f0a04SGreg Roach $visitor->email = ''; 170a25f0a04SGreg Roach 171a25f0a04SGreg Roach return new User($visitor); 172a25f0a04SGreg Roach } else { 173a25f0a04SGreg Roach return $user; 174a25f0a04SGreg Roach } 175a25f0a04SGreg Roach } 176a25f0a04SGreg Roach 177a25f0a04SGreg Roach /** 178a25f0a04SGreg Roach * Login directly as an explicit user - for masquerading. 179a25f0a04SGreg Roach * 180a25f0a04SGreg Roach * @param User $user 181a25f0a04SGreg Roach */ 182c1010edaSGreg Roach public static function login(User $user) 183c1010edaSGreg Roach { 184e988f922SGreg Roach Session::regenerate(false); 18531bc7874SGreg Roach Session::put('wt_user', $user->getUserId()); 186a25f0a04SGreg Roach } 187a25f0a04SGreg Roach 188a25f0a04SGreg Roach /** 189a25f0a04SGreg Roach * End the session for the current user. 190a25f0a04SGreg Roach */ 191c1010edaSGreg Roach public static function logout() 192c1010edaSGreg Roach { 19331bc7874SGreg Roach Session::regenerate(true); 194a25f0a04SGreg Roach } 195a25f0a04SGreg Roach} 196