death-notifier/src/main/php/com/fwdekker/deathnotifier/user/GetPublicUserDataAction.php

55 lines
1.6 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier\user;
use com\fwdekker\deathnotifier\Action;
use com\fwdekker\deathnotifier\ActionException;
/**
* Returns the user's public data.
*/
class GetPublicUserDataAction extends Action
{
/**
* @var UserManager the manager to retrieve the data with
*/
private readonly UserManager $user_manager;
/**
* Constructs a new `GetPublicUserDataAction`.
*
* @param UserManager $user_manager the manager to retrieve the data with
*/
public function __construct(UserManager $user_manager)
{
parent::__construct(require_logged_in: true, require_valid_csrf_token: true);
$this->user_manager = $user_manager;
}
/**
* Returns the user's public data.
*
* @param array<int|string, mixed> $inputs ignored
* @return array{"email": string, "email_verified": bool, "email_notifications_enabled": bool,
* "password_last_change": int} the user's public data
* @throws ActionException if the user's data could not be retrieved
*/
function handle(array $inputs): array
{
$user_data = $this->user_manager->get_user_by_uuid($_SESSION["uuid"]);
if ($user_data === null)
throw new ActionException("Failed to retrieve account data. Refresh the page and try again.");
return [
"email" => $user_data["email"],
"email_verified" => $user_data["email_verification_token"] === null,
"email_notifications_enabled" => $user_data["email_notifications_enabled"] === 1,
"password_last_change" => $user_data["password_last_change"],
];
}
}