Tweak how User::$Benefits getter

This commit is contained in:
Alex Cabal 2024-11-25 13:56:31 -06:00
parent 84f4b2b0bf
commit e6d4ea1ab6

View file

@ -101,19 +101,24 @@ class User{
protected function GetBenefits(): Benefits{ protected function GetBenefits(): Benefits{
if(!isset($this->_Benefits)){ if(!isset($this->_Benefits)){
$result = Db::Query(' if(isset($this->UserId)){
SELECT * $result = Db::Query('
from Benefits SELECT *
where UserId = ? from Benefits
', [$this->UserId], Benefits::class); where UserId = ?
', [$this->UserId], Benefits::class);
if(sizeof($result) == 0){ if(sizeof($result) == 0){
$this->_Benefits = new Benefits(); $this->_Benefits = new Benefits();
$this->_IsRegistered = false; $this->_IsRegistered = false;
}
else{
$this->_Benefits = $result[0];
$this->_IsRegistered = true;
}
} }
else{ else{
$this->_Benefits = $result[0]; $this->_Benefits = new Benefits();
$this->_IsRegistered = true;
} }
} }
@ -167,12 +172,6 @@ class User{
$this->PasswordHash = null; $this->PasswordHash = null;
} }
if(!isset($this->_Benefits)){
// Set this for validation purposes.
// Skip the accessor because it will attempt to use `$this->UserId` which may not be set yet.
$this->_Benefits = new Benefits();
}
// Some benefits require this `User` to have a password set. // Some benefits require this `User` to have a password set.
if($this->Benefits->RequiresPassword && $this->PasswordHash === null){ if($this->Benefits->RequiresPassword && $this->PasswordHash === null){
$error->Add(new Exceptions\BenefitsRequirePasswordException()); $error->Add(new Exceptions\BenefitsRequirePasswordException());