New custom-in-repo plugin entry-actions (un-ignored in .gitignore, NOT in
plugins.txt) registers DELETE /api/v1/entry/{slug} via onApiRegisterRoutes
(KTD5). The handler requires the authenticated site OWNER (not any login/admin),
rejects unsafe slugs (400), resolves the target through the page tree, asserts it
is a direct child of the active trip's dailies container, deletes the folder and
clears the cache — sharing EntryScopeGuard with the save path so R6 can't diverge.
A lazy per-namespace autoloader loads the controller on cached-route requests
(the router dispatches from route.cache without re-firing onApiRegisterRoutes).
EntryScopeGuard gains isOwnerUser() (API user comes from the request, not
$grav['user']) and enablePages() before find() (pages are lazily disabled in the
API context).
feed-actions.js (new, built via make build-assets; loaded on the trip/home feed
only when owner_can_edit) wires the inline Delete → Cancel/Confirm swap: on
Confirm it locks both buttons (D2, no double-DELETE), fetches the route
(credentials:include), removes the card, moves focus to the next card, and
announces via a page-level aria-live region (D4); on failure it restores the
control with an inline message (D7). Adds .sr-only + .entry-action[hidden] CSS.
Verified on the 2.0.4 container — API matrix 8/8 (anon 401, non-owner 403, bad
slug 400, out-of-scope 404, owner 204 + folder removed; V3/V5) and the delete UI
in a headless browser (confirm swap, card removal, disk deletion, live announce).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01H1FrCYNq6RXdGYbn5PFrhM
131 lines
4.7 KiB
PHP
131 lines
4.7 KiB
PHP
<?php
|
|
namespace Grav\Plugin\Shared;
|
|
|
|
use Grav\Common\Grav;
|
|
use Grav\Common\Page\Interfaces\PageInterface;
|
|
|
|
/**
|
|
* Single source of truth for the R6 server-side scope guard, shared by BOTH
|
|
* enforcement points so they cannot diverge (KTD5):
|
|
* - the save/publish path (cache-on-save onFormValidationProcessed), and
|
|
* - the delete path (entry-actions API route).
|
|
*
|
|
* Two independent checks, both required:
|
|
* 1. isOwner() — the acting user is the configured site owner, NOT merely any
|
|
* authenticated account (the super-admin `tester` also authenticates).
|
|
* 2. resolveActiveDailyChild() — the target resolves, through the page tree,
|
|
* to a real DIRECT child of the active trip's `dailies` container. Resolving
|
|
* via $pages->find() + a parent-route assertion (never raw path concatenation)
|
|
* closes the traversal hole where a string like `/…/dailies/../other/entry.md`
|
|
* prefix-matches the active dailies but points elsewhere.
|
|
*/
|
|
class EntryScopeGuard
|
|
{
|
|
/**
|
|
* Active trip's dailies container route ("/trips/<slug>/dailies"), or null
|
|
* when site.active_trip is unset. Accepts a full route or a bare slug.
|
|
*/
|
|
public static function dailiesRoute(Grav $grav): ?string
|
|
{
|
|
$active = $grav['config']->get('site.active_trip');
|
|
$active = is_string($active) ? trim($active) : '';
|
|
if ($active === '') {
|
|
return null;
|
|
}
|
|
$trip = trim($active, '/');
|
|
if (strpos($trip, 'trips/') !== 0) {
|
|
$trip = 'trips/' . $trip;
|
|
}
|
|
return '/' . $trip . '/dailies';
|
|
}
|
|
|
|
/**
|
|
* True only when the current user is authenticated AND their username equals
|
|
* site.owner_username. Gating on authentication alone would grant rights to
|
|
* every account, including the super-admin `tester` (KTD8).
|
|
*/
|
|
public static function isOwner(Grav $grav): bool
|
|
{
|
|
$user = $grav['user'] ?? null;
|
|
if (!$user || empty($user->authenticated)) {
|
|
return false;
|
|
}
|
|
return self::isOwnerUser($grav, $user);
|
|
}
|
|
|
|
/**
|
|
* Owner check for an explicit user object — used by the API delete route,
|
|
* whose authenticated user comes from the request (api_user attribute), not
|
|
* $grav['user']. Same rule: username must equal site.owner_username.
|
|
*/
|
|
public static function isOwnerUser(Grav $grav, $user): bool
|
|
{
|
|
if (!$user || !isset($user->username)) {
|
|
return false;
|
|
}
|
|
$owner = $grav['config']->get('site.owner_username');
|
|
return is_string($owner) && $owner !== '' && $user->username === $owner;
|
|
}
|
|
|
|
/**
|
|
* A safe single path segment: non-empty, no separators, no dot-traversal.
|
|
*/
|
|
public static function isSafeSegment(string $segment): bool
|
|
{
|
|
if ($segment === '' || $segment === '.' || $segment === '..') {
|
|
return false;
|
|
}
|
|
if (strpbrk($segment, '/\\') !== false) {
|
|
return false;
|
|
}
|
|
return strpos($segment, '..') === false;
|
|
}
|
|
|
|
/**
|
|
* The folder segment carried by a hidden edit_path value. post-form.js sets
|
|
* edit_path to "<entry-route>/entry.md", so basename(dirname()) is the entry's
|
|
* own folder name (its route's last segment) — the same value stock
|
|
* add-page-by-form derives for the in-place write.
|
|
*/
|
|
public static function segmentFromEditPath(string $editPath): string
|
|
{
|
|
$editPath = trim($editPath);
|
|
if ($editPath === '') {
|
|
return '';
|
|
}
|
|
return basename(dirname($editPath));
|
|
}
|
|
|
|
/**
|
|
* Resolve a folder segment to the page that is a DIRECT child of the active
|
|
* trip's dailies container, or null when the segment is unsafe, no active trip
|
|
* is set, the page does not exist, or its parent is not the active dailies.
|
|
*/
|
|
public static function resolveActiveDailyChild(Grav $grav, string $segment): ?PageInterface
|
|
{
|
|
if (!self::isSafeSegment($segment)) {
|
|
return null;
|
|
}
|
|
$dailies = self::dailiesRoute($grav);
|
|
if ($dailies === null) {
|
|
return null;
|
|
}
|
|
$pages = $grav['pages'];
|
|
// In the API request context the page tree is lazily disabled; enable it
|
|
// so find() can resolve (mirrors the api plugin's own resolvePageByRoute).
|
|
// Idempotent — a no-op in the frontend save-path context.
|
|
if (method_exists($pages, 'enablePages')) {
|
|
$pages->enablePages();
|
|
}
|
|
$page = $pages->find($dailies . '/' . $segment);
|
|
if ($page === null) {
|
|
return null;
|
|
}
|
|
$parent = $page->parent();
|
|
if ($parent === null || $parent->route() !== $dailies) {
|
|
return null;
|
|
}
|
|
return $page;
|
|
}
|
|
}
|