Skip to content

Commit

Permalink
feature #6447 Pass the entity FQCN to all action voters (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Pass  the entity FQCN to all action voters

Fixes #6101.

Commits
-------

f67b74f Pass  the entity FQCN to all action voters
  • Loading branch information
javiereguiluz committed Sep 19, 2024
2 parents 735ed80 + f67b74f commit 4828f9d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/Controller/AbstractCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function index(AdminContext $context)
return $event->getResponse();
}

if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::INDEX, 'entity' => null])) {
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::INDEX, 'entity' => null, 'entityFqcn' => $context->getEntity()->getFqcn()])) {
throw new ForbiddenActionException($context);
}

Expand Down Expand Up @@ -172,7 +172,7 @@ public function detail(AdminContext $context)
return $event->getResponse();
}

if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::DETAIL, 'entity' => $context->getEntity()])) {
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::DETAIL, 'entity' => $context->getEntity(), 'entityFqcn' => $context->getEntity()->getFqcn()])) {
throw new ForbiddenActionException($context);
}

Expand Down Expand Up @@ -207,7 +207,7 @@ public function edit(AdminContext $context)
return $event->getResponse();
}

if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::EDIT, 'entity' => $context->getEntity()])) {
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::EDIT, 'entity' => $context->getEntity(), 'entityFqcn' => $context->getEntity()->getFqcn()])) {
throw new ForbiddenActionException($context);
}

Expand Down Expand Up @@ -289,7 +289,7 @@ public function new(AdminContext $context)
return $event->getResponse();
}

if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::NEW, 'entity' => null])) {
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::NEW, 'entity' => null, 'entityFqcn' => $context->getEntity()->getFqcn()])) {
throw new ForbiddenActionException($context);
}

Expand Down Expand Up @@ -347,7 +347,7 @@ public function delete(AdminContext $context)
return $event->getResponse();
}

if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::DELETE, 'entity' => $context->getEntity()])) {
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::DELETE, 'entity' => $context->getEntity(), 'entityFqcn' => $context->getEntity()->getFqcn()])) {
throw new ForbiddenActionException($context);
}

Expand Down Expand Up @@ -415,7 +415,7 @@ public function batchDelete(AdminContext $context, BatchActionDto $batchActionDt
}

$entityDto = $context->getEntity()->newWithInstance($entityInstance);
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::DELETE, 'entity' => $entityDto])) {
if (!$this->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::DELETE, 'entity' => $entityDto, 'entityFqcn' => $context->getEntity()->getFqcn()])) {
throw new ForbiddenActionException($context);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Dto/SearchDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getQuery(): string
public function getQueryTerms(): array
{
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $this->query, $matches);
$terms = array_map(static fn ($match) => trim($match, '" '), $matches[0] ?? []);
$terms = array_map(static fn ($match) => trim($match, '" '), $matches[0]);

return $terms;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Security/SecurityVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function voteOnAttribute($permissionName, $subject, TokenInterface $to
}

if (Permission::EA_EXECUTE_ACTION === $permissionName) {
return $this->voteOnExecuteActionPermission($this->adminContextProvider->getContext()->getCrud(), $subject['action'] ?? null, $subject['entity'] ?? null);
return $this->voteOnExecuteActionPermission($this->adminContextProvider->getContext()->getCrud(), $subject['action'] ?? null, $subject['entity'] ?? null, $subject['entityFqcn'] ?? null);
}

if (Permission::EA_VIEW_FIELD === $permissionName) {
Expand All @@ -67,7 +67,7 @@ private function voteOnViewMenuItemPermission(MenuItemDto $menuItemDto): bool
return $this->authorizationChecker->isGranted($menuItemDto->getPermission(), $menuItemDto);
}

private function voteOnExecuteActionPermission(CrudDto $crudDto, ActionDto|string $actionNameOrDto, ?EntityDto $entityDto): bool
private function voteOnExecuteActionPermission(CrudDto $crudDto, ActionDto|string $actionNameOrDto, ?EntityDto $entityDto, ?string $entityFqcn): bool
{
// users can run the Crud action if:
// * they have the required permission to execute the action on the given entity instance
Expand All @@ -78,9 +78,9 @@ private function voteOnExecuteActionPermission(CrudDto $crudDto, ActionDto|strin
$actionPermission = $crudDto->getActionsConfig()->getActionPermissions()[$actionName] ?? null;
$disabledActionNames = $crudDto->getActionsConfig()->getDisabledActions();

$subject = null === $entityDto ? null : $entityDto->getInstance();
$subject = $entityDto?->getInstance() ?? $entityFqcn;

return $this->authorizationChecker->isGranted($actionPermission, $subject) && !\in_array($actionName, $disabledActionNames, true);
return !\in_array($actionName, $disabledActionNames, true) && $this->authorizationChecker->isGranted($actionPermission, $subject);
}

private function voteOnViewPropertyPermission(FieldDto $field): bool
Expand Down

0 comments on commit 4828f9d

Please sign in to comment.