Faery
Faery

Reputation: 4650

Security and routes - Symfony2

I want the index page for my project to be a login form with a link for registration below it and unlogged visitors should be able to see only the login form with route / and the register page with route /register. When the log I want they to be redirected to the home page with route /home. I tried some things and it's working in the dev environment (although having some troubles with the toolbar - Symfony2 - dev environment) but when I switch to prod env, the browser says: "The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies."

Here are my files:

security.yml

security:
    encoders:
        EM\MyFriendsBundle\Entity\User:
            algorithm:        sha1
            encode_as_base64: false
            iterations:       1

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER

providers:
    administrators:
        entity: { class: EMMyFriendsBundle:User }

firewalls:
    secured_area:
        pattern:    ^/
        anonymous: ~
        form_login:
            login_path:  /login
            check_path:  /login_check
            default_target_path: /home

access_control:
    - { path: ^/home, roles: ROLE_ADMIN }

routing.yml

login_display:
    pattern: /
    defaults: { _controller: EMMyFriendsBundle:Welcome:display }

login:
    pattern:   /login
    defaults:  { _controller: EMMyFriendsBundle:Welcome:login}

login_check:
    pattern:   /login_check

register:
    pattern: /register
    defaults: { _controller: EMMyFriendsBundle:Welcome:register }

home_display:
    pattern: /home
    defaults: { _controller: EMMyFriendsBundle:Home:display }

WelcomeController.php

<?php

namespace EM\MyFriendsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;

class WelcomeController extends Controller
{
    public function displayAction()
    {
        $error=null;
        $last_username=null;
        return $this->render('EMMyFriendsBundle:Welcome:login.html.twig', array('error' => $error, 'last_username' => $last_username));
    }

    public function loginAction()
    {
        $request = $this->getRequest();
        $session = $request->getSession();

        // get the login error if there is one
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
            $session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }

        return $this->render('EMMyFriendsBundle:Welcome:login.html.twig', array(
            // last username entered by the user
            'last_username' => $session->get(SecurityContext::LAST_USERNAME),
            'error'         => $error
        ));
    }

    public function registerAction()
    {
         return $this->render('EMMyFriendsBundle:Welcome:register.html.twig');
    }
}

HomeController.php

<?php
namespace EM\MyFriendsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class HomeController extends Controller
{
    public function displayAction()
    {
        return $this->render('EMMyFriendsBundle:Home:home.html.twig');
    }
}
?>

Upvotes: 0

Views: 4052

Answers (1)

Carlos Granados
Carlos Granados

Reputation: 11351

Add:

    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }.

Upvotes: 4

Related Questions