Add cancel snooze

- Add common request validation function
This commit is contained in:
2024-06-23 06:58:47 -04:00
parent d6e8cf66cc
commit 9421bb2035
11 changed files with 96 additions and 83 deletions

View File

@@ -1,8 +1,9 @@
<?php declare(strict_types=1);
use BitBadger\PDODocument\{Configuration, Definition, Mode};
use Auth0\SDK\Exception\ConfigurationException;
use BitBadger\PDODocument\{Configuration, Definition, DocumentException, Mode};
use Dotenv\Dotenv;
use MyPrayerJournal\{Auth, Layout, Table};
use MyPrayerJournal\{Auth, Request, Table};
require __DIR__ . '/vendor/autoload.php';
@@ -25,6 +26,7 @@ if (php_sapi_name() != 'cli') {
Configuration::$pdoDSN = 'sqlite:' . implode(DIRECTORY_SEPARATOR, [__DIR__, 'data', 'mpj.db']);
Configuration::$mode = Mode::SQLite;
Definition::ensureTable(Table::REQUEST);
Definition::ensureFieldIndex(Table::REQUEST, 'user', ['userId']);
$_PATCH = [];
if ($_SERVER['REQUEST_METHOD'] ?? '' == 'PATCH') parse_str(file_get_contents('php://input'), $_PATCH);
@@ -59,3 +61,25 @@ function hide_modal(string $name): void
{
header("X-Hide-Modal: $name");
}
/**
* Validate the user, HTTP method, and request
*
* @param string $id The ID of the prayer request to retrieve
* @param array $methods The allowable HTTP methods
* @param bool $redirect Whether to redirect not-logged-on users (optional, defaults to true)
* @return Request The request (failures will not return)
* @throws ConfigurationException If any is encountered
* @throws DocumentException If any is encountered
*/
function validate_request(string $id, array $methods, bool $redirect = true): Request
{
if (sizeof(array_filter($methods, fn($it) => $_SERVER['REQUEST_METHOD'] == $it)) == 0) not_found();
Auth::requireUser($redirect);
$req = Request::byId($id);
if (!$req) not_found();
return $req;
}