简体   繁体   English

Symfony2 SonataAdmin:尝试扩展SonataUserAdmin时出现“访问被拒绝”异常

[英]Symfony2 SonataAdmin: “Access Denied” Exception when trying to extend SonataUserAdmin

I need to extend SonataUser to set a field called isAdmin to true when a user is being created from the backend. 当从后端创建用户时,我需要扩展SonataUser以将名为isAdmin的字段设置为true。 I have different User groups for ADMIN => (can create admin users and perform CRUD on other entities) and STAFF => (can perform CRUD on other entities). 我对ADMIN =>(可以创建管理员用户并在其他实体上执行CRUD)和STAFF =>(可以对其他实体执行CRUD)使用不同的用户组。 Customers register from the frontend. 客户从前端注册。

Both backend_users (STAFF) and customers are instances of the User entity, which extends SonataUser. backend_users (STAFF)和客户都是User实体的实例,该实体扩展了SonataUser。


Till now I was using the default User and Group Admin classes. 到目前为止,我一直使用默认的User和Group Admin类。 Here is how my app/config/config.yml looked 这是我的app / config / config.yml的样子

...app/config/config.yml...
            users:
                label: Users
                items: [ sonata.user.admin.user ]
            groups:
                label: Groups
                items: [sonata.user.admin.group]
...

It worked fine for me. 对我来说很好。

Now I needed to customize the default implementation so I copied the code from Sonata/UserBundle/User/BaseUser.php to <my namespace>/AdminBundle/Admin/BackendUser.php I created the new service and mapped it in config.yml 现在我需要自定义默认实现,因此我将代码从Sonata/UserBundle/User/BaseUser.php<my namespace>/AdminBundle/Admin/BackendUser.php我创建了新服务并将其映射到config.yml中

...app/config/config.yml...
            users:
                label: Users
                items: [ gd_admin.backend_user ]
            groups:
                label: Groups
                items: [sonata.user.admin.group]
...


...GD/AdminBundle/Resources/services.yml...
parameters:
    gd_admin.backend_user.class: GD\AdminBundle\Admin\BackendUserAdmin
..
services:
    gd_admin.backend_user:
        class: %gd_admin.backend_user.class%
        tags:
            - { name: sonata.admin, manager_type: orm, label: Backend User } 
        arguments: [null, GD\AdminBundle\Entity\User, null]
        # NOTE: No group defined in tags
...

Earlier I had granted the following roles my ADMIN Group: 之前,我曾为ADMIN组授予以下角色:

        'ROLE_SONATA_USER_ADMIN_USER_EDIT',
        'ROLE_SONATA_USER_ADMIN_USER_LIST',
        'ROLE_SONATA_USER_ADMIN_ USER _CREATE',
        'ROLE_SONATA_USER_ADMIN_ USER _VIEW',
        'ROLE_SONATA_USER_ADMIN_ USER _DELETE',
        'ROLE_SONATA_USER_ADMIN_ USER _OPERATOR',
        'ROLE_SONATA_USER_ADMIN_ USER _MASTER',
Now they are:
        'ROLE_GD_ADMIN_BACKEND_USER_EDIT',
        'ROLE_GD_ADMIN_BACKEND_USER_LIST',
        'ROLE_GD_ADMIN_BACKEND_USER_CREATE',
        'ROLE_GD_ADMIN_BACKEND_USER_VIEW',
        'ROLE_GD_ADMIN_BACKEND_USER_DELETE',
        'ROLE_GD_ADMIN_BACKEND_USER_OPERATOR',
        'ROLE_GD_ADMIN_BACKEND_USER_MASTER',

When I log into my admin/dashboard I am able to see the BackendUser in Admin Dashboard widget. 当我登录到admin / dashboard时,可以在Admin Dashboard小部件中看到BackendUser。 But when I click on the "List" or "Add new" I get a 403: Access Denied Exception. 但是,当我单击“列表”或“添加新的”时,我得到了403:访问被拒绝的异常。

Where am I going wrong? 我要去哪里错了?

Thanks, Amit 谢谢阿米特

I don't think you have to mess around with the BaseUser class from the sonata user bundle. 我认为您不必弄混奏鸣曲用户捆绑包中的BaseUser类。

Instead you could create a new admin crud in your own bundle based on the sonata user admin crud (Sonata\\UserBundle\\Admin\\Document\\UserAdmin) and extend it with a prePersist() method to set isAdmin to true: 取而代之的是,您可以根据奏鸣曲用户admin crud(Sonata \\ UserBundle \\ Admin \\ Document \\ UserAdmin)在自己的捆绑包中创建一个新的admin crud,并使用prePersist()方法扩展它以将isAdmin设置为true:

public function prePersist($object)
{
  $object->setIsAdmin(true);
}

prePersist is actually a hook that is called before persisting a new entity. prePersist实际上是在保留新实体之前调用的一个挂钩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM