123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace app\Types\DatabaseObjects;
- use app\Exceptions\AccountNotFoundException;
- use app\Interfaces\IDatabaseObject;
- use app\Hajeebtok;
- use app\Logger;
- use app\Exceptions\SecurityFaultException;
- class Account implements IDatabaseObject
- {
- public private(set) ?int $id;
- public private(set) ?string $username;
- public private(set) ?string $password;
- public private(set) ?string $picture_hash;
- public private(set) ?bool $verified;
- public private(set) ?string $bio;
- public private(set) ?int $social_credit;
- public function __construct(?int $id = null, ?string $username = null, ?string $password=null, ?string $picture_hash=null, ?bool $verified=null, ?string $bio=null, ?int $social_credit=null)
- {
- $this->id = $id;
- $this->username = $username;
- $this->password = $password ? password_hash($password, PASSWORD_DEFAULT) : null;
- $this->picture_hash = $picture_hash;
- $this->verified = $verified;
- $this->bio = $bio;
- $this->social_credit = $social_credit;
- }
- /**
- * Creates the table for the object type in the database.
- */
- public static function CreateTable(): void
- {
- throw new SecurityFaultException("Attempt to create table on user object.");
- }
- /**
- * Drops the table for the object type from the database.
- */
- public static function DropTable(): void
- {
- throw new SecurityFaultException("Attempt to drop table on user object.");
- }
-
- /**
- * Saves the object to the database.
- */
- public function Save(): int
- {
- Hajeebtok::$Database->Query("INSERT INTO accounts (username, password, picture_hash, verified, bio, social_credit) VALUES (:username, :password, :picture_hash, :verified, :bio, :social_credit)", [
- "username" => $this->username,
- "password" => $this->password,
- "picture_hash" => $this->picture_hash,
- "verified" => $this->verified,
- "bio" => $this->bio,
- "social_credit" => $this->social_credit
- ]);
- $id = Hajeebtok::$Database->LastInsertId();
- Logger::Debug("Saved account id ($id).");
- return $id;
- }
- /**
- * Deletes the object from the database.
- */
- public function Delete()
- {
- Hajeebtok::$Database->Query("DELETE FROM accounts WHERE id = :id", ["id" => $this->id]);
- }
- /**
- * Loads the object from the database.
- */
- public function Load()
- {
- if (empty($this->id)) { // search by username
- $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE username = :username", ["username" => $this->username]);
- } else {
- // search by id
- $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE id = :id", ["id" => $this->id]);
- }
- if (empty($data)) throw new AccountNotFoundException($this->id, 404);
- $this->id = $data["id"];
- $this->username = $data["username"];
- $this->password = $data["password"];
- $this->picture_hash = $data["picture_hash"];
- $this->verified = $data["verified"] === 1;
- $this->bio = $data["bio"];
- $this->social_credit = $data["social_credit"];
- }
- public function Update() {
- Hajeebtok::$Database->Query("UPDATE accounts SET username = :username, password = :password, picture_hash = :picture_hash, verified = :verified, bio = :bio, social_credit = :social_credit WHERE id = :id", [
- "id" => $this->id,
- "username" => $this->username,
- "password" => $this->password,
- "picture_hash" => $this->picture_hash,
- "verified" => $this->verified,
- "bio" => $this->bio,
- "social_credit" => $this->social_credit
- ]);
- Logger::Debug("Updated account id ($this->id).");;
- }
-
- public function LoadMany(): array {
- if(empty($this->username)) throw new AccountNotFoundException(0, 404);
- $data = Hajeebtok::$Database->Query("SELECT * FROM accounts WHERE username LIKE :username", ["username" => "%$this->username%"]);
- if(empty($data)) throw new AccountNotFoundException(0, 404);
- return $data;
- }
- public function Exists(): bool {
- if ($this->id === null) { // search by username
- $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE username = :username", ["username" => $this->username]);
- } else { // search by id
- $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE id = :id", ["id" => $this->id]);
- }
- return !empty($data);
- }
- }
|