b85411
b85411

Reputation: 10000

Symfony2 after log in says: Authenticated: NO

Authenticated: No appears in the Symfony2 dev toolbar after a successful login.

In my success handler I can access $token->getRoles() and see the role objects assigned to the user so it appears to be serializing okay.

So I'm not sure why it's not authenticating.

Here is my security.yml:

security:
encoders:
    FixedApp\Model\User:
      algorithm:        sha1
      encode_as_base64: false
      iterations:       1

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

providers:
    administrators:
        entity: { class: FixedApp\Model\User, property: username }

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt|error)|css|images|js)/
        security: false

    login:
        pattern:  ^/$
        security: false

    secured_area:
        pattern: ^/
        form_login:
            check_path: fixed_app_authentication_login
            login_path: fixed_app_homepage
            username_parameter: form[username]
            password_parameter: form[password]
            default_target_path: fixed_app_hub_homepage
            always_use_default_target_path: true
            success_handler: security.authentication.success_handler
        logout:
            path: fixed_app_authentication_logout
            target: fixed_app_homepage

access_control:
- { path: ^/log-in$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /users/edit, roles: ROLE_ADMIN }

It not authenticating is a problem, because when I go to /users/edit as an admin it says Access Denied. So I need to figure out what is going on here. Any ideas would be most appreciated.

Upvotes: 0

Views: 564

Answers (1)

b85411
b85411

Reputation: 10000

I saw a number of other people online with this same problem but I've never seen a solution put anywhere before - so hopefully this helps someone.

In UserRole.php class I was missing this function:

/**
 * @see RoleInterface
 */
public function getRole()
{
    return $this->role;
}

And secondly, in User.php class I made it implement EquatableInterface:

use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\UserInterface;

...

class User implements AdvancedUserInterface, EquatableInterface, \Serializable
{
    ...

    public function isEqualTo(UserInterface $user)
    {
        if ($this->getId() == $user->getId())
        {
            return true;
        }

        else
        {
            return false;
        }
    }

And then it started working. The Symfony toolbar button went green, it says Authenticated: Yes and it lists all the roles for that user.

Upvotes: 2

Related Questions