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 * The ID of the authenticated user, from the current session. 116 * 117 * @return string|null 118 */ 119 public static function id() { 120 global $WT_SESSION; 121 122 return $WT_SESSION ? $WT_SESSION->wt_user : null; 123 } 124 125 /** 126 * The authenticated user, from the current session. 127 * 128 * @return User 129 */ 130 public static function user() { 131 $user = User::find(Auth::id()); 132 if ($user === null) { 133 $visitor = new \stdClass; 134 $visitor->user_id = ''; 135 $visitor->user_name = ''; 136 $visitor->real_name = ''; 137 $visitor->email = ''; 138 139 return new User($visitor); 140 } else { 141 return $user; 142 } 143 } 144 145 /** 146 * Login directly as an explicit user - for masquerading. 147 * 148 * @param User $user 149 */ 150 public static function login(User $user) { 151 global $WT_SESSION; 152 153 $WT_SESSION->wt_user = $user->getUserId(); 154 Zend_Session::regenerateId(); 155 } 156 157 /** 158 * End the session for the current user. 159 */ 160 public static function logout() { 161 Zend_Session::regenerateId(); 162 Zend_Session::destroy(); 163 } 164} 165