0
votes

I have a custom user provider, following the guide in:

http://symfony.com/doc/current/cookbook/security/custom_provider.html

All is working without errors, but I don't manage to access the restricted zone.

In my UserProvider class, I set $roles var to have array("ROLE_USER") and that's the permission I need to access route app/list, but when I go to app/list, Symfony redirects me to login again and again.

I've seen the debug toolbar and it results:

Username    anon.
Authenticated?   yes
Roles   { }
Token class Symfony\Component\Security\Core\Authentication\Token\AnonymousToken

My security.yml file is:

security:
firewalls:
    secured_area:
        pattern:    ^/
        anonymous: ~
        form_login: ~
        http_basic:
            realm: "Secured Demo Area"
        form_login:
            provider: webservice
            login_path: login
            check_path: login_check
            always_use_default_target_path: true
            default_target_path: listado_actas
        logout:
            path:   logout
            target: login

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

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

providers:
    webservice:
        id: webservice_user_provider

encoders:
    Symfony\Component\Security\Core\User\User: plaintext
    Actas\Gestion\UserBundle\Security\User\WebServiceUser:
        id: my.encoder.service

My UserProvider class looks like the following. I just call an XML service that gives me a TOKEN that I will store in my UserClass:

public function loadUserByUsername($username)
{
    $salt = "";
    $roles = "";
    // make a call to your webservice here

    $password = $this->request->get('_password');
    $xml_interface = new XMLInterfaceBundle();
    $token = $xml_interface->requestLogin($username, $password);

    if (strlen($token) > 10) {
        $roles = array("ROLE_USER");
        $salt  = "";
        return new WebserviceUser($username, $password, $salt, $roles, $token);
    }

    throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}

This is my UserObject in DaoAuthenticationProvider::checkAuthentication()

Actas\Gestion\UserBundle\Security\User\WebserviceUser Object
(
    [username:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] => 44886706X
    [password:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] => 44886706XkCrDP
    [salt:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] => 
    [roles:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] => Array
        (
            [0] => ROLE_ADMIN
        )

    [my_token:Actas\Gestion\UserBundle\Security\User\WebserviceUser:private] => 
)

This is my routing.yml:

xml_interface:
    resource: "@XMLInterfaceBundle/Resources/config/routing.yml"
    prefix:   /

actas:
    resource: "@ActasBundle/Resources/config/routing.yml"
    prefix:   /

login:
    pattern:   /login
    defaults:  { _controller: UserBundle:Default:login }

login_check:
    pattern:   /login_check

logout:
    pattern:   /logout
1
How did you set the role_hierarchy in security.yml? - lvarayut
I don't have it set, is it necessary? - Javier Núñez
Yes, I think so. I mentioned in my comment. - lvarayut
What version of Symfony 2 are you using? login_check really should point to a named route. I doubt if that is the problem though. - Cerad
Been awhile since I have dug into a problem like this. The form login process eventually ends up here: Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider. Might want to stick a die statement (or maybe a breakpoint) in the checkAuthentication method. It could be that the password stuff is not right or you have some trivial error going on. - Cerad

1 Answers

0
votes

Just try to set the Role_hierarchy as following:

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

Don't forget to set the role of your User object as ROLE_ADMIN, for example, in order to match the role_hierarchy.