1

我会以非常直接的方式对用户进行身份验证(FOSUserBundle,Symfony2.2)。我正在尝试一个简单的例子,但它不起作用:

...
use FOS\UserBundle\Controller\RegistrationController as RegController;
...
class DefaultController extends Controller{
...
public function indexAction(){
$route = 'first_set_profile';
$url = $this->container->get('router')->generate($route);
$response = new RedirectResponse($url);
$userManager = $this->get('fos_user.user_manager');
$userToLogIn = $userManager->findUserByEmail('aa@bb.com');
new RegController(authenticateUser($userToLogIn, $response));
...
}

此脚本正在运行,但未使用电子邮件 aa@bb.com 对用户进行身份验证...

谢谢

4

1 回答 1

3

这是您可以验证演示用户的方式,例如编程:

use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

public function demologinAction(Request $request)
{
    $userManager = $this->get('fos_user.user_manager');
    $user = $userManager->findUserByEmail('demo@example.com');

    if (!$user) {
        throw $this->createNotFoundException('No demouser found!');
    }

    $token = new UsernamePasswordToken($user, $user->getPassword(), 'main', $user->getRoles());

    $context = $this->get('security.context');
    $context->setToken($token);

    $router = $this->get('router');
    $url = $router->generate('dashboard_show');

    return $this->redirect($url);
}

中的第三个参数UsernamePasswordToken必须是防火墙名称。

于 2013-07-11T11:31:50.063 回答