xref: /webtrees/app/Auth.php (revision dd04c183d8beed05be2226b30b7dda485ea4538a)
1a25f0a04SGreg Roach<?php
2*dd04c183SGreg 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	 *
52a25f0a04SGreg Roach	 * @param Tree|null $tree
53a25f0a04SGreg Roach	 * @param User|null    $user
54a25f0a04SGreg Roach	 *
55a25f0a04SGreg Roach	 * @return boolean
56a25f0a04SGreg Roach	 */
57a25f0a04SGreg Roach	public static function isManager(Tree $tree = null, User $user = null) {
58a25f0a04SGreg Roach		global $WT_TREE;
59a25f0a04SGreg Roach
60a25f0a04SGreg Roach		if ($tree === null) {
61a25f0a04SGreg Roach			$tree = $WT_TREE;
62a25f0a04SGreg Roach		}
63a25f0a04SGreg Roach
64a25f0a04SGreg Roach		if ($user === null) {
65a25f0a04SGreg Roach			$user = self::user();
66a25f0a04SGreg Roach		}
67a25f0a04SGreg Roach
68a25f0a04SGreg Roach		return self::isAdmin($user) || $user && $tree->getUserPreference($user, 'canedit') === 'admin';
69a25f0a04SGreg Roach	}
70a25f0a04SGreg Roach
71a25f0a04SGreg Roach	/**
72a25f0a04SGreg Roach	 * Is a user a moderator of a tree?
73a25f0a04SGreg Roach	 *
74a25f0a04SGreg Roach	 * @param Tree|null $tree
75a25f0a04SGreg Roach	 * @param User|null    $user
76a25f0a04SGreg Roach	 *
77a25f0a04SGreg Roach	 * @return boolean
78a25f0a04SGreg Roach	 */
79a25f0a04SGreg Roach	public static function isModerator(Tree $tree = null, User $user = null) {
80a25f0a04SGreg Roach		global $WT_TREE;
81a25f0a04SGreg Roach
82a25f0a04SGreg Roach		if ($tree === null) {
83a25f0a04SGreg Roach			$tree = $WT_TREE;
84a25f0a04SGreg Roach		}
85a25f0a04SGreg Roach
86a25f0a04SGreg Roach		if ($user === null) {
87a25f0a04SGreg Roach			$user = self::user();
88a25f0a04SGreg Roach		}
89a25f0a04SGreg Roach
90a25f0a04SGreg Roach		return self::isManager($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'accept';
91a25f0a04SGreg Roach	}
92a25f0a04SGreg Roach
93a25f0a04SGreg Roach	/**
94a25f0a04SGreg Roach	 * Is a user an editor of a tree?
95a25f0a04SGreg Roach	 *
96a25f0a04SGreg Roach	 * @param Tree|null $tree
97a25f0a04SGreg Roach	 * @param User|null    $user
98a25f0a04SGreg Roach	 *
99a25f0a04SGreg Roach	 *
100a25f0a04SGreg Roach	 * @return boolean
101a25f0a04SGreg Roach	 */
102a25f0a04SGreg Roach	public static function isEditor(Tree $tree = null, User $user = null) {
103a25f0a04SGreg Roach		global $WT_TREE;
104a25f0a04SGreg Roach
105a25f0a04SGreg Roach		if ($tree === null) {
106a25f0a04SGreg Roach			$tree = $WT_TREE;
107a25f0a04SGreg Roach		}
108a25f0a04SGreg Roach
109a25f0a04SGreg Roach		if ($user === null) {
110a25f0a04SGreg Roach			$user = self::user();
111a25f0a04SGreg Roach		}
112a25f0a04SGreg Roach
113a25f0a04SGreg Roach		return self::isModerator($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'edit';
114a25f0a04SGreg Roach	}
115a25f0a04SGreg Roach
116a25f0a04SGreg Roach	/**
117a25f0a04SGreg Roach	 * Is a user a member of a tree?
118a25f0a04SGreg Roach	 *
119a25f0a04SGreg Roach	 * @param Tree|null $tree
120a25f0a04SGreg Roach	 * @param User|null    $user
121a25f0a04SGreg Roach	 *
122a25f0a04SGreg Roach	 * @return boolean
123a25f0a04SGreg Roach	 */
124a25f0a04SGreg Roach	public static function isMember(Tree $tree = null, User $user = null) {
125a25f0a04SGreg Roach		global $WT_TREE;
126a25f0a04SGreg Roach
127a25f0a04SGreg Roach		if ($tree === null) {
128a25f0a04SGreg Roach			$tree = $WT_TREE;
129a25f0a04SGreg Roach		}
130a25f0a04SGreg Roach
131a25f0a04SGreg Roach		if ($user === null) {
132a25f0a04SGreg Roach			$user = self::user();
133a25f0a04SGreg Roach		}
134a25f0a04SGreg Roach
135a25f0a04SGreg Roach		return self::isEditor($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'access';
136a25f0a04SGreg Roach	}
137a25f0a04SGreg Roach
138a25f0a04SGreg Roach	/**
139a25f0a04SGreg Roach	 * The ID of the authenticated user, from the current session.
140a25f0a04SGreg Roach	 *
141a25f0a04SGreg Roach	 * @return string|null
142a25f0a04SGreg Roach	 */
143a25f0a04SGreg Roach	public static function id() {
144a25f0a04SGreg Roach		global $WT_SESSION;
145a25f0a04SGreg Roach
146a25f0a04SGreg Roach		return $WT_SESSION ? $WT_SESSION->wt_user : null;
147a25f0a04SGreg Roach	}
148a25f0a04SGreg Roach
149a25f0a04SGreg Roach	/**
150a25f0a04SGreg Roach	 * The authenticated user, from the current session.
151a25f0a04SGreg Roach	 *
152a25f0a04SGreg Roach	 * @return User
153a25f0a04SGreg Roach	 */
154a25f0a04SGreg Roach	public static function user() {
155a25f0a04SGreg Roach		$user = User::find(Auth::id());
156a25f0a04SGreg Roach		if ($user === null) {
157a25f0a04SGreg Roach			$visitor = new \stdClass;
158a25f0a04SGreg Roach			$visitor->user_id = '';
159a25f0a04SGreg Roach			$visitor->user_name = '';
160a25f0a04SGreg Roach			$visitor->real_name = '';
161a25f0a04SGreg Roach			$visitor->email = '';
162a25f0a04SGreg Roach
163a25f0a04SGreg Roach			return new User($visitor);
164a25f0a04SGreg Roach		} else {
165a25f0a04SGreg Roach			return $user;
166a25f0a04SGreg Roach		}
167a25f0a04SGreg Roach	}
168a25f0a04SGreg Roach
169a25f0a04SGreg Roach	/**
170a25f0a04SGreg Roach	 * Login directly as an explicit user - for masquerading.
171a25f0a04SGreg Roach	 *
172a25f0a04SGreg Roach	 * @param User $user
173a25f0a04SGreg Roach	 */
174a25f0a04SGreg Roach	public static function login(User $user) {
175a25f0a04SGreg Roach		global $WT_SESSION;
176a25f0a04SGreg Roach
177a25f0a04SGreg Roach		$WT_SESSION->wt_user = $user->getUserId();
178a25f0a04SGreg Roach		Zend_Session::regenerateId();
179a25f0a04SGreg Roach	}
180a25f0a04SGreg Roach
181a25f0a04SGreg Roach	/**
182a25f0a04SGreg Roach	 * End the session for the current user.
183a25f0a04SGreg Roach	 */
184a25f0a04SGreg Roach	public static function logout() {
185a25f0a04SGreg Roach		Zend_Session::regenerateId();
186a25f0a04SGreg Roach		Zend_Session::destroy();
187a25f0a04SGreg Roach	}
188a25f0a04SGreg Roach}
189