xref: /webtrees/app/Auth.php (revision ffd703ea1e658c7dcb5e5f1f9ef137a420f2d167)
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
19/**
20 * Class Auth - authentication functions
21 */
22class Auth {
23	// Privacy constants
24	const PRIV_PRIVATE = 2; // Allows visitors to view the item
25	const PRIV_USER    = 1; // Allows members to access the item
26	const PRIV_NONE    = 0; // Allows managers to access the item
27	const PRIV_HIDE    = -1; // Hide the item to all users
28
29	/**
30	 * Are we currently logged in?
31	 *
32	 * @return boolean
33	 */
34	public static function check() {
35		return self::id() !== null;
36	}
37
38	/**
39	 * Is the specified/current user an administrator?
40	 *
41	 * @param User|null $user
42	 *
43	 * @return boolean
44	 */
45	public static function isAdmin(User $user = null) {
46		if ($user === null) {
47			$user = self::user();
48		}
49
50		return $user && $user->getPreference('canadmin') === '1';
51	}
52
53	/**
54	 * Is the specified/current user a manager of a tree?
55	 *
56	 * @param Tree      $tree
57	 * @param User|null $user
58	 *
59	 * @return boolean
60	 */
61	public static function isManager(Tree $tree, User $user = null) {
62		if ($user === null) {
63			$user = self::user();
64		}
65
66		return self::isAdmin($user) || $user && $tree->getUserPreference($user, 'canedit') === 'admin';
67	}
68
69	/**
70	 * Is the specified/current user a moderator of a tree?
71	 *
72	 * @param Tree      $tree
73	 * @param User|null $user
74	 *
75	 * @return boolean
76	 */
77	public static function isModerator(Tree $tree, User $user = null) {
78		if ($user === null) {
79			$user = self::user();
80		}
81
82		return self::isManager($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'accept';
83	}
84
85	/**
86	 * Is the specified/current user an editor of a tree?
87	 *
88	 * @param Tree      $tree
89	 * @param User|null $user
90	 *
91	 *
92	 * @return boolean
93	 */
94	public static function isEditor(Tree $tree, User $user = null) {
95		if ($user === null) {
96			$user = self::user();
97		}
98
99		return self::isModerator($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'edit';
100	}
101
102	/**
103	 * Is the specified/current user a member of a tree?
104	 *
105	 * @param Tree      $tree
106	 * @param User|null $user
107	 *
108	 * @return boolean
109	 */
110	public static function isMember(Tree $tree, User $user = null) {
111		if ($user === null) {
112			$user = self::user();
113		}
114
115		return self::isEditor($tree, $user) || $user && $tree->getUserPreference($user, 'canedit') === 'access';
116	}
117
118	/**
119	 * What is the specified/current user's access level within a tree?
120	 *
121	 * @param Tree      $tree
122	 * @param User|null $user
123	 *
124	 * @return integer
125	 */
126	public static function accessLevel(Tree $tree, User $user = null) {
127		if ($user === null) {
128			$user = self::user();
129		}
130
131		if (self::isManager($tree, $user)) {
132			return self::PRIV_NONE;
133		} elseif (self::isMember($tree, $user)) {
134			return self::PRIV_USER;
135		} else {
136			return self::PRIV_PRIVATE;
137		}
138	}
139
140	/**
141	 * Is the current visitor a search engine?  The global is set in session.php
142	 *
143	 * @return boolean
144	 */
145	public static function isSearchEngine() {
146		global $SEARCH_SPIDER;
147
148		return $SEARCH_SPIDER;
149	}
150
151	/**
152	 * The ID of the authenticated user, from the current session.
153	 *
154	 * @return string|null
155	 */
156	public static function id() {
157		return Session::get('wt_user');
158	}
159
160	/**
161	 * The authenticated user, from the current session.
162	 *
163	 * @return User
164	 */
165	public static function user() {
166		$user = User::find(self::id());
167		if ($user === null) {
168			$visitor = new \stdClass;
169			$visitor->user_id = '';
170			$visitor->user_name = '';
171			$visitor->real_name = '';
172			$visitor->email = '';
173
174			return new User($visitor);
175		} else {
176			return $user;
177		}
178	}
179
180	/**
181	 * Login directly as an explicit user - for masquerading.
182	 *
183	 * @param User $user
184	 */
185	public static function login(User $user) {
186		Session::put('wt_user', $user->getUserId());
187		Session::regenerate(false);
188	}
189
190	/**
191	 * End the session for the current user.
192	 */
193	public static function logout() {
194		Session::regenerate(true);
195	}
196}
197