| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace App;
- use App\Interfaces\OptionInterface;
- use Carbon\CarbonImmutable;
- use DateTimeInterface;
- use Exception;
- use RuntimeException;
- /**
- * Class User
- * @property int $id
- * @property int $role_id
- * @property string $title
- * @property string $first_name
- * @property string $last_name
- * @property string $email
- * @property string $password
- * @property bool $admin
- * @property string $csrf_token
- * @property string $dts_last_seen
- * @property string $dts_created
- * @property string $dts_updated
- * @property string $dts_deleted
- */
- class User extends Model implements OptionInterface {
- protected static $table = "user";
- protected static $idField = "id";
- protected static $fillableFields = [
- "role_id",
- "title", "first_name", "last_name", "email", "password",
- "csrf_token",
- "admin",
- "dts_last_seen",
- "dts_created",
- "dts_updated",
- "dts_deleted",
- ];
- // Static methods
- public static function getTitleOptions(): array {
- return ["Mr", "Mrs", "Ms"];
- }
- public static function getStaffOptions(): array {
- $staff_users = self::query(["admin" => 1]);
- $staff_options = [];
- foreach($staff_users as $staff_user) {
- $staff_options[$staff_user->id] = $staff_user->getDisplayName();
- }
- return $staff_options;
- }
- /**
- * @return array
- * @throws Exception
- */
- public static function getOptions(): array {
- $options = [];
- foreach(self::getAll() as $user) {
- $options[$user->id] = $user->getDisplayName();
- }
- uasort($options, static function(string $a, string $b) {
- return strnatcasecmp($a, $b);
- });
- return $options;
- }
- public static function requireStaff(?int $user_id): User {
- $user = self::get($user_id);
- if(!$user || !$user->isAdmin()) {
- throw new RuntimeException(__("Invalid staff."));
- }
- return $user;
- }
- // Object methods
- /**
- * @return Role
- * @throws Exception
- */
- public function getRole(): Role {
- return Role::get($this->role_id);
- }
- /**
- * @param string|null $password
- * @return bool
- * @throws Exception
- */
- public function validatePassword(?string $password): bool {
- return password_verify($password, $this->password);
- }
- /**
- * @return void
- * @throws Exception
- */
- public function refreshCsrfToken(): void {
- $this->csrf_token = bin2hex(random_bytes(16));
- $this->commit();
- }
- /**
- * @return void
- * @throws Exception
- */
- public function insertCsrfToken(): void {
- if(!$this->csrf_token) {
- $this->refreshCsrfToken();
- }
- ?>
- <input type="hidden" name="csrf_token" value="<?=htmlspecialchars($this->csrf_token)?>" />
- <?php
- }
- public function validateCsrfToken(): bool {
- $request = Request::fromRequest();
- return $request->raw("csrf_token") === $this->csrf_token;
- }
- public function requireValidCsrfToken(): void {
- if(!$this->validateCsrfToken()) {
- throw new RuntimeException(__("CSRF validation failed."));
- }
- }
- public function getDateTimeCreated(): CarbonImmutable {
- return CarbonImmutable::parse($this->dts_created, "UTC");
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function touch(): bool {
- $this->dts_last_seen = Database::dts();
- return $this->commit();
- }
- public function isAdmin(): bool {
- return (bool) $this->admin;
- }
- public function getUrl(): string {
- return sprintf("/users/%d", $this->id);
- }
- public function requireAdmin(): void {
- if(!$this->isAdmin()) {
- throw new RuntimeException(__("You do not have permission to access this page."));
- }
- }
- public function getDisplayName(): string {
- return $this->first_name . " " . $this->last_name;
- }
- public function getAvatarUrl(): string {
- return "https://www.gravatar.com/avatar/" . md5($this->email) . "?d=mp";
- }
- public function formatDateTime(?DateTimeInterface $dt, string $format = "d/m/Y H:i"): string {
- if(!$dt) {
- return __("Never");
- }
- return CarbonImmutable::instance($dt)->setTimezone("Europe/London")->format($format);
- }
- public function formatDate(?DateTimeInterface $dt): string {
- return $this->formatDateTime($dt, "d/m/Y");
- }
- public function getTimezone(): string {
- return "Europe/London";
- }
- public function getOptionValue(): string {
- return $this->getIdString();
- }
- public function getOptionLabel(): string {
- return $this->getDisplayName();
- }
- }
|