]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Features/Permissions.php
9580a3442179640801c3437f8d9866bfd42cf115
[galette.git] / galette / lib / Galette / Features / Permissions.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 namespace Galette\Features;
23
24 use Galette\Entity\FieldsConfig;
25 use Throwable;
26 use Analog\Analog;
27 use Galette\Core\L10n;
28 use Laminas\Db\Sql\Expression;
29
30 /**
31 * Permissions
32 *
33 * @author Johan Cwiklinski <johan@x-tnd.be>
34 */
35
36 trait Permissions
37 {
38 protected ?int $permission = null;
39
40 /* FIXME/ requires PHP 8.2
41 public const NOBODY = 0;
42 public const USER_WRITE = 1;
43 public const ADMIN = 2;
44 public const STAFF = 3;
45 public const MANAGER = 4;
46 public const USER_READ = 5;
47 public const ALL = 10;*/
48
49 /**
50 * Get permissions list
51 *
52 * @param bool $can_public Can have "public" permission
53 *
54 * @return array<int, string>
55 */
56 public static function getPermissionsList(bool $can_public = false): array
57 {
58 $list = [
59 FieldsConfig::NOBODY => _T("Inaccessible"),
60 FieldsConfig::USER_READ => _T("Read only"),
61 FieldsConfig::USER_WRITE => _T("Read/Write"),
62 FieldsConfig::MANAGER => _T("Group manager"),
63 FieldsConfig::STAFF => _T("Staff member"),
64 FieldsConfig::ADMIN => _T("Administrator"),
65 ];
66
67 if ($can_public) {
68 $all = [FieldsConfig::ALL => _T("Public")];
69 $list = array_unshift($all);
70 }
71
72 return $list;
73 }
74
75 /**
76 * Get permission name
77 *
78 * @return string
79 */
80 public function getPermissionName(): string
81 {
82 $perms = self::getPermissionsList();
83 return $perms[$this->getPermission()];
84 }
85
86 /**
87 * Get current permissions
88 *
89 * @return integer|null
90 */
91 public function getPermission(): ?int
92 {
93 return $this->permission;
94 }
95 }