]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Authentication.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Abstract authentication class for galette
10 * Copyright © 2009-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
27 * @category Authentication
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
37 namespace Galette\Core
;
40 * Abstract authentication class for galette
42 * @category Authentication
43 * @name Authentication
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
52 abstract class Authentication
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;
63 private $admin = false;
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;
77 * @param string $user user's login
78 * @param string $passe user's password
82 abstract public function logIn($user, $passe);
85 * Does this login already exists ?
86 * These function should be used for setting admin login into Preferences
88 * @param string $user the username
90 * @return true if the username already exists, false otherwise
92 abstract public function loginExists($user);
95 * Login for the superuser
97 * @param string $login name
98 * @param Preferences $preferences Preferences instance
102 public function logAdmin($login, Preferences
$preferences)
104 $this->logged
= true;
105 $this->name
= 'Admin';
106 $this->login
= $login;
108 $this->active
= true;
109 $this->staff
= false;
110 $this->uptodate
= false;
112 $this->lang
= $preferences->pref_lang
;
113 //a flag for super admin only, since it's not a regular user
114 $this->superadmin
= true;
118 * Authenticate from cron
120 * @param string $name Service name
124 public function logCron($name)
126 //known cronable files
127 $ok = array('reminder');
129 if (in_array($name, $ok)) {
130 $this->logged
= true;
132 $this->login
= 'cron';
134 trigger_error('Not authorized!', E_USER_ERROR
);
139 * Log out user and unset variables
143 public function logOut()
146 $this->logged
= false;
149 $this->admin
= false;
150 $this->active
= false;
151 $this->superadmin
= false;
152 $this->staff
= false;
153 $this->uptodate
= false;
161 public function isLogged()
163 return $this->logged
;
171 public function isAdmin()
177 * Is user super admin?
181 public function isSuperAdmin()
183 return $this->superadmin
;
191 public function isActive()
193 return $this->active
;
197 * Is user member of staff?
201 public function isStaff()
211 public function isCron()
217 * Is user a group manager?
218 * If no group id is specified, check if user is manager for at
221 * @param array|int $id_group Group(s) identifier(s)
225 public function isGroupManager($id_group = null)
228 if ($this->isAdmin() ||
$this->isStaff()) {
231 if ($id_group === null) {
232 $manager = count($this->managed_groups
) > 0;
234 $groups = (array)$id_group;
236 foreach ($groups as $group) {
237 if (in_array($group, $this->managed_groups
)) {
248 * Is user currently up to date?
249 * An up to date member is active and either due free, or with up to date
254 public function isUp2Date()
256 return $this->uptodate
;
260 * Display logged in member name
262 * @param boolean $only_name If we want only the name without any additional text
266 public function loggedInAs($only_name = false)
268 $n = $this->name
. ' ' . $this->surname
. ' (' . $this->login
. ')';
269 if ($only_name === false) {
273 _T("Logged in as:<br/>%login")
281 * Global getter method
283 * @param string $name name of the property we want to retrieve
285 * @return false|object the called property
287 public function __get($name)
289 $forbidden = array('logged', 'admin', 'active');
290 if (!in_array($name, $forbidden) && isset($this->$name)) {
298 * Global setter method
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
305 public function __set($name, $value)
307 $this->$name = $value;
311 * get user access level
315 public function getAccessLevel()
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
;
327 return self
::ACCESS_USER
;