Introduction to SonataUserBundle
5.xAdminBundle to include user and group management features.repository·5.x·Indexed 43 days ago
https://github.com/sonata-project/sonatauserbundleA 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.
AdminBundle to include user and group management features.The Sonata\UserBundle\Form\Type\RolesMatrixType provides a matrix visualization of user permissions. It consists of two main components:
EDIT, LIST, CREATE, VIEW, DELETE, EXPORT, ALL).config/packages/security.yaml as checkboxes, including their inherited roles.Users can only select roles they have been granted.
When managing users, roles are categorized into two distinct types to control what a user can assign to others:
MASTER permission or role.This distinction ensures that administrative users can only delegate specific levels of authority.
normal users from modifying the settings or configurations of super-admin users.User groups (previously provided via FOSUserBundle integration) were removed in SonataUserBundle 5.0 to reduce complexity. To upgrade:
Group class.If you require grouping functionality, it is recommended to use a role hierarchy instead.
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],
];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: trueSonataUserBundle 5.0 has removed its dependency on FOSUserBundle. Most core features from FOSUserBundle have been migrated directly into SonataUserBundle. When upgrading:
FOSUserBundle from your composer.json and dependencies.FOSUserBundle specific code from your application.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\SonataUserUserUser 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.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