1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2017 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16namespace Fisharebest\Webtrees; 17 18/** 19 * Authentication. 20 */ 21class Auth { 22 // Privacy constants 23 const PRIV_PRIVATE = 2; // Allows visitors to view the item 24 const PRIV_USER = 1; // Allows members to access the item 25 const PRIV_NONE = 0; // Allows managers to access the item 26 const PRIV_HIDE = -1; // Hide the item to all users 27 28 /** 29 * Are we currently logged in? 30 * 31 * @return bool 32 */ 33 public static function check() { 34 return self::id() !== null; 35 } 36 37 /** 38 * Is the specified/current user an administrator? 39 * 40 * @param User|null $user 41 * 42 * @return bool 43 */ 44 public static function isAdmin(User $user = null) { 45 if ($user === null) { 46 $user = self::user(); 47 } 48 49 return $user && $user->getPreference('canadmin') === '1'; 50 } 51 52 /** 53 * Is the specified/current user a manager of a tree? 54 * 55 * @param Tree $tree 56 * @param User|null $user 57 * 58 * @return bool 59 */ 60 public static function isManager(Tree $tree, User $user = null) { 61 if ($user === null) { 62 $user = self::user(); 63 } 64 65 return self::isAdmin($user) || $user && $tree->getUserPreference($user, 'canedit') === 'admin'; 66 } 67 68 /** 69 * Is the specified/current user a moderator of a tree? 70 * 71 * @param Tree $tree 72 * @param User|null $user 73 * 74 * @return bool 75 */ 76 public static function isModerator(Tree $tree, User $user = null) { 77 if ($user === null) { 78 $user = self::user(); 79 } 80 81 return self::isManager($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'accept'; 82 } 83 84 /** 85 * Is the specified/current user an editor of a tree? 86 * 87 * @param Tree $tree 88 * @param User|null $user 89 * 90 * @return bool 91 */ 92 public static function isEditor(Tree $tree, User $user = null) { 93 if ($user === null) { 94 $user = self::user(); 95 } 96 97 return self::isModerator($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'edit'; 98 } 99 100 /** 101 * Is the specified/current user a member of a tree? 102 * 103 * @param Tree $tree 104 * @param User|null $user 105 * 106 * @return bool 107 */ 108 public static function isMember(Tree $tree, User $user = null) { 109 if ($user === null) { 110 $user = self::user(); 111 } 112 113 return self::isEditor($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'access'; 114 } 115 116 /** 117 * What is the specified/current user's access level within a tree? 118 * 119 * @param Tree $tree 120 * @param User|null $user 121 * 122 * @return int 123 */ 124 public static function accessLevel(Tree $tree, User $user = null) { 125 if ($user === null) { 126 $user = self::user(); 127 } 128 129 if (self::isManager($tree, $user)) { 130 return self::PRIV_NONE; 131 } elseif (self::isMember($tree, $user)) { 132 return self::PRIV_USER; 133 } else { 134 return self::PRIV_PRIVATE; 135 } 136 } 137 138 /** 139 * Is the current visitor a search engine? The global is set in session.php 140 * 141 * @return bool 142 */ 143 public static function isSearchEngine() { 144 global $SEARCH_SPIDER; 145 146 return $SEARCH_SPIDER; 147 } 148 149 /** 150 * The ID of the authenticated user, from the current session. 151 * 152 * @return string|null 153 */ 154 public static function id() { 155 return Session::get('wt_user'); 156 } 157 158 /** 159 * The authenticated user, from the current session. 160 * 161 * @return User 162 */ 163 public static function user() { 164 $user = User::find(self::id()); 165 if ($user === null) { 166 $visitor = new \stdClass; 167 $visitor->user_id = ''; 168 $visitor->user_name = ''; 169 $visitor->real_name = ''; 170 $visitor->email = ''; 171 172 return new User($visitor); 173 } else { 174 return $user; 175 } 176 } 177 178 /** 179 * Login directly as an explicit user - for masquerading. 180 * 181 * @param User $user 182 */ 183 public static function login(User $user) { 184 Session::regenerate(false); 185 Session::put('wt_user', $user->getUserId()); 186 } 187 188 /** 189 * End the session for the current user. 190 */ 191 public static function logout() { 192 Session::regenerate(true); 193 } 194} 195