SonataUserBundle 的 check_path 异常

Posted

技术标签:

【中文标题】SonataUserBundle 的 check_path 异常【英文标题】:check_path exception with SonataUserBundle 【发布时间】:2014-10-20 06:00:24 【问题描述】:

我确实可以使用 FOSUserBundle 正常登录。我正在尝试让 SonataUserBundle 现在与 acl 一起工作。

我已按照https://github.com/sonata-project/SonataUserBundle/blob/2.2/Resources/doc/reference/installation.rst 此处的说明进行操作,但每次尝试登录时都会出现异常:

You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.

在 dev 和 prod 中都会引发此异常。当人们尝试引入本地化时,来自网络的类似问题似乎涉及 check_path 的问题。但是针对我提出的解决方案都没有。

这个问题现在已经让我筋疲力尽了,我根本不知道该怎么办。我在相关源文件的 cmets 中留下了几种不同的尝试解决方案,所有相关部分都在下面。

感谢您的帮助。

config.yml:

# FOS User Configuration
fos_user:
    db_driver:      orm # can be orm or odm
    firewall_name:  main
    user_class:     Aleph2Omega\EducationBundle\Entity\User # Application\Sonata\UserBundle\Entity\User # previously


    group:
        group_class:   Application\Sonata\UserBundle\Entity\Group
        group_manager: sonata.user.orm.group_manager

    service:
        user_manager: sonata.user.orm.user_manager

sonata_block:
    default_contexts: [sonata_page_bundle]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]

        #sonata.admin_doctrine_orm.block.audit:
        #    contexts:   [admin]

        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
        sonata.block.service.text:
        sonata.block.service.rss:

        # Some specific block from the SonataMediaBundle
        #sonata.media.block.media:
        #sonata.media.block.gallery:
        #sonata.media.block.feature_media:

sonata_media:
    # if you don't use default namespace configuration
    #class:
    #    media: MyVendor\MediaBundle\Entity\Media
    #    gallery: MyVendor\MediaBundle\Entity\Gallery
    #    gallery_has_media: MyVendor\MediaBundle\Entity\GalleryHasMedia
    default_context: default
    db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr
    contexts:
        default:  # the default context is mandatory
            providers:
                - sonata.media.provider.dailymotion
                - sonata.media.provider.youtube
                - sonata.media.provider.image
                - sonata.media.provider.file

            formats:
                small:  width: 100 , quality: 70
                big:    width: 500 , quality: 70

    cdn:
        server:
            path: /uploads/media # http://media.sonata-project.org/

    filesystem:
        local:
            directory:  %kernel.root_dir%/../web/uploads/media
            create:     false

sonata_user:
    security_acl: true
    manager_type: orm # can be orm or mongodb

routing.yml:

sonata_user_security:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_security_1.xml"

sonata_user_resetting:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_resetting_1.xml"
    prefix: /resetting

sonata_user_profile:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_profile_1.xml"
    prefix: /profile

sonata_user_register:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_registration_1.xml"
    prefix: /register

sonata_user_change_password:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_change_password_1.xml"
    prefix: /profile

gallery:
    resource: '@SonataMediaBundle/Resources/config/routing/gallery.xml'
    prefix: /media/gallery

media:
    resource: '@SonataMediaBundle/Resources/config/routing/media.xml'
    prefix: /media

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

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

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

security.yml:

security.yml:
    providers:
        fos_userbundle:
            id: fos_user.user_manager

        in_memory:
            memory: ~

    # encoders:
    #     FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        # configuration prior to installation of SonataUserBundle
        # main:
        #     pattern: ^/
        #     form_login:
        #         provider: fos_userbundle
        #         csrf_provider: form.csrf_provider
        #     logout:       true
        #     anonymous:    true

        default:
            anonymous: ~

        # 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 # have also tried fos_user_security_login
                use_forward:    false
                check_path:     /login_check # have also tried fos_user_security_check
                failure_path:   null
                csrf_provider:  form.csrf_provider
            logout:             true
            anonymous:          true

    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 access without credential
        -  path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY 
        -  path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY 
        -  path: ^/admin/login_check$, 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 

    acl:
        connection: default

    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

index.html.twig(如果我尝试使用两个路径选项 fos_* 或 sonata_* 中的任何一个,就会出现问题):

% extends 'Aleph2OmegaEducationBundle::layout.html.twig' %

% block title %Welcome to Aleph2Omega% endblock %

% block body %
    % if is_granted('ROLE_SUPER_ADMIN') -%
         include('Aleph2OmegaEducationBundle::menu.html.twig') 
        Hello  app.user.username !
    % elseif is_granted('ROLE_USER') -%
         include('Aleph2OmegaEducationBundle::menu.html.twig') 
        Hello  app.user.username !
    % else %
        <a href=" path('fos_user_security_login') ">Login</a>
        <a href=" path('fos_user_registration_register') ">Register</a>
        <br>
        <a href=" path('sonata_user_security_login') ">Test Sonata Login</a>
        <a href=" path('sonata_user_register') ">Test Sonata Register</a>
    %- endif %
% endblock %

....MyBundle/Entity/User.php:

<?php
// src/A/UserBundle/Entity/User.php

namespace Aleph2Omega\EducationBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    
        parent::__construct();
        // your own logic
    

【问题讨论】:

【参考方案1】:

好的...解决了我自己的问题。

似乎在 security.yml 中的防火墙配置下,通过将 main 放在其他任何东西之前,不再抛出异常。

【讨论】:

以上是关于SonataUserBundle 的 check_path 异常的主要内容,如果未能解决你的问题,请参考以下文章

如何覆盖 SonataUserBundle 模板

获取用户列表时发生 SonataUserBundle 错误

SonataUserBundle 和 Symfony 3.4

SonataUserBundle 覆盖 EditableRolesBuilder

Symfony2 - 扩展或替换 SonataUserBundle 注册表单

禁用 SonataUserBundle sonata.user.admin.group 服务