]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Authentication.php
4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Core
;
24 use Galette\Entity\Group
;
27 * Abstract authentication class for galette
29 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @property ?string $login
32 * @property ?string $name
33 * @property ?string $surname
34 * @property ?integer $id
35 * @property string $lang
36 * @property array<int, Group|int> $managed_groups
39 abstract class Authentication
41 public const ACCESS_PUBLIC
= -1;
42 public const ACCESS_USER
= 0;
43 public const ACCESS_MANAGER
= 1;
44 public const ACCESS_STAFF
= 2;
45 public const ACCESS_ADMIN
= 3;
46 public const ACCESS_SUPERADMIN
= 4;
48 protected string $login;
49 protected string $name;
50 protected ?
string $surname;
51 protected bool $admin = false;
53 protected string $lang;
54 protected bool $logged = false;
55 protected bool $active = false;
56 protected bool $superadmin = false;
57 protected bool $staff = false;
58 protected bool $uptodate = false;
59 /** @var array<int, Group|int> */
60 protected array $managed_groups = [];
61 protected bool $cron = false;
66 * @param string $user user's login
67 * @param string $passe user's password
71 abstract public function logIn(string $user, string $passe): bool;
74 * Does this login already exists ?
75 * These function should be used for setting admin login into Preferences
77 * @param string $user the username
79 * @return boolean true if the username already exists, false otherwise
81 abstract public function loginExists(string $user): bool;
84 * Login for the superuser
86 * @param string $login name
87 * @param Preferences $preferences Preferences instance
91 public function logAdmin(string $login, Preferences
$preferences): bool
94 $this->name
= 'Admin';
95 $this->login
= $login;
99 $this->uptodate
= false;
101 $this->lang
= $preferences->pref_lang
;
102 //a flag for super admin only, since it's not a regular user
103 $this->superadmin
= true;
108 * Authenticate from cron
110 * @param string $name Service name
111 * @param Preferences $preferences Preferences instance
115 abstract public function logCron(string $name, Preferences
$preferences): bool;
118 * Log out user and unset variables
122 public function logOut(): bool
125 $this->logged
= false;
128 $this->admin
= false;
129 $this->active
= false;
130 $this->superadmin
= false;
131 $this->staff
= false;
132 $this->uptodate
= false;
142 public function isLogged(): bool
144 return $this->logged
;
152 public function isAdmin(): bool
158 * Is user super admin?
162 public function isSuperAdmin(): bool
164 return $this->superadmin
;
172 public function isActive(): bool
174 return $this->active
;
178 * Is user member of staff?
182 public function isStaff(): bool
192 public function isCron(): bool
198 * Is user a group manager?
199 * If no group id is specified, check if user is manager for at
202 * @param array<int>|int $id_group Group(s) identifier(s)
206 public function isGroupManager(array|
int $id_group = null): bool
209 if ($this->isAdmin() ||
$this->isStaff()) {
212 if ($id_group === null) {
213 $manager = count($this->managed_groups
) > 0;
215 $groups = is_array($id_group) ?
$id_group : (array)$id_group;
217 foreach ($groups as $group) {
218 if (in_array($group, $this->managed_groups
)) {
231 * @return array<int, Group|int>
233 public function getManagedGroups(): array
235 return $this->managed_groups
;
239 * Get compact menu mode
243 public function getCompactMenu(): bool
245 return $this->logged
&& isset($_COOKIE['galette_compact_menu']) && $_COOKIE['galette_compact_menu'];
249 * Is dark mode enabled?
253 public function isDarkModeEnabled(): bool
255 return isset($_COOKIE['galette_dark_mode']) && $_COOKIE['galette_dark_mode'];
259 * Is user currently up to date?
260 * An up-to-date member is active and either due free, or with up-to-date
265 public function isUp2Date(): bool
267 return $this->uptodate
;
271 * Display logged in member name
273 * @param boolean $only_name If we want only the name without any additional text
277 public function loggedInAs(bool $only_name = false): string
279 $n = $this->name
. ' ' . ($this->surname ??
'') . ' (' . $this->login
. ')';
280 if ($only_name === false) {
284 _T("Logged in as:<br/>%login")
292 * Global getter method
294 * @param string $name name of the property we want to retrieve
298 public function __get(string $name): mixed
300 $forbidden = array('logged', 'admin', 'active', 'superadmin', 'staff', 'cron', 'uptodate');
301 if (in_array($name, $forbidden)) {
302 throw new \
RuntimeException('Property ' . $name . ' is forbidden!');
307 if (isset($this->$name)) {
308 return (int)$this->$name;
313 if (isset($this->$name)) {
318 if (!isset($this->$name)) {
319 throw new \
RuntimeException('Property ' . $name . ' is not set!');
326 * Global isset method
327 * Required for twig to access properties via __get
329 * @param string $name name of the property we want to retrieve
333 public function __isset(string $name): bool
335 $forbidden = array('logged', 'admin', 'active', 'superadmin', 'staff', 'cron', 'uptodate');
336 if (isset($this->$name) && !in_array($name, $forbidden)) {
345 * get user access level
349 public function getAccessLevel(): int
352 if ($this->isSuperAdmin()) {
353 return self
::ACCESS_SUPERADMIN
;
354 } elseif ($this->isAdmin()) {
355 return self
::ACCESS_ADMIN
;
356 } elseif ($this->isStaff()) {
357 return self
::ACCESS_STAFF
;
358 } elseif ($this->isGroupManager()) {
359 return self
::ACCESS_MANAGER
;
360 } elseif ($this->isLogged()) {
361 return self
::ACCESS_USER
;
363 return self
::ACCESS_PUBLIC
;