aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model/UserAccount.php
diff options
context:
space:
mode:
Diffstat (limited to 'Digitigrade/Model/UserAccount.php')
-rw-r--r--Digitigrade/Model/UserAccount.php52
1 files changed, 52 insertions, 0 deletions
diff --git a/Digitigrade/Model/UserAccount.php b/Digitigrade/Model/UserAccount.php
new file mode 100644
index 0000000..3d0e2a4
--- /dev/null
+++ b/Digitigrade/Model/UserAccount.php
@@ -0,0 +1,52 @@
+<?php
+namespace Digitigrade\Model;
+
+use Digitigrade\Model;
+
+class UserAccount extends Model {
+ public ?int $id;
+ public string $email;
+ public string $passwordHash;
+ public ?Actor $actor;
+ public bool $banned = false;
+
+ public static function create(string $email, #[\SensitiveParameter] string $password): self {
+ $user = new self();
+ $user->email = $email;
+ $user->passwordHash = defaults_password_hash($password);
+ $user->actor = null;
+ $user->save();
+ return $user;
+ }
+
+ public function createLinkedActor(string $handle) {
+ $this->actor = Actor::create($handle);
+ $this->save();
+ }
+
+ protected function getUpdateWhereClause(\PDO $db): ?string {
+ if (isset($this->id) && self::find($this->{'id'}) != null) {
+ return 'id = ' . $this->{'id'};
+ }
+ if (self::findByEmail($this->email) != null) {
+ return 'email = ' . $db->quote($this->email);
+ }
+ return null;
+ }
+
+ public static function findByEmail(string $email): ?self {
+ return self::findWhere('email = ?', [$email]);
+ }
+
+ public static function findByCurrentSession(): ?self {
+ if (!isset($_COOKIE['digitigrade-session'])) {
+ return null;
+ }
+ $token = $_COOKIE['digitigrade-session'];
+ return Session::findByToken($token)?->userAccount;
+ }
+
+ public function verifyPassword(#[\SensitiveParameter] $attemptedPassword): bool {
+ return password_verify($attemptedPassword, $this->passwordHash);
+ }
+} \ No newline at end of file