1

カスタム セキュリティ ハンドラを作成したいのですが、これはユーザー ID によってデータを制限する単純な ACL になります。標準の ACL を使用したくありません。すべての機能を使用して、権限を持つ追加のデータベースを作成する必要はありません。

新しいハンドラーを作成すると、$object を Admin クラスとして受け取ります。Admin クラスを使用すると、サービスへのアクセスを制限できますが、サービス内の行を制限することはできません。

問題は、次のようにエンティティを受け取り、エンティティの許可を確認する方法です。

  public function isGranted(AdminInterface $admin, $attributes, $object = null)
  {
    if ($object->getUserId()==5){
      return true
    }
  }
4

1 に答える 1

2

Overwrite the security handler in sonata config:

sonata_admin:
    title: "Admin"
    security:
         handler: custom.sonata.security.handler.role  

Create your service:

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%

Last step, but not less important is to create your class, retrieve your user and based by his credentials allow/deny access:

/**
* 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
    }
}
于 2014-12-05T08:35:29.377 に答える