]>
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;
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
133 public function logCron($name)
135 //known cronable files
136 $ok = array('reminder');
138 if (in_array($name, $ok)) {
139 $this->logged
= true;
141 $this->login
= 'cron';
143 trigger_error('Not authorized!', E_USER_ERROR
);
148 * Log out user and unset variables
152 public function logOut()
155 $this->logged
= false;
158 $this->admin
= false;
159 $this->active
= false;
160 $this->superadmin
= false;
161 $this->staff
= false;
162 $this->uptodate
= false;
170 public function isLogged()
172 return $this->logged
;
180 public function isAdmin(): bool
182 return (bool)$this->admin
;
186 * Is user super admin?
190 public function isSuperAdmin(): bool
192 return (bool)$this->superadmin
;
200 public function isActive(): bool
202 return (bool)$this->active
;
206 * Is user member of staff?
210 public function isStaff(): bool
212 return (bool)$this->staff
;
220 public function isCron(): bool
222 return (bool)$this->cron
;
226 * Is user a group manager?
227 * If no group id is specified, check if user is manager for at
230 * @param array|int $id_group Group(s) identifier(s)
234 public function isGroupManager($id_group = null): bool
237 if ($this->isAdmin() ||
$this->isStaff()) {
240 if ($id_group === null) {
241 $manager = count($this->managed_groups
) > 0;
243 $groups = (array)$id_group;
245 foreach ($groups as $group) {
246 if (in_array($group, $this->managed_groups
)) {
261 public function getManagedGroups(): array
263 return $this->managed_groups
;
267 * Get compact menu mode
271 public function getCompactMenu(): bool
273 return $this->logged
&& isset($_COOKIE['galette_compact_menu']) && $_COOKIE['galette_compact_menu'];
277 * Is dark mode enabled?
281 public function isDarkModeEnabled(): bool
283 return isset($_COOKIE['galette_dark_mode']) && $_COOKIE['galette_dark_mode'];
287 * Is user currently up to date?
288 * An up-to-date member is active and either due free, or with up-to-date
293 public function isUp2Date(): bool
295 return (bool)$this->uptodate
;
299 * Display logged in member name
301 * @param boolean $only_name If we want only the name without any additional text
305 public function loggedInAs($only_name = false)
307 $n = $this->name
. ' ' . $this->surname
. ' (' . $this->login
. ')';
308 if ($only_name === false) {
312 _T("Logged in as:<br/>%login")
320 * Global getter method
322 * @param string $name name of the property we want to retrieve
326 public function __get($name)
328 $forbidden = array('logged', 'admin', 'active', 'superadmin', 'staff', 'cron', 'uptodate');
329 if (isset($this->$name) && !in_array($name, $forbidden)) {
332 return (int)$this->$name;
342 * Global isset method
343 * Required for twig to access properties via __get
345 * @param string $name name of the property we want to retrieve
349 public function __isset($name)
351 $forbidden = array('logged', 'admin', 'active', 'superadmin', 'staff', 'cron', 'uptodate');
352 if (isset($this->$name) && !in_array($name, $forbidden)) {
361 * get user access level
365 public function getAccessLevel()
368 if ($this->isSuperAdmin()) {
369 return self
::ACCESS_SUPERADMIN
;
370 } elseif ($this->isAdmin()) {
371 return self
::ACCESS_ADMIN
;
372 } elseif ($this->isStaff()) {
373 return self
::ACCESS_STAFF
;
374 } elseif ($this->isGroupManager()) {
375 return self
::ACCESS_MANAGER
;
377 return self
::ACCESS_USER
;