Allow getting users by name

This commit is contained in:
Alex Cabal 2024-12-02 10:20:36 -06:00
parent 75ef304824
commit ab17e6af5d
2 changed files with 19 additions and 4 deletions

View file

@ -1,6 +1,6 @@
RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^post$/" RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^post$/"
RewriteRule ^/users/([\d]+)$ /users/post.php?user-id=$1 [L] RewriteRule ^/users/([\d]+)$ /users/post.php?user-id=$1 [L]
RewriteRule ^/users/([^/]+)$ /users/get.php?user-identifier=$1 [L] RewriteRule ^/users/([^/]+)$ /users/get.php?user-identifier=$1 [B,L]
RewriteRule ^/users/([\d]+)/edit$ /users/edit.php?user-id=$1 [L] RewriteRule ^/users/([\d]+)/edit$ /users/edit.php?user-id=$1 [L]

View file

@ -278,7 +278,7 @@ class User{
} }
/** /**
* Get a `User` based on either a `UserId`, `Email`, or `Uuid`. * Get a `User` based on either a `UserId`, `Email`, or `Uuid`, or `Name`.
* *
* @throws Exceptions\UserNotFoundException * @throws Exceptions\UserNotFoundException
*/ */
@ -293,11 +293,11 @@ class User{
elseif(mb_stripos($identifier, '@') !== false){ elseif(mb_stripos($identifier, '@') !== false){
return User::GetByEmail($identifier); return User::GetByEmail($identifier);
} }
elseif(mb_stripos($identifier, '-') !== false){ elseif(preg_match('/^[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}$/', $identifier)){
return User::GetByUuid($identifier); return User::GetByUuid($identifier);
} }
else{ else{
throw new Exceptions\UserNotFoundException(); return User::GetByName($identifier);
} }
} }
@ -316,6 +316,21 @@ class User{
', [$email], User::class)[0] ?? throw new Exceptions\UserNotFoundException(); ', [$email], User::class)[0] ?? throw new Exceptions\UserNotFoundException();
} }
/**
* @throws Exceptions\UserNotFoundException
*/
public static function GetByName(?string $name): User{
if($name === null){
throw new Exceptions\UserNotFoundException();
}
return Db::Query('
SELECT *
from Users
where Name = ?
', [$name], User::class)[0] ?? throw new Exceptions\UserNotFoundException();
}
/** /**
* @throws Exceptions\UserNotFoundException * @throws Exceptions\UserNotFoundException
*/ */