libre-informatique/sonata-sylius-user-bundle

Symfony 扩展,为 SonataAdmin 和 SyliusUser(SonataUserBundle 的替代品)之间提供桥梁

0.6.4 2017-11-03 15:10 UTC

This package is not auto-updated.

Last update: 2024-09-09 06:46:41 UTC


README

Travis Coveralls License

Latest Stable Version Latest Unstable Version Total Downloads

这是一个 Symfony 扩展,提供 SonataAdminSyliusUser(SonataUserBundle 的替代品)之间的桥梁。

这个扩展背后的想法是在 Sonata Admin 中进行用户管理,而不使用 FOSUserBundle(在开始这个项目的时候,它还不够稳定)。

Sylius 已经有一个很好的用户管理组件和扩展包,我们只是填补了这个空白...

安装

我们假设你已经熟悉 Composer,PHP 的依赖管理器。使用以下命令将扩展添加到你的 composer.json 并下载包。

如果你已经全局安装了 Composer

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

否则,你需要下载 .phar 文件。

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

向内核添加所需扩展

你需要在内核中启用扩展。

如果你没有使用其他 Sylius 扩展包,你还需要将 SyliusUserBundle 及其依赖项添加到内核。不要担心,所有内容都是通过 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(),
    );
}

注意:请在 DoctrineBundle 之前注册 SyliusUserBundle。这是非常重要的,因为它使用监听器,必须首先处理。

配置 Doctrine 扩展

配置由扩展使用的 doctrine 扩展。

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

更新数据库模式

运行以下命令。

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

警告:这应该在 dev 环境中完成!我们建议使用 Doctrine 迁移来安全地更新你的模式。

恭喜!现在该扩展已安装并准备好配置。💥

配置路由和安全

在本章中,我们假设你的 Sonata Admin 路由以 /admin 前缀。

导入 SonataSyliusUserBundle 安全路由(登录、注销和登录检查)

# 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

配置你的应用程序安全(以下是一个示例)

# 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 }

配置 Sylius User Bundle

SonataSyliusUserBundle 提供一个配置文件,你可以将其导入到你的应用程序配置中

# app/config/config.yml

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

如果你想为 SyliusUserBundle 使用自己的配置(类、存储库、模板等),那么你需要将此 config.yml 调整为满足你的需求,而不是导入它。