5
votes

I am having some trouble with login and authentication in Symfony2. The exception is "$user must be an instanceof UserInterface, an object implementing a __toString method, or a primitive string."

Debugging my code I could notice that the user I am trying to log in my application can get authenticated successfully (app/log/dev.log) but the credentials var is null:

Debugging

The user variable from AbstractToken has the user data from database.

I continue debugging in the ContextListener->refreshUser function and I get these values:

Debugging2

Everything has the value null and on the Symfony\Bridge\Doctrine\Security\User\EntityUserProvider->refreshUser function returns the variable $refreshedUser as null, so when the function $token->setUser($refreshedUser) on the ContextListener class fails and throws the exception.

I write down my security.yml and my entities I am using:

security.yml:

security:
    encoders:
        Pladuch\BackBundle\Entity\BaseUser:
            algorithm: sha512
            encode_as_base64: false
            iterations: 1

    providers:
        sga:
            entity: { class: 'PladuchBackBundle:Usuario', property: username }

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

        sga:
            pattern: ^/gestion
            anonymous: ~
            form_login:
                login_path: pladuch_login_sga
                check_path: pladuch_login_check
                default_target_path: pladuch_sga_index
                csrf_provider: form.csrf_provider
                provider: sga
            logout:
                path: pladuch_logout_sga
                target: pladuch_login_sga

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

Abstract class BaseUser:

<?php

namespace Pladuch\BackBundle\Entity;


use Symfony\Component\Security\Core\User\AdvancedUserInterface;

abstract class BaseUser implements AdvancedUserInterface, \Serializable
{
    protected $id;

    protected $salt;

    protected $username;

    protected $password;

    public function __construct()
    {
        $this->isActive = true;
        $this->salt = $this->generateSalt();
    }

    public function serialize()
    {
        return serialize(array($this->id, $this->username, $this->password));
    }

    public function unserialize($serialized)
    {
        list($this->id, $this->username, $this->password) = unserialize($serialized);
    }

    public function getRoles()
    {
        return array('ROLE_ADMIN');
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function eraseCredentials()
    {
    }

    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }

    public function getSalt()
    {
        return $this->salt;
    }

    public function generateSalt()
    {
        return base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    }

    public function isAccountNonExpired()
    {
        return true;
    }

    public function isAccountNonLocked()
    {
        return true;
    }

    public function isCredentialsNonExpired()
    {
        return true;
    }

    public function isEnabled()
    {
        return true;
    }
}

Class Usuario:

<?php

namespace Pladuch\BackBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Usuario
 *
 * @ORM\Table(name="usuario",
 *            uniqueConstraints={
 *              @ORM\UniqueConstraint(name="username", columns={"username"})
 *            },
 *            indexes={@ORM\Index(name="FK_USUARIO_ROL", columns={"rol_id"})})
 * @ORM\Entity(repositoryClass="Pladuch\BackBundle\Repository\UsuarioRepository")
 */
class Usuario extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=255, nullable=false)
     */
    protected $username;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=1024, nullable=false)
     */
    protected $password;

    /**
     * @var string
     *
     * @ORM\Column(name="salt", type="string", length=1024, nullable=false)
     */
    protected $salt;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=false)
     */
    protected $email;

    /**
     * @var Rol
     *
     * @ORM\ManyToOne(targetEntity="Rol", inversedBy="id")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="rol_id", referencedColumnName="id")
     * })
     */
    protected $rol;

    /**
     * @var bool
     *
     * @ORM\Column(name="activo", type="boolean", nullable=true)
     */
    protected $activo = true;



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set username
     *
     * @param string $username
     * @return Usuario
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Get username
     *
     * @return string 
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return Usuario
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set salt
     *
     * @param string $salt
     * @return Usuario
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }

    /**
     * Get salt
     *
     * @return string 
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Usuario
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set rol
     *
     * @param Rol $rol
     * @return Usuario
     */
    public function setRol(Rol $rol = null)
    {
        $this->rol = $rol;

        return $this;
    }

    /**
     * Get rol
     *
     * @return Rol
     */
    public function getRol()
    {
        return $this->rol;
    }

    /**
     * @return array|\Symfony\Component\Security\Core\Role\Role[]
     */
    public function getRoles()
    {
        return array($this->getRol()->getRol());
    }

    /**
     * Set activo
     *
     * @param $activo
     * @return $this
     */
    public function setActivo($activo)
    {
        $this->activo = $activo;

        return $this;
    }

    /**
     * Get activo
     *
     * @return bool
     */
    public function getActivo()
    {
        return $this->activo;
    }
}

The UsuarioRepository where I implements the three functions loadUserByUsername, refreshUser and supportsClass:

class UsuarioRepository extends EntityRepository implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $q = $this->createQueryBuilder('u')
            ->where('u.username = :username')
            ->setParameter('username', $username)
            ->getQuery();

        try {
            $user = $q->getSingleResult();
        } catch (NoResultException $e) {
            $message = sprintf('Unable to find an active Usuario object identified by %s', $username);
            throw new UsernameNotFoundException($message, 0, $e);
        }

        return $user;
    }

    public function refreshUser(UserInterface $userInterface)
    {
        $class = get_class($userInterface);

        if (! $this->supportsClass($class)) {
            throw new UnsupportedUserException(sprintf('Instances of %s are not suppoted', $class));
        }
    }

    public function supportsClass($class)
    {
        return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
    }
}

Thank you for your help.

Kind regards.

P.S: I am using Symfony 2.5.6

1

1 Answers

6
votes

Ok, I forgot to add the return sentence on refreshUser function in my repository...

return $this->find($userInterface->getId());

or

return $userInterface;

$userInterface has the authenticated user so I didn't need the $this->find() method. That solved everything