Sonata管理员:如何根据角色跳过或删除仪表板?

时间:2015-06-29 14:16:48

标签: symfony sonata-admin

我有几个角色只管理一种类型的实体。在仪表板上登陆用户并让他们点击实体部分似乎是多余的。

是否可以删除信息中心,并根据角色设置备用默认目标网页?

1 个答案:

答案 0 :(得分:0)

您可以创建一个新服务并覆盖Sonata的isGranted方法:

.yml

custom.sonata.security.handler.role:
    class: AdminBundle\Security\Handler\CustomRoleSecurityHandler
    arguments:
        - @security.token_storage
        - @security.authorization_checker
        - [ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_USER]
        - %security.role_hierarchy.roles%

.PHP

class CustomRoleSecurityHandler extends RoleSecurityHandler
{
    protected $securityContext;

    protected $superAdminRoles;

    protected $roles;
    /**
    * @var TokenStorageInterface
    */
    private $tokenStorageInterface;

    /**
    * @param TokenStorageInterface $tokenStorageInterface
    * @param AuthorizationCheckerInterface $securityContext
    * @param array $superAdminRoles
    * @param $roles
    */
    public function __construct(TokenStorageInterface $tokenStorageInterface, AuthorizationCheckerInterface $securityContext, array $superAdminRoles, $roles)
    {
        $this->securityContext = $securityContext;
        $this->superAdminRoles = $superAdminRoles;
        $this->roles           = $roles;
        $this->tokenStorageInterface = $tokenStorageInterface;
    }

    /**
    * {@inheritDoc}
    */
    public function isGranted(AdminInterface $admin, $attributes, $object = null)
    {

        if (!is_array($attributes)) {
            $attributes = array($attributes);
        }

        foreach ($attributes as $pos => $attribute) {
            $attributes[$pos] = sprintf($this->getBaseRole($admin), $attribute);
        }

        $user = $this->tokenStorageInterface->getToken()->getUser();
        // ... check user role and do your stuff
    }

}