简体   繁体   English

Symfony2 + FosUserBundle:使用预设的用户名登录

[英]Symfony2 + FosUserBundle : login with username already preset

i'm having this project which allows a user to login on multiple tablets. 我有一个允许用户在多个平板电脑上登录的项目。

on my interface i have list of users who are "online" ( last seen < 2 minutes ) . 在我的界面上,我有“在线”用户列表(最近一次出现是在<2分钟内)。

i would like , by selecting one of these, be redirected to a login page where the username is already filled or set, and just have to specify the user's password. 我想通过选择其中之一,将其重定向到登录页面,在该登录页面中,用户名已被填充或设置,而只需指定用户的密码即可。

This feature would allows user to simply switch between the differents tablets. 此功能将允许用户在不同平板电脑之间轻松切换。

for the moment i made my online user list works and redirect to the fos user bundle overrided login form. 目前,我使我的在线用户列表有效,并重定向到fos用户捆绑替代的登录表单。

i think i should override a second time the login form and may be the controller, but i actually wondering if there is already a built-in feature. 我认为我应该第二次覆盖登录表单,并且可能是控制器,但我实际上想知道是否已经有内置功能。

Okay here is my answer ( which works of course ) 好的,这是我的答案(当然可以)

1) you need to override fosUserBundle SecurityController and routing to add your custom code : 1)您需要重写fosUserBundle SecurityController并路由以添加您的自定义代码:

  Copy from FOS\UserBundle\Controller\SecurityController
  To your bundle ( for example : Acme\UserBundle\Controller

  Copy from FOS\UserBundle\Ressources\config\routing\security.xml
  To your bundle ( for example : Acme\UserBundle\Ressources\config\routing\security.xml

2) in your app\\config\\routing.yml 2)在您的app \\ config \\ routing.yml中

  Change : 
        fos_user_security:
            resource: "@FOSUserBundle/Resources/config/routing/security.xml"
  To :
        fos_user_security:
            resource: "@ACMEUserBundle/Resources/config/routing/security.xml"

( ACME here is you app name ) (ACME这是您的应用程式名称)

3) in your Acme\\UserBundle\\Controller\\SecurityController.php 3)在您的Acme \\ UserBundle \\ Controller \\ SecurityController.php中

  Add the following ( after line 43 ):
    /**
     * Custom login if we want to prefill login with an online user email
     */
    if ( $lastUsername === null && $request->get('email') != '' ) 
        $lastUsername            = $request->get('email');

Now we are able to use login in both standard mode and custom mode which allow to switch between online users from different devices without loosing security . 现在,我们可以在标准模式和自定义模式下使用登录名,从而可以在不损失安全性的情况下在不同设备的在线用户之间进行切换。

im conscious that having an email in get parameter isn't very clean code, but this project is running through an webview in a mobile app and the url is never displayed . 我意识到在get参数中包含电子邮件不是很干净的代码,但是此项目正在通过移动应用程序中的webview运行,并且从未显示url。

the code should be updated in case of post method . 如果使用post方法,则应更新代码。

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

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