Symfony app登录和Sonata管理仪表板登录

时间:2017-02-22 18:54:45

标签: symfony sonata-admin

在我的应用程序中,我有2个部分,一个手动生成的前端和一个使用Sonata管理员生成的后端仪表板。我在登录这两个部分时遇到了问题:

应用/配置/ security.yml

security:

encoders:
    FOS\UserBundle\Model\UserInterface: sha512
    m_and_m\UsuariosBundle\Entity\Usuarios: { algorithm: sha512, iterations: 10 }
    m_and_m\ClientesBundle\Entity\Clientes: { algorithm: sha512, iterations: 10 }

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/login ,role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/logout ,role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/* ,role: [ROLE_ADMIN, ROLE_SONATA_ADMIN,TOTAL] }

    - { path: ^/usuarios/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/usuarios/registro, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/usuarios/*, roles: ROLE_USUARIO }
    - { path: ^/clientes/*, roles: ROLE_CLIENTES }


role_hierarchy:
    ROLE_ADMIN:     [ROLE_USER, ROLE_SONATA_ADMIN,ROLE_USUARIO,ROLE_CLIENTE]
    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

    chain_provider:
        chain:
            providers: [usuarios, clientes]
    usuarios:
        entity: { class: m_and_m\UsuariosBundle\Entity\Usuarios, property: email }
    clientes:
        entity: { class: m_and_m\ClientesBundle\Entity\Clientes, property: email }



firewalls:


    # -> 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:   /admin/login
        logout:
            path:           /admin/logout
            target:         /admin
        anonymous:          true
    # -> end custom configuration

    # default login area for standard users

    frontend:
        pattern:    ^/*
        context:    frontend
        provider:   chain_provider
        anonymous:  ~
        form_login:
            login_path: usuarios_login
            check_path: usuarios_login_check
        logout:
            path: usuarios_logout

UsuariosBundle /资源/配置/ services.yml

services:
usuarios.admin.usuarios:
    class: m_and_m\UsuariosBundle\Admin\UsuariosAdmin
    arguments: [~, m_and_m\UsuariosBundle\Entity\Usuarios, SonataAdminBundle:CRUD]
    tags:
        - {name: sonata.admin, manager_type: orm, group: Usuarios(Admin), label: Usuarios}

login_listener:
    class:      m_and_m\UsuariosBundle\Listener\LoginListener
    arguments:  [@security.context, @router]
    tags:
        - { name: kernel.event_listener, event: security.interactive_login }
        - { name: kernel.event_listener, event: kernel.response }

根据该文档,我在LoginListener中创建了usuariosbundle。 当我从前端登录时,一切都很完美。但是,Sonata管理仪表板登录会转到LoginListener.php文件并返回错误:

  

FatalErrorException:错误:调用未定义的方法Application \ Sonata \ UserBundle \ Entity \ User :: getNombre()in   C:\ WAMP \ WWW \ m_and_m的\ src \ m_and_m \ UsuariosBundle \监听\ LoginListener.php   第24行

LoginListener.php

<?php
namespace m_and_m\UsuariosBundle\Listener;

use Symfony\Componene\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Routing\Router;
use Symfony\Component\HttpFoundation\RedirectResponse;

class LoginListener
{
private $contexto, $router, $usuario=null,$role=null;

public function __construct(SecurityContext $context, Router $router)
{
    $this->contexto=$context;
    $this->router = $router;
}

public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
    $token = $event->getAuthenticationToken();
    $this->usuario = $token->getUser()->getNombre();
    $this->role = $token->getRoles();
    $this->role=$this->role[0]->getrole();
    if($this->role=='ROLE_CLIENTE' && $token->getUser()->getActivo()==false){
        $this->usuario = null;
    }
}

public function onKernelResponse(FilterResponseEvent $event)
{
    if(null != $this->usuario)
    {
        if($this->role=='ROLE_USUARIO'){
            $portada=$this->router->generate('portada_usuario');
        }
        else{
            $portada=$this->router->generate('portada_cliente');
        }

        $event->setResponse(new RedirectResponse($portada));
        $event->stopPropagation();
    }
}
}
?>

我不知道为什么会发生这种情况,因为两个提供商都处于不同的环境中。我是否需要做更多的事情来分开两次登录?

1 个答案:

答案 0 :(得分:0)

您正在调用该实体中不存在的方法。如果您在错误中注意,您将看到来自令牌的用户类不是您认为的那个。

相关问题