如何使用FOSUserBundle为管理员和其他用户设置登录表单?

问题描述:

在为管理员用户提供后端时,有趣的是要有一个登录表单,同时在我们网站的公共区域中有一个普通用户的普通登录表单.

When having a backend for admin users, it is interesting to have a login form, and at the same time having a normal login form for normal users in the public area of our website.

使用FOSUserBundle可以吗?如何通过"Symfony2"方式完成操作?

Is that possible using FOSUserBundle? How can it be done "the Symfony2" way?

首先,我们需要为管理区域配置一些特殊的路由:

First we need to configure some special routes for the admin area:

admin_login:
    pattern:  /admin/login
    defaults: { _controller: FOSUserBundle:Security:login }

admin_login_check:
    pattern:  /admin/login_check
    defaults: { _controller: FOSUserBundle:Security:check }

admin_logout:
    pattern:  /admin/logout
    defaults: { _controller: FOSUserBundle:Security:logout }

接下来,使用这些路由为管理区域配置一个特殊的防火墙,并将其定义为匿名访问:

Next configure a special firewall for the admin area using these routes, and define them to be anonymously accessed:

firewalls:
  ...
  admin:
    pattern:            /admin/(.*)
    form_login:
      provider:       fos_userbundle
      login_path:     admin_login
      check_path:     admin_login_check
      default_target_path: yourproject_admin_default_index
    logout:
      path:           admin_logout
      target:         admin_login
    anonymous:        true
    context:          application

  main:
    pattern: ^/
    form_login:
      provider:      fos_userbundle
      csrf_provider: form.csrf_provider
    context:         application
    ...

access_control:
  ...
  - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  - { path: ^/admin/, role: ROLE_ADMIN }

好!我们刚刚将登录系统分为两部分:admin和main.

Ok! We have just separated our login system in two parts: admin and main.

让我们重写SecurityController.为此,我们将需要创建一个父级为FOSUserBundle的自定义捆绑包(检查该文档).在这个新的捆绑包中,创建控制器:

Let's override the SecurityController. For that we will need to create a custom bundle which parent is FOSUserBundle (check the doc for that). In this new bundle, create the controller:

<?php

namespace YourProject\UserBundle\Controller;

use FOS\UserBundle\Controller\SecurityController as BaseController;

/**
 * {@inheritDoc}
 */
class SecurityController extends BaseController
{
    /**
     * {@inheritDoc}
     */
    public function renderLogin(array $data)
    {
        $requestAttributes = $this->container->get('request')->attributes;

        if ('admin_login' === $requestAttributes->get('_route')) {
            $template = sprintf('AdminBundle:Security:login.html.twig');
        } else {
            $template = sprintf('FOSUserBundle:Security:login.html.twig');
        }

        return $this->container->get('templating')->renderResponse($template, $data);
    }
}

就是这样!现在,您可以编写AdminBundle:Security:login.html.twig:)

That's it! Now you can write your AdminBundle:Security:login.html.twig :)

注意::不要忘记在您的管理区域中使用管理路由! (在登录表单操作,注销链接等中)

NOTE: Don't forget to use the admin routes in your admin area! (in the login form action, the logout link, etc)