一旦记录,Symfony2不会显示视图登录

时间:2016-04-12 10:30:56

标签: php symfony

我正在使用捆绑软件FOSUser来管理用户,我遇到的问题是当你签名时如果你手动输入路径/登录我重新出现表单进行签名和我想要的是如果我放在吧台浏览器/登录并记录重定向我/索引而不重新显示要登录的表单。

我可以帮忙吗?

security:
encoders:
    FOS\UserBundle\Model\UserInterface: sha512
role_hierarchy:
    ROLE_USER:       ROLE_USER
    ROLE_ADMIN:       ROLE_ADMIN
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

providers:
    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    main:
        pattern:    ^/
        form_login:
            check_path: /login_check
            login_path: /
            provider: fos_userbundle
            always_use_default_target_path: false
            default_target_path: /index

        logout:
            path:   /logout
            target: /
        anonymous: ~
        #http_basic:
        #    realm: "Secured Demo Area"

access_control:
    - { path: ^/$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, role: ROLE_SUPER_ADMIN }
    - { path: ^/index, role: ROLE_USER, requires_channel: http}
    #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

我的控制器是:

class SecurityController extends ContainerAware
{
public function loginAction()
{
    $request = $this->container->get('request');
    $session = $request->getSession();


    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } elseif (null !== $session && $session->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    } else {
        $error = '';
    }

    if ($error) {

        $error = $error->getMessage();
    }

    $lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);

    $csrfToken = $this->container->get('form.csrf_provider')->generateCsrfToken('authenticate');

    return $this->renderLogin(array(
        'last_username' => $lastUsername,
        'error'         => $error,
        'csrf_token' => $csrfToken,
    ));
}


protected function renderLogin(array $data)
{
    $template = sprintf('FOSUserBundle:Security:login.html.%s', $this->container->getParameter('fos_user.template.engine'));

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

public function checkAction()
{
    throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
}

public function logoutAction()
{
    throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
}
}

1 个答案:

答案 0 :(得分:0)

在您的登录控制器中,您可以执行类似的操作

if($this->getUser() instanceof your_user_entity){
    return $this->redirectToRoute('route_of_your_index');
}

或者

将您的安全系统编辑为自动