FosUserBundle不同的登录页面 [英] FosUserBundle Different Login Pages

查看:84
本文介绍了FosUserBundle不同的登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了几个小时,但我没有发现我们该如何覆盖用户和管理员侧的登录模板.

I'v searching for hours and i did not find really how can we override login template for user and the admin side.

我已经在security.yml中创建了一个管理防火墙

I'v created an admin firewall in security.yml

  firewalls:
    admin:
      pattern: /admin(.*)
      form_login:
        provider: fos_userbundle
        csrf_provider: form.csrf_provider
        login_path: /admin/login
        check_path: /admin/login_check
        default_target_path: /admin
      logout:
        path: /admin/logout
        target: /
      anonymous: true

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

现在我想创建一个登录表单.

Now i want to create a login form.

据我了解,我们必须创建一个登录操作(因为如果我们不symfony2抛出未找到的路由异常).

As i understand, we have to create an login action (because if we don't symfony2 throws a route not found exception).

用于管理员登录的路由:

Routing for admin login:

admin_login:
    pattern: /admin/login
    defaults: { _controller: CSCommonBundle:Admin/Default:login }

用于显示登录页面的控制器:

Controller for showing login page:

public function loginAction()
{
    return $this->render('CSCommonBundle:Admin/Login:login.html.twig');
}

登录表单:

<form class="form-signin" action="/admin/login_check" method="post">
    <h2 class="form-signin-heading">Giriş Yapın</h2>
    <div class="login-wrap">
        <input type="text" name="_username" id="username" class="form-control" placeholder="{% trans %}Kullanıcı Adı{% endtrans %} / {% trans %}E-Posta{% endtrans %}" autofocus>
        <input type="password" name="_password" id="password" class="form-control" placeholder="{% trans %}Şifre{% endtrans %}">
        <label class="checkbox">
            <input type="checkbox" id="remember_me" name="_remembe_me" value="on"> Beni Hatırla
        </label>
        <button class="btn btn-lg btn-login btn-block" type="submit">{% trans %}Giriş Yap{% endtrans %}</button>
    </div>
</form>

现在,我有几个问题;

Now, i have a few questions about;

  1. 我们如何获得登录错误?
  2. 是否正在创建像fosuserbundle这样的登录表单?
  3. 我们可以为其他用户类型创建另一个登录页面吗?
  4. 提供/admin/login_check表格(与security.yml中的内容相同)是否有效?

请向我解释这些.我迷路了,陷入困境.

Please explain these to me. I'm lost and stuck at this.

推荐答案

我强烈建议您说服FOSUserBundle文档正确执行此操作.呈现您的登录操作的控制器实际上应该是FOSUserBundle SecurityControlle loginAction.从安全角度来看,这将正确处理登录,还将为您处理登录错误消息.

I would highly recommend persuing the FOSUserBundle documentation to do this correctly. The controller to render your login action should actually be the FOSUserBundle SecurityControlle loginAction. This will handle logging in correctly from a security perspective and will also handle your login error messaging for you.

1.我相信FOSUserBundle使用Flash消息传递来解决登录错误,因此您需要在模板中实现此功能.

1.I believe FOSUserBundle uses flash messaging for the login errors so you would need to implement this in your template.

2.我建议您查阅FOS的文档,他们准确地列出了如何创建新的表单而不是其表单. https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master /Resources/doc/overriding_templates.md

2.I would recommend checking out FOS' documentation, they lay out exactly how to create a new form to use instead of their form. https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_templates.md

3.是的,可以.过去,我通过扩展默认的FOSUserBundle SecurityController并覆盖renderLogin()方法来做到这一点. https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller /SecurityController.php .这将允许您指定不同的登录模板.例如,您可以添加一张支票,以查看用户前往的路线是这样的:

3.Yes you can. I have done this in the past by extending the default FOSUserBundle SecurityController and overriding the renderLogin() method. https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/SecurityController.php. This will allow you to specify different login templates. For example, you could add a check to see what route the user is heading to like this:

protected function renderLogin(array $data) {

    // get target path
    if ( $this->getSession()->get( '_security.main.target_path' ) ) {
        $targetPath = $this->getSession()->get( '_security.main.target_path' );
    } else {
        $targetPath = $this->get('router')->generate( 'default_route' );
    }

    $template = 'AcmeBundle:Security:loginOne.html.twig';

    // if admin
    if( strstr( $targetPath, '/admin' ) !== false ) {
        $template = 'AcmeBundle:Security:loginTwo.html.twig';
    }

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

我建议您使用主表单和Twig块,您可以在所有其他登录表单中覆盖它们,这样,您只需更改表单的外观即可,而不更改整个表单标记本身.如果要扩展安全控制器,则可能需要重新创建Resources/config/routing/security.xml中的登录路由才能使用它.

I would recommend using a master form and using Twig blocks that you can override in all your other login forms, that way your only changing the skin of the form and not the entire form markup itself. If you do extend the security controller you may need to re-create the login routes that are in Resources/config/routing/security.xml to use it.

4.是的,可以,但是有一些方法可以解决.我不会在表单中硬编码您的login_check路径,而是创建一条路由并生成该路由.对于客户端来说,它是相同的URL,但是将更易于维护.该检查的默认FOS路由为"fos_user_security_check".您还应该在security.yml文件中使用路由,而不是对路径进行硬编码.这样,您只需更改路线即可更改路径.

4.Yes, this is ok, but there are routes for this. I would not hard-code your login_check path in the form but create a route and generate that. To the client it's the same url but it will be easier to maintain. The default FOS route for the check is 'fos_user_security_check'. You should also use the route in your security.yml file instead of hardcoding the path. This way you can change the path simply by changing the routes.

这应允许您将管理员登录和前端登录使用不同的模板,而无需重新创建整个表单和安全控制器.让FOS管理安全性,只需扩展并覆盖您所需的内容即可.

This should allow you to use different templates for an admin login vs. a front end login without re-creating the whole form and security controller. Let FOS manage the security, just extend and override what you need.

这篇关于FosUserBundle不同的登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆