1
votes

I'm trying to configure authentication on symfony2 with this configuration:

Security.yml

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        in_memory:
            memory:
                users:
                    user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                    admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }

    firewalls:
        admin_area:
            pattern:   ^/admin
            provider:  in_memory
            anonymous: ~
            form_login:
                login_path: login
                check_path: login_check
            logout:
                path:   /logout
                target: /  

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, roles: ROLE_ADMIN }

/src/MyBundle/Resources/Routing.yml

ies_cierva_encuesta_backend_admin:
    pattern:  /admin
    defaults: { _controller: Bundle:Default:admin }

login:
    pattern:  /login
    defaults: { _controller: Bundle:Login:login }

login_check:
    pattern:  /login_check  

logout:
    pattern:  /logout

src/Bundle/Controller/LoginController.php

<?php

namespace ...

use ...

class LoginController extends Controller {

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

        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(
            'Bundle:Security:login.html.twig',
            array(
                // last username entered by the user
                'last_username' => $session->get(SecurityContext::LAST_USERNAME),
                'error'         => $error,
            )
        );
    }
}

I'm getting this error:

"Unable to find the controller for path "/login_check". Maybe you forgot to add the matching route in your routing configuration?"

If I'm not wrong, this route doesn't need a Controller...

1

1 Answers

1
votes

In http://symfony.com/doc/current/book/security.html, it is mentioned that

"Make sure that your check_path URL (e.g. /login_check) is behind the firewall you're using for your form login".

But the /login_check isn't behind the same firewall which you are using for form login.

firewalls:
    admin_area:
        pattern:   ^/admin
        provider:  in_memory
        anonymous: ~
        form_login:
            login_path: login
            check_path: login_check
        logout:
            path:   /logout
            target: /  

In the above configuration, pattern path "login_check" doesn't match "^/admin" pattern. Change the pattern accordingly to make it work.