FriendsOfSymfony / FOSUserBundle

Provides user management for your Symfony project. Compatible with Doctrine ORM & ODM, and custom storages.
https://symfony.com/doc/master/bundles/FOSUserBundle/index.html
MIT License
3.25k stars 1.57k forks source link

Migration fos user 2.x to 3.x -> groups depreciation #3063

Closed salamichel closed 1 year ago

salamichel commented 1 year ago

Hi fos community,

I have an application using groups, i do not understand why this feature have been removed, and i do not find any solutions to handle this migration.

My application works with groups where i have set permissions ROLE_xxx

Then i can easily change user permission by change or assign the user to a right groups.

How to handle this functionnality with fos 3.x ?

Regards

stof commented 1 year ago

If the hierarchy of permissions is static, the recommended replacement is to use the Symfony role hierarchy feature. If the hierarchy of permissions is dynamic, the recommended replacement is to use custom voters (which can then be implemented based on your business domain, which could be architectured as you want).

Groups in FOSUserBundle were implemented at a time where KnpUserBundle was a port of the sfUserPlugin of symfony 1.x and did not really fit the Symfony 2+ architecture. That's why it has been deprecated and removed.

salamichel commented 1 year ago

Hi, thank you for your reply, regarding my issue, i'm using Sonata Admin for BackOffice, with lot of groups and permissions associate.

I found a work around based on this https://symfony.com/doc/current/security.html#roles

namespace App\Application\Sonata\UserBundle\Entity;

    public function getRoles(): array
    {
        $roles = $this->roles;

        // we need to make sure to have at least one role
        $roles[] = static::ROLE_DEFAULT;

        foreach ($this->getGroups() as $group) {
            if ($group instanceof Group) {
                $roles = array_merge($roles, $group->getRoles());
            }
        }
        return array_values(array_unique($roles));
    }