SonataUserBundle

repository·5.x·Indexed 43 days ago

https://github.com/sonata-project/sonatauserbundle

A Symfony bundle providing comprehensive user management and authentication capabilities. It integrates with Symfony's security component and SonataAdminBundle to handle users, roles, and security configurations, supporting both Doctrine ORM and MongoDB.

Tokens
6.7K
Snippets
24
Records
37
Agent score
56%

What's inside SonataUserBundle

  1. Understand the Roles Matrix view

    5.x

    The Sonata\UserBundle\Form\Type\RolesMatrixType provides a matrix visualization of user permissions. It consists of two main components:

    1. Admin Permissions Matrix: Displays each admin and their associated permissions (e.g., EDIT, LIST, CREATE, VIEW, DELETE, EXPORT, ALL).
    2. Custom Roles List: Displays custom roles configured in config/packages/security.yaml as checkboxes, including their inherited roles.

    Users can only select roles they have been granted.

  2. Understand role assignment permissions in SonataUserBundle

    5.x

    When managing users, roles are categorized into two distinct types to control what a user can assign to others:

    1. editable roles: These are the roles that the current user is permitted to assign to other users. This is controlled by the MASTER permission or role.
    2. readonly roles: These are roles assigned to the current user that they are allowed to see, but are not permitted to assign to anyone else.

    This distinction ensures that administrative users can only delegate specific levels of authority.

  3. Remove User Groups in version 5.0

    5.x

    User groups (previously provided via FOSUserBundle integration) were removed in SonataUserBundle 5.0 to reduce complexity. To upgrade:

    1. Remove your Group class.
    2. Execute a database migration to remove the groups table.
    3. Update your configuration to remove any references to groups.

    If you require grouping functionality, it is recommended to use a role hierarchy instead.

  4. Install SonataUserBundle

    5.x

    To install the bundle, use Composer to require the package. If you plan to use it with SonataAdminBundle, ensure you have installed SonataAdminBundle and a persistence bundle (SonataDoctrineORMAdminBundle or SonataDoctrineMongoDBAdminBundle) first.

    After installing, enable the bundle in your config/bundles.php file.

    composer require sonata-project/user-bundle
    // config/bundles.php
    return [
        // ...
        Sonata\UserBundle\SonataUserBundle::class => ['all' => true],
    ];
  5. Enable User Impersonation in Symfony Security

    5.x

    To use user impersonation links provided by SonataUserBundle, you must first enable the feature within your Symfony firewall configuration. Additionally, the user performing the switch must possess the ROLE_ALLOWED_TO_SWITCH role.

    Note that sessions are not shared across different firewalls by default. If you are attempting to impersonate a user authenticated on a different firewall (e.g., switching from an admin firewall to a user on the main firewall), you must configure the firewall context as described in the Symfony Documentation.

    # config/packages/security.yaml
    
    security:
        role_hierarchy:
            ROLE_SUPER_ADMIN: [ROLE_SONATA_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    
        firewalls:
            admin:
                switch_user: true
  6. Migrate from FOSUserBundle to SonataUserBundle 5.0

    5.x

    SonataUserBundle 5.0 has removed its dependency on FOSUserBundle. Most core features from FOSUserBundle have been migrated directly into SonataUserBundle. When upgrading:

    1. Remove FOSUserBundle from your composer.json and dependencies.
    2. Remove any FOSUserBundle specific code from your application.
    3. Run database migrations to clean up any FOSUserBundle-specific tables.
  7. Configure Doctrine MongoDB for SonataUserBundle

    5.x

    When using Doctrine MongoDB, create a document in src/Document/SonataUserUser that extends Sonata\UserBundle\Document\BaseUser. Then, update config/packages/sonata_user.yaml to set the manager_type to mongodb and point to your new document class.

    // src/Document/SonataUserUser.php
    
    use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
    use Sonata\UserBundle\Document\BaseUser;
    
    #[ODM\Document]
    class SonataUserUser extends BaseUser
    {
        #[ODM\Id]
        protected $id;
    }
    # config/packages/sonata_user.yaml
    
    sonata_user:
        manager_type: mongodb
        class:
            user: App\Document\SonataUserUser
  8. Extend the User class for Profile or Social fields in version 5.0

    5.x
    The base User class in version 5.0 no longer includes Profile or Social fields. If your application requires these fields, you must extend the User class and manually add them. After extending the class, ensure you run migrations to remove the old fields from your database schema.
  9. Configure Doctrine ORM for SonataUserBundle

    5.x

    When using Doctrine ORM, you must add the bundle to your entity manager mappings in config/packages/doctrine.yaml.

    Then, create a user entity that extends Sonata\UserBundle\Entity\BaseUser (or BaseUser3 if using doctrine/orm ^3). Finally, update your database schema.

    # config/packages/doctrine.yaml
    
    doctrine:
        orm:
            entity_managers:
                default:
                    mappings:
                        SonataUserBundle: ~
    // src/Entity/SonataUserUser.php
    
    use Doctrine\DBAL\Types\Types;
    use Doctrine\ORM\Mapping as ORM;
    use Sonata\UserBundle\Entity\BaseUser;
    
    #[ORM\Entity]
    #[ORM\Table(name: 'user__user')]
    class SonataUserUser extends BaseUser
    {
        #[ORM\Id]
        #[ORM\Column(type: Types::INTEGER)]
        #[ORM\GeneratedValue]
        protected $id;
    }
    bin/console doctrine:schema:update --force