]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Authentication.php
389450f54f1ed2b07c9910866c71cb33e343f464
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Abstract authentication class for galette
10 * Copyright © 2009-2024 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-2024 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-2024 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;
80 protected $compact_menu = false;
81 protected $dark_mode = false;
86 * @param string $user user's login
87 * @param string $passe user's password
91 abstract public function logIn($user, $passe);
94 * Does this login already exists ?
95 * These function should be used for setting admin login into Preferences
97 * @param string $user the username
99 * @return true if the username already exists, false otherwise
101 abstract public function loginExists($user);
104 * Login for the superuser
106 * @param string $login name
107 * @param Preferences $preferences Preferences instance
111 public function logAdmin($login, Preferences
$preferences)
113 $this->logged
= true;
114 $this->name
= 'Admin';
115 $this->login
= $login;
117 $this->active
= true;
118 $this->staff
= false;
119 $this->uptodate
= false;
121 $this->lang
= $preferences->pref_lang
;
122 //a flag for super admin only, since it's not a regular user
123 $this->superadmin
= true;
127 * Authenticate from cron
129 * @param string $name Service name
130 * @param Preferences $preferences Preferences instance
134 public function logCron($name, Preferences
$preferences)
136 //known cronable files
137 $ok = array('reminder');
139 if (in_array($name, $ok)) {
140 $this->logged
= true;
142 $this->login
= 'cron';
143 $this->lang
= $preferences->pref_lang
;
145 trigger_error('Not authorized!', E_USER_ERROR
);
150 * Log out user and unset variables
154 public function logOut()
157 $this->logged
= false;
160 $this->admin
= false;
161 $this->active
= false;
162 $this->superadmin
= false;
163 $this->staff
= false;
164 $this->uptodate
= false;
173 public function isLogged()
175 return $this->logged
;
183 public function isAdmin(): bool
185 return (bool)$this->admin
;
189 * Is user super admin?
193 public function isSuperAdmin(): bool
195 return (bool)$this->superadmin
;
203 public function isActive(): bool
205 return (bool)$this->active
;
209 * Is user member of staff?
213 public function isStaff(): bool
215 return (bool)$this->staff
;
223 public function isCron(): bool
225 return (bool)$this->cron
;
229 * Is user a group manager?
230 * If no group id is specified, check if user is manager for at
233 * @param array|int $id_group Group(s) identifier(s)
237 public function isGroupManager($id_group = null): bool
240 if ($this->isAdmin() ||
$this->isStaff()) {
243 if ($id_group === null) {
244 $manager = count($this->managed_groups
) > 0;
246 $groups = (array)$id_group;
248 foreach ($groups as $group) {
249 if (in_array($group, $this->managed_groups
)) {
264 public function getManagedGroups(): array
266 return $this->managed_groups
;
270 * Get compact menu mode
274 public function getCompactMenu(): bool
276 return $this->logged
&& isset($_COOKIE['galette_compact_menu']) && $_COOKIE['galette_compact_menu'];
280 * Is dark mode enabled?
284 public function isDarkModeEnabled(): bool
286 return isset($_COOKIE['galette_dark_mode']) && $_COOKIE['galette_dark_mode'];
290 * Is user currently up to date?
291 * An up-to-date member is active and either due free, or with up-to-date
296 public function isUp2Date(): bool
298 return (bool)$this->uptodate
;
302 * Display logged in member name
304 * @param boolean $only_name If we want only the name without any additional text
308 public function loggedInAs($only_name = false)
310 $n = $this->name
. ' ' . $this->surname
. ' (' . $this->login
. ')';
311 if ($only_name === false) {
315 _T("Logged in as:<br/>%login")
323 * Global getter method
325 * @param string $name name of the property we want to retrieve
329 public function __get($name)
331 $forbidden = array('logged', 'admin', 'active', 'superadmin', 'staff', 'cron', 'uptodate');
332 if (isset($this->$name) && !in_array($name, $forbidden)) {
335 return (int)$this->$name;
345 * Global isset method
346 * Required for twig to access properties via __get
348 * @param string $name name of the property we want to retrieve
352 public function __isset($name)
354 $forbidden = array('logged', 'admin', 'active', 'superadmin', 'staff', 'cron', 'uptodate');
355 if (isset($this->$name) && !in_array($name, $forbidden)) {
364 * get user access level
368 public function getAccessLevel()
371 if ($this->isSuperAdmin()) {
372 return self
::ACCESS_SUPERADMIN
;
373 } elseif ($this->isAdmin()) {
374 return self
::ACCESS_ADMIN
;
375 } elseif ($this->isStaff()) {
376 return self
::ACCESS_STAFF
;
377 } elseif ($this->isGroupManager()) {
378 return self
::ACCESS_MANAGER
;
380 return self
::ACCESS_USER
;