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