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

@ -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
*/
@ -293,11 +293,11 @@ class User{
elseif(mb_stripos($identifier, '@') !== false){
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);
}
else{
throw new Exceptions\UserNotFoundException();
return User::GetByName($identifier);
}
}
@ -316,6 +316,21 @@ class User{
', [$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
*/