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