如何在security.yml中使用PUGXMultiUserBundle设置防火墙

时间:2017-04-22 01:18:34

标签: php symfony

我刚开始使用PUGXMultiUserBundle 我想在系统中有2个用户(管理,客户端) 现在,我想在成功登录,注册等之后拥有单独的管理面板和不同的重定向。

我不应该根据用户歧视在我的security.yml中设置防火墙吗?

现在,我按照说明操作并找出了如何构建注册表单,并且我的用户是分开的。 注册完成后,我在确认的网址上收到错误

There is no user provider for user "AppBundle\Entity\CabAgencyUser".

1 个答案:

答案 0 :(得分:0)

抱歉我的英语很差,但你的解决方案可能就是这样:

在app / config / services.yml中声明两个这样的服务,或在任何bundle中声明一些其他service.yml(例如AppBundle):

app_user_security.component.authentication.handler.login_success_handler:
    class:  AdminBundle\Component\Authentication\Handler\LoginSuccessHandler
    arguments:  [@router, @security.context]
    tags:
        - { name: 'monolog.logger', channel: 'security' }

app_user_security.component.authentication.handler.logout_success_handler:
    class:  AdminBundle\Component\Authentication\Handler\LogoutSuccessHandler
    arguments:  [@router]
    tags:
        - { name: 'monolog.logger', channel: 'security' }

接下来创建两个类:

<?php

/**
 * Handler for users Login
 */

namespace AdminBundle\Component\Authentication\Handler;

use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use BeSimple\I18nRoutingBundle\Routing\Router;

class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface {

  protected $router;
  protected $security;

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

  public function onAuthenticationSuccess(Request $request, TokenInterface $token)
  {
    $session = $request->getSession();
    $obj = array();
    $obj['name'] = $token->getUser()->__toString();
    $obj['username'] = $token->getUsername();

    $session->set('last_login_user', $obj);

    if ($this->security->isGranted('ROLE_SUPER_ADMIN') || $this->security->isGranted('ROLE_ADMINISTRADOR'))
    {
      $referer_url = $this->router->generate('admin_dashboard');
    }
    elseif($this->security->isGranted('ROLE_USUARIO') || $this->security->isGranted('ROLE_USUARIO_SOCIAL')) {
      $referer_url = $this->router->generate('app_frontend_dashboard', array(
          //'slug' => $token->getUser()->getSlug()
      ));
    } else {
        $referer_url = $this->router->generate('app_frontend_dashboard');
    }

    $cookie = new Cookie('last_login_user', serialize($token->getUser()), time()+(3600*48));

    $response = new RedirectResponse($referer_url);
    $response->headers->setCookie($cookie);

    return $response;
  }


}

和此:

<?php

    /**
     * Handler for logout...
     */

    namespace AdminBundle\Component\Authentication\Handler;

    use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use BeSimple\I18nRoutingBundle\Routing\Router;

    class LogoutSuccessHandler implements LogoutSuccessHandlerInterface {

      protected $router;

      public function __construct(Router $router)
      {
        $this->router = $router;
      }

      public function onLogoutSuccess(Request $request)
      {
        // redirect the user to where they were before the login process begun.
        //$referer_url = $request->headers->get('referer');
        //$response = new RedirectResponse($referer_url);

          $referer_url = $this->router->generate('app_frontend_homepage');
          $response = new RedirectResponse($referer_url);

          return $response;
      }
    }

好luk!

我解决了没有这样的链接提供商的用户提供商的问题:

//security.yml

security:

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

    role_hierarchy:
        ROLE_ADMINISTRADOR:         [ROLE_USUARIO, ROLE_USUARIO_SOCIAL]
        ROLE_SUPER_ADMINISTRADOR:   ROLE_ADMINISTRADOR

    # http://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
    providers:
        chain_provider:
            chain:
                providers: [in_memory, fos_userbundle, user_db_username, user_db_email]
        in_memory:
            memory: ~
        fos_userbundle:
            id: fos_user.user_provider.username_email
        user_db_username:
            entity: { class: AdminBundle\Entity\UsuarioBase, property: username }
        user_db_email:
            entity: { class: AdminBundle\Entity\UsuarioBase, property: email }

我想刚才,为什么与FOSUserBundle和PUXMultiuserBundle有一定程度的不兼容,有些人认为工作时间以前,现在已经破产了!