Skip to content

Instantly share code, notes, and snippets.

@tobalsan
Last active August 29, 2015 14:04
Show Gist options
  • Save tobalsan/4ec49540aad087130c9d to your computer and use it in GitHub Desktop.
Save tobalsan/4ec49540aad087130c9d to your computer and use it in GitHub Desktop.
Symfony2: Custom equals method in user entity to refresh roles
<?php
// Just change the "equals" method of your User class like the following to enable automatic roles refresh instead of having to logout / login for roles change to take effect
class User implements UserInterface
{
// ...
/**
* Compares this user to another to determine if they are the same.
*
* @param UserInterface $user
* @return boolean True if equal, false otherwise.
*/
public function equals(UserInterface $user)
{
return
md5($user->getUsername()) == md5($this->getUsername()) &&
md5(serialize($user->getRoles())) == md5(serialize($this->getRoles()));
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment