Skip to content

refactor(hydra): extract the operation grant decision into OperationGrantChecker - #25

Merged
schmunk42 merged 3 commits into
masterfrom
feature/utils-grant-checker
Nov 30, 2026
Merged

schmunk42 merged 3 commits into
masterfrom
feature/utils-grant-checker

Conversation

@schmunk42

Copy link
Copy Markdown
Member

Summary

Refactoring only, no behaviour change. Release as patch 0.8.2.

The grant decision of the runtime hydra:operation subscriber ("is filtering active" and "may the current token execute this operation") moves from private methods of AddHydraOperationsSubscriber into the shared, privately registered service Dmstr\ApiPlatformUtils\Service\OperationGrantChecker (isActive(), isGranted(Operation $operation, ?object $subject = null)). The subscriber delegates to it. This prepares the discovery filter of #21, which needs the same decision for the OpenAPI document and the Hydra documentation; #21 stays open.

What changes

  • New OperationGrantChecker: the logic is moved literally, including the order of the checks (inactive, no security, no resource class, access checker) and the fail-closed rule (any \Throwable while evaluating the expression denies).
  • AddHydraOperationsSubscriber keeps its four constructor arguments (type, position, default, name) and gains an optional fifth ?OperationGrantChecker $grantChecker. Without it the subscriber builds a checker from arguments three and four; with it the given checker wins.
  • config/services.yaml: the optional reference @?api_platform.security.resource_access_checker and the parameter dmstr_api_platform_utils.hydra_operations.filter_operations_by_security move from the subscriber definition to the checker definition; the subscriber gets a reference to the checker.

Unchanged: service IDs, configuration keys, parameters, the Vary: Authorization header and the emitted hydra:operation entries. The checker stays coupled to hydra_operations.filter_operations_by_security.

Tests

  • tests/EventSubscriber/AddHydraOperationsSubscriberTest.php is unchanged and green, which is the proof that behaviour is preserved.
  • New tests/Service/OperationGrantCheckerTest.php covers the checker directly: both inactive cases, operations without security or resource class (no call to the access checker), the call arguments including the object key for a null subject, and denial on \Exception and \Error.
  • ApiPlatformUtilsExtensionTest pins the wiring, in particular the optional reference on the checker definition: a typo there would silently disable filtering.

🤖 Generated with Claude Code

schmunk42 and others added 3 commits November 30, 2026 16:48
…rantChecker [*]

Move isFilteringActive() and isOperationGranted() from
AddHydraOperationsSubscriber into a shared, privately registered service.
The subscriber delegates to it; its first four constructor arguments keep
type, position, default and name, the checker is an optional fifth one.
No behaviour change.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Adds a direct unit test of the grant decision: inactive (switch off or no
access checker) and operations without security expression or resource
class are granted without calling the access checker; otherwise the
checker is called once with class, expression and ['object' => $subject],
with the key present for a null subject; any \Throwable denies.

The extension test pins the wiring: the optional reference to
api_platform.security.resource_access_checker on the checker definition
and the subscriber's reference to the shared checker.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@schmunk42 schmunk42 self-assigned this Nov 30, 2026
@schmunk42
schmunk42 merged commit 7f07958 into master Nov 30, 2026
2 checks passed
@schmunk42
schmunk42 deleted the feature/utils-grant-checker branch November 30, 2026 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant