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