Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception : Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() must return a string or NULL

I am not able to authenticate in symfony2 with the 'Employee' entity as it contains many mapping with other entities in my project. some of my mapping is as follows:

/**
 * @var EmployeeDesignation
 *
 * @ORM\ManyToOne(targetEntity="EmployeeDesignation")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="employee_designation_id", referencedColumnName="id")
 * })
 */
private $employeeDesignation;

/**
 * @var EmployeeDesignation
 *
 * @ORM\ManyToOne(targetEntity="EmployeeType")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="employee_type_id", referencedColumnName="id")
 * })
 */
private $employeeType;

Authentication works fine without any mapping. I have tried with 'Serialize()' and 'Unserialize()' methods in it like below:

class Employee implements AdvancedUserInterface, \Serializable {
    /**
     * serialize the username
     * @return serialize
     */
    public function serialize() {
        return serialize($this->emailOfficial);
    }

    /**
     * unserialize
     * @param $data
     */
    public function unserialize($data) {
        $this->em = unserialize($data);
    }
}

I am getting the following error after doing the above method:

You cannot refresh a user from the EntityUserProvider that does not contain an identifier. The user object has to be serialized with its own identifier mapped by Doctrine. 

I have tried this way so as to get rid of the previous error, which is as follows:

Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::serialize() must return a string or NULL 

So, can anybody please suggest a way to overcome from this problem?

like image 202
Rajesh Vasani Avatar asked Mar 16 '12 13:03

Rajesh Vasani


1 Answers

I have encountered something similar, and after some research, I tried the same things as you did.

But at some point, I found out that by setting the __sleep method, every thing worked fine.

class User implements PlayerInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
...

    public function __sleep()
    {
        return array('id');
    }
...
  • Make sure that the field which is defined as @ORM\Id is part of the returned array.
  • Make sure to drop the browser cookie, since it uses the session.

I don't know exactly why it causes this when setting up a new association (mine was a ManyToMany), but It probably originate from this place:

// see Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse()
...
        $event->getRequest()
            ->getSession()
            ->set('_security_'.$this->contextKey, serialize($token));
...

Hope this could help someone.

Edit:

References:

  • http://forum.symfony-project.org/viewtopic.php?f=23&t=35764
  • http://translate.google.com/translate?hl=en&sl=es&u=http://jonsegador.com/2012/03/error-con-symfony2-you-cannot-refresh-a-user-from-the-entityuserprovider-that-does-not-contain-an-identifier/
like image 178
yvoyer Avatar answered Oct 01 '22 05:10

yvoyer