libre-informatique/sonata-sylius-user-bundle

Symfony bundle providing a bridge between SonataAdmin and SyliusUser (an alternative to SonataUserBundle)

0.6.4 2017-11-03 15:10 UTC

This package is not auto-updated.

Last update: 2024-12-16 08:01:03 UTC


README

Travis Coveralls License

Latest Stable Version Latest Unstable Version Total Downloads

This is a Symfony bundle providing a bridge between SonataAdmin and SyliusUser (an alternative to SonataUserBundle).

The idea behind this bundle was to have user management in Sonata Admin without using FOSUserBundle (which was not stable enough by the time we started this project).

Sylius already had a good user management component and bundle, we just filled the gap...

Installation

We assume you're familiar with Composer, a dependency manager for PHP. Use the following command to add the bundle to your composer.json and download the package.

If you have Composer installed globally

$ composer require libre-informatique/sonata-sylius-user-bundle

Otherwise you have to download .phar file.

$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar require libre-informatique/sonata-sylius-user-bundle

Adding required bundles to the kernel

You need to enable the bundle inside the kernel.

If you're not using any other Sylius bundles, you will also need to add SyliusUserBundle and its dependencies to kernel. Don't worry, everything was automatically installed via Composer.

<?php

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // SYLIUS USER BUNDLE AND DEPENDENCIES
        new FOS\RestBundle\FOSRestBundle(),
        new JMS\SerializerBundle\JMSSerializerBundle($this),
        new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
        new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(),
        new Bazinga\Bundle\HateoasBundle\BazingaHateoasBundle(),
        new winzou\Bundle\StateMachineBundle\winzouStateMachineBundle(),
        // Sylius Bundles have to be declared before DoctrineBundle
        new Sylius\Bundle\ResourceBundle\SyliusResourceBundle(),
        new Sylius\Bundle\MailerBundle\SyliusMailerBundle(),
        new Sylius\Bundle\UserBundle\SyliusUserBundle(),

        // OTHER BUNDLES...
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        // [...]

        // SONATA SYLIUS USER BUNDLE
        new Librinfo\SonataSyliusUserBundle\SonataSyliusUserBundle(),
    );
}

Note: Please register SyliusUserBundle before DoctrineBundle. This is important as it uses listeners which have to be processed first.

Configure Doctrine extensions

Configure doctrine extensions which are used by the bundle.

# app/config/config.yml
stof_doctrine_extensions:
    orm:
        default:
            timestampable: true

Update database schema

Run the following command.

$ php bin/console doctrine:schema:update --force

Warning: This should be done only in dev environment! We recommend using Doctrine migrations, to safely update your schema.

Congratulations! The bundle is now installed and ready to be configured. 💥

Configure routes and security

In this chapter, we assume your Sonata Admin routes are prefixed with /admin.

Import SonataSyliusUserBundle security routes (for login, lougout an login_check):

# app/config/routing.yml

# Security routing for SyliusUserBundle
# (defines login, logout and login_check routes)
sonata_sylius_user_security:
    resource: "@SonataSyliusUserBundle/Resources/config/routing/security.yml"
    prefix: /admin

Configure your application security (this is an example):

# app/config/security.yml

security:

    encoders:
        Sylius\Component\User\Model\UserInterface: sha512

    providers:
        sonata_user_provider:
            id: sylius.sonata_user_provider.email_or_name_based

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        sonata:
            switch_user: true
            context: sonata
            pattern: /admin(?:/.*)?$
            form_login:
                provider: sonata_user_provider
                login_path: sonata_sylius_user_login
                check_path: sonata_sylius_user_login_check
                failure_path: sonata_sylius_user_login
                default_target_path: sonata_admin_dashboard
                use_forward: false
                use_referer: true
            logout:
                path: sonata_sylius_user_logout
                target: sonata_sylius_user_login
            anonymous: true

    access_control:
        - { path: ^/(css|images|js), role: IS_AUTHENTICATED_ANONYMOUSLY } # allow assets for anonymous users
        - { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } # allow resetting password for anonymous users
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: "^/admin.*", role: ROLE_ADMINISTRATION_ACCESS }

Configure Sylius User Bundle

SonataSyliusUserBundle provides a configuration file that you can import in your application configuration :

# app/config/config.yml

imports:
    - { resource: "@SonataSyliusUserBundle/Resources/config/app/config.yml" }

If you want to use your own configuration for SyliusUserBundle (classes, repositoties, templates, etc), then you will have to adapt this config.yml to your needs instead of importing it.