FOSUserBundle验证所选用户

时间:2013-07-11 10:01:10

标签: symfony fosuserbundle

我会以非常直接的方式验证用户(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 ...

对用户进行身份验证

由于

1 个答案:

答案 0 :(得分:3)

这是验证演示用户的方法,例如programmatic:

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中的第三个参数必须是防火墙名称。