Problem extending SonataUserBundle v.5.10 + Symfony v.6.4 + SonataAdminBundle v.4.29

46 views Asked by At

Symfony 6.4, SonataAdminBundle 4.29 SonataUserBundle 5.10 PHP 8.3.2

I've tried to google this, I checked Sonata User Bundle documentation (none there on this), so far I haven't found a solution.

Can anyone please help and if possible provide a simple, working example of an extended SonataUserBundle UserAdmin class in version 5.10 of SonataUserBundle?

I've got a problem with extending SonataUserBundle UserAdmin class, using latest (Feb 2024) version of sonata admin bundle 4.29 + sonata user bundle 5.10.

I'm trying to extend default bundle's UserAdmin class. Having my own custom UserAdmin class extending Sonata\UserBundle\Admin\Model\UserAdmin throws exception on incorrect __construct() parameters. It appears that my service + __construct + parameters are clashing with default (base admin) UserAdmin class __construct() and 1 parameter there. I can't find a way of making it work.

Here are the details of my setup. If there's anything else required to provide please let me know. Any help will be greatly appreciated.

Here's my custom UserAdmin code:

use Sonata\UserBundle\Admin\Model\UserAdmin as BaseAdmin;
use Doctrine\ORM\EntityManager;
use Sonata\UserBundle\Model\UserManagerInterface;
//...

class UserAdmin extends BaseAdmin
{
protected $tokenStorage;
protected $container;
public EntityManager $doctrine;

public function __construct(UserManagerInterface $userManager, TokenStorageInterface $securityToken, EntityManager $em)
{
    $this->tokenStorage = $securityToken;
    $this->doctrine = $em;
    parent::__construct($userManager);
}
//...

Here's my service:

app.admin.user:
    class: App\Admin\UserAdmin
    arguments: ["@sonata.user.manager.user", "@security.token_storage", "@doctrine.orm.entity_manager"]
    tags:
        - { name: sonata.admin, model_class: App\Entity\User, manager_type: orm, group: sonata_user, label: User, pager_type: "simple" }
    public: true```

I've also tried to use set methods in my service but that causes security token = null when I try to access token:

calls:
    - [ setSecurityToken, ["@security.token_storage"] ]
    - [ setEntityManager, ["@doctrine.orm.entity_manager"] ]
    - [ setContainer, ["@service_container"] ]```

that is also not working as expected because when I try to access security tokenStorage in:

protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
{
    $rootAlias = current($query->getRootAliases());
    
    dump($this->tokenStorage);exit; // <-- null

    $user = $this->tokenStorage->getToken()->getUser();

    $query->andWhere(
            $query->expr()->eq($query->getRootAliases()[0] . '.organisation', ':id')
        );
    $query->setParameter('id', $user->getOrganisation());
    
    return $query;
}

security token storage is null, meaning I cannot get the query right.

Here's my sonata.user.yaml config:

sonata_user:
    security_acl: false
    manager_type: orm # can be orm or mongodb
    #impersonating_route: sonata_user_impersonating
    impersonating:
        route: sonata_admin_dashboard

    class:                  # Entity Classes
        user:               App\Entity\User #App\Entity\SonataUserUser
        #taken out sf6.4 group:              App\Entity\Group
    resetting:
        email:
            address: sonata@localhost
            sender_name: Sonata Admin
    admin:                  # Admin Classes
        user:
            class:          App\Admin\UserAdmin
            #class: Sonata\UserBundle\Admin\Entity\UserAdmin
        
            controller: '%sonata.admin.configuration.default_controller%'
            translation:    SonataUserBundle

If anyone could help that would be great.

I've tried to find answers in official documentation official documentation for version 5.x but no info exists.

I've tried to raise a support ticket on sonata user bundle github sonata user bundle github but it seems to me this bundle is rather dead or abandoned.

I've tried stack overflow but I couldn't find anything on the latest sonata user bundle.

1

There are 1 answers

0
71k On

I think I found a solution to allow me to extend SonataUserAdmin.

I changed service from this:

app.admin.user:
        class: App\Admin\UserAdmin
        arguments: ["@sonata.user.manager.user", "@security.token_storage", "@doctrine.orm.entity_manager"]
        tags:
          - { name: sonata.admin, model_class: App\Entity\User, manager_type: orm, group: sonata_user, label: User, pager_type: "simple" }
        public: true

to this:

sonata.user.admin.user:
        class: App\Admin\UserAdmin
        arguments: ["@sonata.user.manager.user", "@security.token_storage", "@doctrine.orm.entity_manager"]
        tags:
          - { name: sonata.admin, model_class: App\Entity\User, manager_type: orm, group: sonata_user, label: User, pager_type: "simple" }
        public: true

Instead of app.admin.user change it to sonata.user.admin.user. This allowed me to load my extended UserAdmin class. Is it just a workaround, or should it be like that? If anyone can explain, provide some insight. I found this to resolve issue with loading extended UserAdmin class and it seems to at least load the page including custom form fields, which is a success.