aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model
diff options
context:
space:
mode:
Diffstat (limited to 'Digitigrade/Model')
-rw-r--r--Digitigrade/Model/UserAccount.php13
1 files changed, 13 insertions, 0 deletions
diff --git a/Digitigrade/Model/UserAccount.php b/Digitigrade/Model/UserAccount.php
index 3d0e2a4..72a9ce9 100644
--- a/Digitigrade/Model/UserAccount.php
+++ b/Digitigrade/Model/UserAccount.php
@@ -1,6 +1,7 @@
<?php
namespace Digitigrade\Model;
+use Digitigrade\HttpResponseStatus\Unauthorized;
use Digitigrade\Model;
class UserAccount extends Model {
@@ -46,6 +47,18 @@ class UserAccount extends Model {
return Session::findByToken($token)?->userAccount;
}
+ /**
+ * Gets the currently logged-in user account, or throws an error
+ * @throws Unauthorized if no account could be found
+ */
+ public static function requireByCurrentSession(): self {
+ $user = self::findByCurrentSession();
+ if ($user == null) {
+ throw new Unauthorized();
+ }
+ return $user;
+ }
+
public function verifyPassword(#[\SensitiveParameter] $attemptedPassword): bool {
return password_verify($attemptedPassword, $this->passwordHash);
}