EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

How to hide/show a menu in EasyAdminBundle programatically.

Open abdielcs opened this issue 10 years ago • 4 comments

I read the EasyAdmin documentation and see a lot of customization, but nothing like programatically set the showing menus. I'm thinking for example hide or show the menus depending of the user logged roles.

Thanks in advance.

abdielcs avatar Feb 11 '16 18:02 abdielcs

I'm afraid that this feature is not implemented yet ... and it's going to take a while before including it in the bundle. If this feature is essential to your backend, I recommend you to use SonataAdminBundle instead.

I'm closing this issue because there are other issues related with this feature and we are aware of this. Thank you!

javiereguiluz avatar Feb 11 '16 19:02 javiereguiluz

@abdielcs As this authorization could be done either with classic roles or with voters, I think you should implement this yourself.

What's good is that if you specify an "unknown option" in the entity config, nothing will happen and the option is usable everywhere.

Maybe you could override menu.html.twig and override the helper macro? I don't know if it might work but this is where I'd look first to check whether it's doable without us to implement the feature.

Pierstoval avatar Feb 12 '16 10:02 Pierstoval

Yes I was also looking and it not seem hard to do. I was thinking in override menu.html.twig with some function call before menu being paint that check if some conditions are meet. I don't find an easy way to put that function in AdminController since is not exposed as a service or accessible from twig extensions, something like the Persisting/Updating/Removing functions applied to menus and accessible in my override admin class. Also is the problem of some hidden menu being the default one. But in some other custom service and not putting that menu as the default it should work. Thanks a lot for the advice.

abdielcs avatar Feb 12 '16 15:02 abdielcs

If an event is triggered before render or load the menu is very easy parse the config to load only entries for certain roles. I am using an event subscriber to check permissions for actions. yaml

easy_admin:
    entities:
        DocumentType:
            class: App\Entity\DocumentType
            permissions:
                list:   ['ROLE_USER']
                new:    ['ROLE_ADMIN']
                edit:   ['ROLE_ADMIN']
                delete: ['ROLE_ADMIN']

php

<?php

namespace App\Security\Event;


use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class EasyAdminSecurityEventSubscriber implements EventSubscriberInterface
{

    private $decisionManager;
    private $token;

    public function __construct(AccessDecisionManagerInterface $decisionManager, TokenStorageInterface $token)
    {
        $this->decisionManager = $decisionManager;
        $this->token = $token;
    }

    public static function getSubscribedEvents()
    {
        return array(
            EasyAdminEvents::PRE_LIST => array('isAuthorized'),
            EasyAdminEvents::PRE_EDIT => array('isAuthorized'),
            EasyAdminEvents::PRE_DELETE => array('isAuthorized'),
            EasyAdminEvents::PRE_NEW => array('isAuthorized'),
        );
    }

    public function isAuthorized(GenericEvent $event)
    {
        $entityConfig = $event['entity'];
        $action = $event->getArgument('request')->query->get('action');

        if (array_key_exists('permissions', $entityConfig)) {
            $authorizedRoles = $entityConfig['permissions'][$action];

            if (!$this->decisionManager->decide($this->token->getToken(), $authorizedRoles)) {
                throw new AccessDeniedException();
            };
        }
    }
}

I am looking for a way around using php EasyAdminEvents::PRE_INITIALIZE.

rernesto avatar May 30 '18 14:05 rernesto