Blog Post

Comment installer Sonata + FOSUserBundle ?

Ce tutoriel est basé sur une version Symfony 2.7 et considère que FOSUserBundle a déjà été installé dans le projet.
source : https://sonata-project.org/bundles/admin/master/doc/reference/installation.html

Installation de Sonata

composer require sonata-project/admin-bundle
composer require sonata-project/doctrine-orm-admin-bundle
#app/AppKernel.php : enregistrer les bundle registerBundles

new Sonata\CoreBundle\SonataCoreBundle(),
new Sonata\BlockBundle\SonataBlockBundle(),
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
new Sonata\AdminBundle\SonataAdminBundle(),
# app/config/config.yml
sonata_block:
    default_contexts: [cms]
    blocks:
        # enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts:   [admin]

Installation des Assets :

php app/console assets:install

Nettoyage du cache :

php app/console cache:clear

Ajout des routes :

# app/config/routing.yml
admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

Une fois fait, il est possible d’accéder à l’admin http://yoursite.local/admin/dashboard

NOTA : si vous avez déjà installer FOSUserBundle vous devriez avoir un message du genre :

Full authentication is required to access this resource.

Il faut mettre la ligne – { path: ^/admin/, role: ROLE_ADMIN } en commentaire dans le fichier security.yml
pour pouvoir accéder à l’admin sans authentification.

Configurer Sonata + FOSUserBundle

Source : https://sonata-project.org/bundles/user/master/doc/reference/installation.html

composer require sonata-project/easy-extends-bundle

et on enregistre le bundle dans AppKernel.php

#app/AppKernel.php : enregistrer les bundle registerBundles
new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle(),
composer require sonata-project/user-bundle --no-update
composer require sonata-project/doctrine-orm-admin-bundle  --no-update
composer update

et on enregistre le bundle dans AppKernel.php

#app/AppKernel.php : enregistrer les bundle registerBundles

# si FOSUserBundle n'a pas été installé :	
new FOS\UserBundle\FOSUserBundle(),
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
# app/config/config.yml
sonata_user:
    security_acl: true
    manager_type: orm # can be orm or mongodb

sonata_block:
    blocks:
        sonata.user.block.menu:    # used to display the menu in profile pages
        sonata.user.block.account: # used to display menu option (login option)
        sonata.block.service.text: # used to if you plan to use Sonata user routes
# app/config/security.yml
security:
    acl:
        connection: default

Mise à jour de Doctrine :

# app/config/config.yml
fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main
    user_class:     Sonata\UserBundle\Entity\BaseUser

    group:
        group_class:   Sonata\UserBundle\Entity\BaseGroup
        group_manager: sonata.user.orm.group_manager                    # If you're using doctrine orm (use sonata.user.mongodb.user_manager for mongodb)

    service:
        user_manager: sonata.user.orm.user_manager                      # If you're using doctrine orm (use sonata.user.mongodb.group_manager for mongodb)

doctrine:
    dbal:
        types:
            json: Sonata\Doctrine\Types\JsonType

Si on n’utilise pas auto_mapping=true il faut ajouter :

doctrine:
    orm:
        entity_managers:
            default:
                mappings:
                    ApplicationSonataUserBundle: ~
                    SonataUserBundle: ~
                    FOSUserBundle: ~                   

On ajoute les routes :

sonata_user_admin_security:
    resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml'
    prefix: /admin

sonata_user_admin_resetting:
    resource: '@SonataUserBundle/Resources/config/routing/sonata_resetting_1.xml' # et non 'admin_resetting.xml' : erreur dans la doc
    prefix: /admin/resetting

et ensuite on configure le firewall :

# app/config/security.yml
security:
    role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
        SONATA:
            - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  # if you are using acl then this line must be commented

    providers:
        fos_userbundle:
            id: fos_user.user_manager

    firewalls:
        # Disabling the security for the web debug toolbar, the profiler and Assetic.
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # -> custom firewall for the admin area of the URL
        admin:
            pattern:            /admin(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
            anonymous:          true

        # -> end custom configuration

        # default login area for standard users

        # This firewall is used to handle the public login area
        # This part is handled by the FOS User Bundle
        main:
            pattern:             .*
            context:             user
            form_login:
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
            logout:             true
            anonymous:          true

Ensuite mettre à jour le security.yml :

# app/config/security.yml
security:
    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Admin login page needs to be accessed without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

On lance la création des entités :

php app/console sonata:easy-extends:generate SonataUserBundle -d src

et on enregistre le bunlde nouvellement créé :

#app/AppKernel.php : enregistrer les bundle registerBundles
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),

et on met à jour la configuration :

# app/config/config.yml
fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main
    user_class:     Application\Sonata\UserBundle\Entity\User

    group:
        group_class:   Application\Sonata\UserBundle\Entity\Group
        group_manager: sonata.user.orm.group_manager                    # If you're using doctrine orm (use sonata.user.mongodb.group_manager for mongodb)

    service:
        user_manager: sonata.user.orm.user_manager                      # If you're using doctrine orm (use sonata.user.mongodb.user_manager for mongodb)

doctrine:

    dbal:
        types:
            json: Sonata\Doctrine\Types\JsonType
            

About pascall

Related Posts

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *