Debian Codegraph @

classes/Users/User.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2023 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains the class User
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg\Users;
33
34 use Liuch\DmarcSrg\Core;
35 use Liuch\DmarcSrg\Exception\SoftException;
36
37 /**
38 * It's an abstract class for accessing to stored user data
39 */
40 abstract class User
41 {
42 public const LEVEL_ADMIN = 99;
43 public const LEVEL_MANAGER = 50;
44 public const LEVEL_USER = 10;
45
46 /**
47 * Returns true if the user exists in the database or false otherwise
48 *
49 * @return bool Whether the user exists
50 */
51 public function exists(): bool
52 {
53 return true;
54 }
55
56 /**
57 * Returns the user id
58 *
59 * @return int
60 */
61 abstract public function id(): int;
62
63 /**
64 * Returns the user name
65 *
66 * @return string
67 */
68 abstract public function name(): string;
69
70 /**
71 * Returns the user access level
72 *
73 * @return int
74 */
75 abstract public function level(): int;
76
77 /**
78 * Checks if the user is enabled
79 *
80 * @return bool
81 */
82 abstract public function isEnabled(): bool;
83
84 /**
85 * Returns the sequence number of the session
86 *
87 * It changes when the user credentials or state are changed.
88 *
89 * @return int
90 */
91 public function session(): int
92 {
93 return 1;
94 }
95
96 /**
97 * Returns the user's data as an array
98 *
99 * @return array
100 */
101 abstract public function toArray(): array;
102
103 /**
104 * Converts the integer access level value to string
105 *
106 * @param int $level
107 *
108 * @return string
109 */
110 public static function levelToString(int $level): string
111 {
112 if ($level >= self::LEVEL_ADMIN) {
113 return 'admin';
114 }
115 if ($level >= self::LEVEL_MANAGER) {
116 return 'manager';
117 }
118 return 'user';
119 }
120
121 /**
122 * Converts the string acess level to integer value
123 *
124 * @param string $name Access level name
125 *
126 * @return int
127 */
128 public static function stringToLevel(string $name): int
129 {
130 switch ($name) {
131 case 'admin':
132 return self::LEVEL_ADMIN;
133 case 'manager':
134 return self::LEVEL_MANAGER;
135 case 'user':
136 return self::LEVEL_USER;
137 }
138 throw new SoftException('Wrong access level name');
139 }
140
141 /**
142 * Verifies the passed password with the hash stored in the database
143 *
144 * @param string $password Password to validate
145 *
146 * @return bool
147 */
148 abstract public function verifyPassword(string $password): bool;
149 }