]>
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-2023 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-2023 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-2023 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
51 * @property ?string $login
52 * @property ?string $name
53 * @property ?string $surname
54 * @property ?integer $id
55 * @property ?string $lang
56 * @property array $managed_groups
59 abstract class Authentication
61 public const ACCESS_USER
= 0;
62 public const ACCESS_MANAGER
= 1;
63 public const ACCESS_STAFF
= 2;
64 public const ACCESS_ADMIN
= 3;
65 public const ACCESS_SUPERADMIN
= 4;
70 protected $admin = false;
73 protected $logged = false;
74 protected $active = false;
75 protected $superadmin = false;
76 protected $staff = false;
77 protected $uptodate = false;
78 protected $managed_groups = [];
79 protected $cron = false;
84 * @param string $user user's login
85 * @param string $passe user's password
89 abstract public function logIn($user, $passe);
92 * Does this login already exists ?
93 * These function should be used for setting admin login into Preferences
95 * @param string $user the username
97 * @return true if the username already exists, false otherwise
99 abstract public function loginExists($user);
102 * Login for the superuser
104 * @param string $login name
105 * @param Preferences $preferences Preferences instance
109 public function logAdmin($login, Preferences
$preferences)
111 $this->logged
= true;
112 $this->name
= 'Admin';
113 $this->login
= $login;
115 $this->active
= true;
116 $this->staff
= false;
117 $this->uptodate
= false;
119 $this->lang
= $preferences->pref_lang
;
120 //a flag for super admin only, since it's not a regular user
121 $this->superadmin
= true;
125 * Authenticate from cron
127 * @param string $name Service name
131 public function logCron($name)
133 //known cronable files
134 $ok = array('reminder');
136 if (in_array($name, $ok)) {
137 $this->logged
= true;
139 $this->login
= 'cron';
141 trigger_error('Not authorized!', E_USER_ERROR
);
146 * Log out user and unset variables
150 public function logOut()
153 $this->logged
= false;
156 $this->admin
= false;
157 $this->active
= false;
158 $this->superadmin
= false;
159 $this->staff
= false;
160 $this->uptodate
= false;
168 public function isLogged()
170 return $this->logged
;
178 public function isAdmin(): bool
180 return (bool)$this->admin
;
184 * Is user super admin?
188 public function isSuperAdmin(): bool
190 return (bool)$this->superadmin
;
198 public function isActive(): bool
200 return (bool)$this->active
;
204 * Is user member of staff?
208 public function isStaff(): bool
210 return (bool)$this->staff
;
218 public function isCron(): bool
220 return (bool)$this->cron
;
224 * Is user a group manager?
225 * If no group id is specified, check if user is manager for at
228 * @param array|int $id_group Group(s) identifier(s)
232 public function isGroupManager($id_group = null): bool
235 if ($this->isAdmin() ||
$this->isStaff()) {
238 if ($id_group === null) {
239 $manager = count($this->managed_groups
) > 0;
241 $groups = (array)$id_group;
243 foreach ($groups as $group) {
244 if (in_array($group, $this->managed_groups
)) {
259 public function getManagedGroups(): array
261 return $this->managed_groups
;
265 * Is user currently up to date?
266 * An up to date member is active and either due free, or with up to date
271 public function isUp2Date(): bool
273 return (bool)$this->uptodate
;
277 * Display logged in member name
279 * @param boolean $only_name If we want only the name without any additional text
283 public function loggedInAs($only_name = false)
285 $n = $this->name
. ' ' . $this->surname
. ' (' . $this->login
. ')';
286 if ($only_name === false) {
290 _T("Logged in as:<br/>%login")
298 * Global getter method
300 * @param string $name name of the property we want to retrieve
304 public function __get($name)
306 $forbidden = array('logged', 'admin', 'active', 'superadmin', 'staff', 'cron', 'uptodate');
307 if (isset($this->$name) && !in_array($name, $forbidden)) {
310 return (int)$this->$name;
320 * Global isset method
321 * Required for twig to access properties via __get
323 * @param string $name name of the property we want to retrieve
327 public function __isset($name)
329 $forbidden = array('logged', 'admin', 'active', 'superadmin', 'staff', 'cron', 'uptodate');
330 if (isset($this->$name) && !in_array($name, $forbidden)) {
339 * get user access level
343 public function getAccessLevel()
346 if ($this->isSuperAdmin()) {
347 return self
::ACCESS_SUPERADMIN
;
348 } elseif ($this->isAdmin()) {
349 return self
::ACCESS_ADMIN
;
350 } elseif ($this->isStaff()) {
351 return self
::ACCESS_STAFF
;
352 } elseif ($this->isGroupManager()) {
353 return self
::ACCESS_MANAGER
;
355 return self
::ACCESS_USER
;