]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Authentication.php
CS: declare visibility for constants
[galette.git] / galette / lib / Galette / Core / Authentication.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Abstract authentication class for galette
7 *
8 * PHP version 5
9 *
10 * Copyright © 2009-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Authentication
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2009-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7dev - 2009-02-28
35 */
36
37 namespace Galette\Core;
38
39 /**
40 * Abstract authentication class for galette
41 *
42 * @category Authentication
43 * @name Authentication
44 * @package Galette
45 * @author Johan Cwiklinski <johan@x-tnd.be>
46 * @copyright 2009-2014 The Galette Team
47 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
48 * @link http://galette.tuxfamily.org
49 * @since Available since 0.7dev - 2009-02-28
50 */
51
52 abstract class Authentication
53 {
54 public const ACCESS_USER = 0;
55 public const ACCESS_MANAGER = 1;
56 public const ACCESS_STAFF = 2;
57 public const ACCESS_ADMIN = 3;
58 public const ACCESS_SUPERADMIN = 4;
59
60 private $login;
61 private $name;
62 private $surname;
63 private $admin = false;
64 private $id;
65 private $lang;
66 private $logged = false;
67 private $active = false;
68 private $superadmin = false;
69 private $staff = false;
70 private $uptodate = false;
71 private $managed_groups = [];
72 private $cron = false;
73
74 /**
75 * Logs in user.
76 *
77 * @param string $user user's login
78 * @param string $passe user's password
79 *
80 * @return boolean
81 */
82 abstract public function logIn($user, $passe);
83
84 /**
85 * Does this login already exists ?
86 * These function should be used for setting admin login into Preferences
87 *
88 * @param string $user the username
89 *
90 * @return true if the username already exists, false otherwise
91 */
92 abstract public function loginExists($user);
93
94 /**
95 * Login for the superuser
96 *
97 * @param string $login name
98 * @param Preferences $preferences Preferences instance
99 *
100 * @return void
101 */
102 public function logAdmin($login, Preferences $preferences)
103 {
104 $this->logged = true;
105 $this->name = 'Admin';
106 $this->login = $login;
107 $this->admin = true;
108 $this->active = true;
109 $this->staff = false;
110 $this->uptodate = false;
111 $this->id = 0;
112 $this->lang = $preferences->pref_lang;
113 //a flag for super admin only, since it's not a regular user
114 $this->superadmin = true;
115 }
116
117 /**
118 * Authenticate from cron
119 *
120 * @param string $name Service name
121 *
122 * @return void
123 */
124 public function logCron($name)
125 {
126 //known cronable files
127 $ok = array('reminder');
128
129 if (in_array($name, $ok)) {
130 $this->logged = true;
131 $this->cron = true;
132 $this->login = 'cron';
133 } else {
134 trigger_error('Not authorized!', E_USER_ERROR);
135 }
136 }
137
138 /**
139 * Log out user and unset variables
140 *
141 * @return void
142 */
143 public function logOut()
144 {
145 $this->id = null;
146 $this->logged = false;
147 $this->name = null;
148 $this->login = null;
149 $this->admin = false;
150 $this->active = false;
151 $this->superadmin = false;
152 $this->staff = false;
153 $this->uptodate = false;
154 }
155
156 /**
157 * Is user logged-in?
158 *
159 * @return bool
160 */
161 public function isLogged()
162 {
163 return $this->logged;
164 }
165
166 /**
167 * Is user admin?
168 *
169 * @return bool
170 */
171 public function isAdmin()
172 {
173 return $this->admin;
174 }
175
176 /**
177 * Is user super admin?
178 *
179 * @return bool
180 */
181 public function isSuperAdmin()
182 {
183 return $this->superadmin;
184 }
185
186 /**
187 * Is user active?
188 *
189 * @return bool
190 */
191 public function isActive()
192 {
193 return $this->active;
194 }
195
196 /**
197 * Is user member of staff?
198 *
199 * @return bool
200 */
201 public function isStaff()
202 {
203 return $this->staff;
204 }
205
206 /**
207 * is user a crontab?
208 *
209 * @return bool
210 */
211 public function isCron()
212 {
213 return $this->cron;
214 }
215
216 /**
217 * Is user a group manager?
218 * If no group id is specified, check if user is manager for at
219 * least one group.
220 *
221 * @param array|int $id_group Group(s) identifier(s)
222 *
223 * @return boolean
224 */
225 public function isGroupManager($id_group = null)
226 {
227 $manager = false;
228 if ($this->isAdmin() || $this->isStaff()) {
229 return true;
230 } else {
231 if ($id_group === null) {
232 $manager = count($this->managed_groups) > 0;
233 } else {
234 $groups = (array)$id_group;
235
236 foreach ($groups as $group) {
237 if (in_array($group, $this->managed_groups)) {
238 $manager = true;
239 break;
240 }
241 }
242 }
243 }
244 return $manager;
245 }
246
247 /**
248 * Is user currently up to date?
249 * An up to date member is active and either due free, or with up to date
250 * subscription
251 *
252 * @return bool
253 */
254 public function isUp2Date()
255 {
256 return $this->uptodate;
257 }
258
259 /**
260 * Display logged in member name
261 *
262 * @param boolean $only_name If we want only the name without any additional text
263 *
264 * @return String
265 */
266 public function loggedInAs($only_name = false)
267 {
268 $n = $this->name . ' ' . $this->surname . ' (' . $this->login . ')';
269 if ($only_name === false) {
270 return str_replace(
271 '%login',
272 $n,
273 _T("Logged in as:<br/>%login")
274 );
275 } else {
276 return $n;
277 }
278 }
279
280 /**
281 * Global getter method
282 *
283 * @param string $name name of the property we want to retrieve
284 *
285 * @return false|object the called property
286 */
287 public function __get($name)
288 {
289 $forbidden = array('logged', 'admin', 'active');
290 if (!in_array($name, $forbidden) && isset($this->$name)) {
291 return $this->$name;
292 } else {
293 return false;
294 }
295 }
296
297 /**
298 * Global setter method
299 *
300 * @param string $name name of the property we want to assign a value to
301 * @param object $value a relevant value for the property
302 *
303 * @return void
304 */
305 public function __set($name, $value)
306 {
307 $this->$name = $value;
308 }
309
310 /**
311 * get user access level
312 *
313 * @return integer
314 */
315 public function getAccessLevel()
316 {
317
318 if ($this->isSuperAdmin()) {
319 return self::ACCESS_SUPERADMIN;
320 } elseif ($this->isAdmin()) {
321 return self::ACCESS_ADMIN;
322 } elseif ($this->isStaff()) {
323 return self::ACCESS_STAFF;
324 } elseif ($this->isGroupManager()) {
325 return self::ACCESS_MANAGER;
326 } else {
327 return self::ACCESS_USER;
328 }
329 }
330 }