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(); } ?> 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(); } }