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