-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAuth.php
More file actions
202 lines (185 loc) · 6.41 KB
/
Copy pathAuth.php
File metadata and controls
202 lines (185 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace Authentication;
use Exceptions\HttpExceptions\LoginFailedException;
use Exceptions\HttpExceptions\NotLoggedInException;
use Exceptions\HttpExceptions\UnauthorizedException;
use Exceptions\HttpExceptions\UserRegistrationException;
use Models\User;
use Database\TableManagers\UserTableManager;
use Exceptions\HttpExceptions\BadRequestException;
use Exceptions\HttpExceptions\NotFoundException;
class Auth
{
private static $activeUser;
/**
* Logs in a user.
*
* This function attempts to log in a user using the provided username and password. If the user is found in the database and the password is correct, the user is logged in and returned. If not, a LoginFailedException is thrown.
*
* @param string $username The username of the user.
* @param string $password The password of the user.
* @return User The logged-in user.
* @throws LoginFailedException If the password is incorrect or the user is not found.
*/
public static function login(string $username, string $password, $requirePassword = true): User
{
$user = UserTableManager::getUserByUsername($username);
if (empty($user)) {
throw new LoginFailedException("User not found");
}
if ($requirePassword && !password_verify($password, $user->getPassword())) {
throw new LoginFailedException("Incorrect password");
}
if (!$user->getIsVerified()) {
throw new LoginFailedException("User not verified", 403);
}
self::setSessionUser($user);
return $user;
}
/**
* sets the user id of the active user in the session
* @param string $userId The id of the user.
*@return void
*/
private static function setSessionUserId($userId): void
{
$_SESSION['user_id'] = $userId;
self::$activeUser = null;
}
/**
* sets the active user in the session
* @param User $user The user.
* @return void
*/
private static function setSessionUser($user): void
{
$_SESSION['user_id'] = $user->getId();
self::$activeUser = $user;
}
/**
* Logs out the current user.
*
* This function destroys the session and terminates the script execution.
*/
public static function logout()
{
session_destroy();
exit();
}
/**
* Registers a new user.
*
* This function checks if the provided username and email are unique. If either is already in use, a UserRegistrationException is thrown.
* If both are unique, the password is hashed and the new user is added to the database. If the user is successfully added, the function returns true.
* If the user could not be added, a UserRegistrationException is thrown.
*
* @param string $username The desired username of the new user.
* @param string $password The desired password of the new user.
* @param string $email The desired email of the new user.
* @return bool True if the user was successfully registered.
* @throws UserRegistrationException If the username or email is already in use, or if the user could not be added to the database.
*/
public static function register(string $username, string $password, string $email): bool
{
if (UserTableManager::verifyExistenceByUserName($username)) {
throw new UserRegistrationException("Username exists");
} else if (UserTableManager::verifyExistenceByEmail($email)) {
throw new UserRegistrationException("Email exists");
}
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
if (!empty(UserTableManager::addUser($username, $email, $hashedPassword))) {
return true;
} else {
throw new UserRegistrationException("Failed to register user");
}
}
/**
* Modify the password of a user
* @param $userId
* @param $password
* @return bool
*/
public static function modifyPassword($userId, $password): bool
{
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
UserTableManager::updatePassword($userId, $hashedPassword);
self::forceReloadActiveUser();
return true;
}
/**
* this function is used to check if a user is logged in
* @return bool
*
*/
public static function isLoggedIn(): bool
{
return isset($_SESSION['user_id']);
}
/**
* this function is used to check if a specific user is logged in
* @param int $userId The id of the user.
* @return bool
*/
public static function isSpecificUserLoggedIn($userId): bool
{
return isset($_SESSION['user_id']) && $_SESSION['user_id'] == $userId;
}
/**
* this function throw an exception if there is no user logged in
* @throws NotLoggedInException
*/
public static function requireLogin()
{
if (!self::isLoggedIn()) {
throw new NotLoggedInException();
}
}
/**
* this function throw an exception if the user logged in is not the admin
* @throws UnauthorizedException
*/
public static function requireAdminAccess()
{
if (!self::isLoggedIn() || self::getActiveUser()->getRole() != 'admin') {
throw new UnauthorizedException();
}
}
/**
* this function is used to force the active user to be reloaded from the database on the next call to getActiveUser()
* this is useful when the user's data has been modified in the database and the active user's data needs to be updated
* @return void
*/
private static function forceReloadActiveUser()
{
self::$activeUser = null;
}
/**
* Get the active user
*
* @return User
* @throws NotLoggedInException
*/
public static function getActiveUser(): User
{
if (self::$activeUser !== null) {
return self::$activeUser;
}
if (!self::isLoggedIn()) {
throw new NotLoggedInException();
}
$userId = $_SESSION['user_id'];
self::$activeUser = UserTableManager::getUserById($userId);
if (self::$activeUser === null) {
throw new NotFoundException("User not found");
}
return self::$activeUser;
}
/**
* Get the active user's ID
* @return int
*/
public static function getActiveUserId()
{
return $_SESSION['user_id'];
}
}