CoolCMS API

OwnerContextGuardTrait

Server-side context of the owner of the items of an admin component.

Components of the "items of the edited record" kind (replies and followers of an advert, payment items of an advert or post) receive the owner id in a signal parameter and in a hidden form field; both are client-editable. Without this binding, a role allowed to edit through the component of one advert could edit or delete an item of another (a forged id was enough), or add an item to any advert (forged hidden advertId).

The source of truth is a host callback (fn() => $this->editedAdvert?->getId()), read at signal or submit time, when the host's action*() has already loaded the edited entity:

  • a signal with a foreign owner id gives 403 (requireContextOwner()),
  • an item is looked up within the context only; a foreign item is "not found",
  • the hidden owner id in the form is only an echo; if it differs from the context, nothing is saved.

IMPORTANT: fail-closed. Without a context the component saves, deletes and opens nothing. A new host must call setOwnerContext() in createComponent…(), otherwise the component is unusable.

Table of Contents

Properties

$ownerContextProvider  : callable(): mixed|null

Methods

setOwnerContext()  : static
Sets the host callback returning the id of the owner currently being edited (advert or post).
contextOwnerId()  : int|null
Owner id from the host context; null when the host edits nothing (fail-closed).
requireContextOwner()  : int
Signal gate: an owner id from a hand-built URL is usable only when it equals the host context; otherwise 403.
submittedOwnerId()  : int|null
Owner for a form submit: the host context, or null when it is missing or the posted hidden id differs from it (a forged form of another record).

Properties

Methods

setOwnerContext()

Sets the host callback returning the id of the owner currently being edited (advert or post).

public setOwnerContext(callable(): mixed $provider) : static

It is called on every use, so a value from an earlier request never lingers.

Parameters
$provider : callable(): mixed
Return values
static

contextOwnerId()

Owner id from the host context; null when the host edits nothing (fail-closed).

protected contextOwnerId() : int|null
Return values
int|null

requireContextOwner()

Signal gate: an owner id from a hand-built URL is usable only when it equals the host context; otherwise 403.

protected requireContextOwner(int $ownerId) : int
Parameters
$ownerId : int
Return values
int

submittedOwnerId()

Owner for a form submit: the host context, or null when it is missing or the posted hidden id differs from it (a forged form of another record).

protected submittedOwnerId(mixed $postedOwnerId) : int|null
Parameters
$postedOwnerId : mixed
Return values
int|null
On this page

Search results