Skip to content

Commit a16b068

Browse files
committedFeb 12, 2025
bug #6778 disable switch if user has no permission to edit (IndraGunawan)
This PR was merged into the 4.x branch. Discussion ---------- disable switch if user has no permission to edit This PR aims to make the switch completely disabled on index page if the edit action is disabled or user does not have enough permission to execute edit action ```php public function configureFields(string $pageName): iterable { return [ BooleanField::new('isPublished'), ]; } public function configureActions(Actions $actions): Actions { return $actions ->disable(Action::EDIT) // completely disable EDIT action ->setPermission(Action::EDIT, 'ROLE_ADMIN') // minimum role to execute EDIT action ; } ``` current condition, the switch is toggleable when user clicks it then it creates an ajax request and gets 403 then switch disabled --- I'm a little worried about reordering `BooleanConfigurator` constructor but if it does not reorder I get a deprecated message ``` Deprecated: Optional parameter $csrfTokenManager declared before required parameter $authChecker is implicitly treated as a required parameter ``` Commits ------- ff1faca disable switch if user has no permission to edit
2 parents 37fc36f + ff1faca commit a16b068

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed
 

‎config/services.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@
339339

340340
->set(BooleanConfigurator::class)
341341
->arg(0, service(AdminUrlGenerator::class))
342-
->arg(1, new Reference('security.csrf.token_manager', ContainerInterface::NULL_ON_INVALID_REFERENCE))
342+
->arg(1, new Reference(AuthorizationChecker::class))
343+
->arg(2, new Reference('security.csrf.token_manager', ContainerInterface::NULL_ON_INVALID_REFERENCE))
343344

344345
->set(CollectionConfigurator::class)
345346

‎src/Field/Configurator/BooleanConfigurator.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
1010
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
1111
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface;
12+
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
13+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1214
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
1315

1416
/**
1517
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
1618
*/
1719
final class BooleanConfigurator implements FieldConfiguratorInterface
1820
{
19-
private AdminUrlGeneratorInterface $adminUrlGenerator;
20-
private ?CsrfTokenManagerInterface $csrfTokenManager;
21-
22-
public function __construct(AdminUrlGeneratorInterface $adminUrlGenerator, ?CsrfTokenManagerInterface $csrfTokenManager = null)
23-
{
24-
$this->adminUrlGenerator = $adminUrlGenerator;
25-
$this->csrfTokenManager = $csrfTokenManager;
21+
public function __construct(
22+
private readonly AdminUrlGeneratorInterface $adminUrlGenerator,
23+
private readonly AuthorizationCheckerInterface $authChecker,
24+
private readonly ?CsrfTokenManagerInterface $csrfTokenManager = null,
25+
) {
2626
}
2727

2828
public function supports(FieldDto $field, EntityDto $entityDto): bool
@@ -37,7 +37,9 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
3737
if ($isRenderedAsSwitch) {
3838
$crudDto = $context->getCrud();
3939

40-
if (null !== $crudDto && null !== $entityDto->getPrimaryKeyValue()) {
40+
$hasEditPermission = $this->authChecker->isGranted(Permission::EA_EXECUTE_ACTION, ['action' => Action::EDIT, 'entity' => $entityDto]);
41+
42+
if (null !== $crudDto && null !== $entityDto->getPrimaryKeyValue() && $hasEditPermission) {
4143
$toggleUrl = $this->adminUrlGenerator
4244
->setController($crudDto->getControllerFqcn())
4345
->setAction(Action::EDIT)
@@ -48,6 +50,10 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
4850
$field->setCustomOption(BooleanField::OPTION_TOGGLE_URL, $toggleUrl);
4951
}
5052

53+
if (Action::INDEX === $crudDto->getCurrentAction() && !$hasEditPermission) {
54+
$field->setFormTypeOptionIfNotSet('disabled', true);
55+
}
56+
5157
$field->setFormTypeOptionIfNotSet('label_attr.class', 'checkbox-switch');
5258
$field->setCssClass($field->getCssClass().' has-switch');
5359
}

0 commit comments

Comments
 (0)
Please sign in to comment.