覆盖奏鸣曲配置中的安全处理程序:
sonata_admin:
title: "Admin"
security:
handler: custom.sonata.security.handler.role
创建您的服务:
custom.sonata.security.handler.role:
class: MyApp\MyBundle\Security\Handler\CustomRoleSecurityHandler
arguments:
- @security.context
- [ROLE_SUPER_ADMIN, ROLE_ADMIN, ROLE_USER]
- %security.role_hierarchy.roles%
最后一步,但同样重要的是创建您的课程,检索您的用户并根据他的凭据允许/拒绝访问:
/**
* Class CustomRoleSecurityHandler
*/
class CustomRoleSecurityHandler extends RoleSecurityHandler
{
protected $securityContext;
protected $superAdminRoles;
protected $roles;
/**
* @param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext
* @param array $superAdminRoles
* @param $roles
*/
public function __construct(SecurityContextInterface $securityContext, array $superAdminRoles, $roles)
{
$this->securityContext = $securityContext;
$this->superAdminRoles = $superAdminRoles;
$this->roles = $roles;
}
/**
* {@inheritDoc}
*/
public function isGranted(AdminInterface $admin, $attributes, $object = null)
{
/** @var $user User */
$user = $this->securityContext->getToken()->getUser();
if ($user->hasRole('ROLE_ADMIN')){
return true;
}
// do your stuff
}
}