简体   繁体   English

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

[英]How to setup firewalls with PUGXMultiUserBundle in security.yml

I just started using PUGXMultiUserBundle I want to have 2 users in the system (Administration, clients) Now I want to have separate admin panels and different redirects after successful logins, registrations, etc.. 我刚开始使用PUGXMultiUserBundle,我想在系统中拥有2个用户(管理,客户端)。现在,我想拥有独立的管理面板和成功登录,注册后的不同重定向。

Shouldn't I be able to setup firewalls in my security.yml based on the userdiscrimination? 我是否应该基于用户区分在我的security.yml中设置防火墙?

Now that I followed the instructions and figured out how to build the registration form, and my users are separated. 现在,我按照说明进行操作,并弄清楚如何构建注册表单,并且我的用户已经分开。 When the registration is complete I get an error on the confirmed url 注册完成后,我在确认的网址上收到错误消息

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

Sorry for my poor english, but your solution maybe this: 对不起,我的英语不好,但是您的解决方案可能是这样的:

Declare two services like this in app/config/services.yml or some other service.yml inside any bundle (AppBundle for example): 在app / config / services.yml或任何捆绑包(例如AppBundle)内的其他service.yml中声明两个这样的服务:

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

Next create two class like this: 接下来创建两个这样的类:

<?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;
  }


}

and this: 和这个:

<?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;
      }
    }

Good luk ! 祝你好运!

I solve the problem of No user provider with chained providers like this: 我用这样的链接提供程序解决了没有用户提供程序的问题:

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

I think just now, why have some level of incompatibility with FOSUserBundle and PUXMultiuserBundle, some thinks working time ago, now are brokens ! 我想刚才,为什么与FOSUserBundle和PUXMultiuserBundle有某种程度的不兼容,有人认为工作时间以前,现在坏了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM