From f9ce55f968e92296e28b6a93e63a0de4b7c0ee21 Mon Sep 17 00:00:00 2001 From: Alessandro Tagliapietra Date: Wed, 31 Oct 2012 11:13:05 +0100 Subject: [PATCH 1/4] Updated "Load security users from database" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I've updated the "How to load Security Users from the Database" cookbook to include fix this error: PHP Fatal error:  Call to a member function getRole() on a non-object in /vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php on line 47 which is happening in PHP > 5.4. The fix is taken by this issue: https://github.com/symfony/symfony/issues/3691#issuecomment-9831914 --- cookbook/security/entity_provider.rst | 58 ++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/cookbook/security/entity_provider.rst b/cookbook/security/entity_provider.rst index 8a23e5b0d3c..b29287a3215 100644 --- a/cookbook/security/entity_provider.rst +++ b/cookbook/security/entity_provider.rst @@ -429,7 +429,7 @@ returns the list of related groups:: use Doctrine\Common\Collections\ArrayCollection; // ... - class User implements AdvancedUserInterface + class User implements AdvancedUserInterface, \Serializable { /** * @ORM\ManyToMany(targetEntity="Group", inversedBy="users") @@ -448,6 +448,36 @@ returns the list of related groups:: { return $this->groups->toArray(); } + + /** + * @see \Serializable::serialize() + */ + public function serialize() + { + return \serialize(array( + $this->id, + $this->username, + $this->email, + $this->salt, + $this->password, + $this->isActive + )); + } + + /** + * @see \Serializable::unserialize() + */ + public function unserialize($serialized) + { + list ( + $this->id, + $this->username, + $this->email, + $this->salt, + $this->password, + $this->isActive + ) = \unserialize($serialized); + } } The ``AcmeUserBundle:Group`` entity class defines three table fields (``id``, @@ -468,7 +498,7 @@ that forces it to have a ``getRole()`` method:: * @ORM\Table(name="acme_groups") * @ORM\Entity() */ - class Group implements RoleInterface + class Group implements RoleInterface, \Serializable { /** * @ORM\Column(name="id", type="integer") @@ -506,6 +536,30 @@ that forces it to have a ``getRole()`` method:: { return $this->role; } + + /** + * @see \Serializable::serialize() + */ + public function serialize() + { + return \serialize(array( + $this->id, + $this->name, + $this->role + )); + } + + /** + * @see \Serializable::unserialize() + */ + public function unserialize($serialized) + { + list( + $this->id, + $this->name, + $this->role + ) = \unserialize($serialized); + } } To improve performances and avoid lazy loading of groups when retrieving a user From 4a77e332b9a0519e4f8e60ea83f31fadefdfede5 Mon Sep 17 00:00:00 2001 From: Alessandro Tagliapietra Date: Wed, 31 Oct 2012 16:08:23 +0100 Subject: [PATCH 2/4] Updated "Load security users from database" --- cookbook/security/entity_provider.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cookbook/security/entity_provider.rst b/cookbook/security/entity_provider.rst index b29287a3215..dc462b34e19 100644 --- a/cookbook/security/entity_provider.rst +++ b/cookbook/security/entity_provider.rst @@ -454,13 +454,13 @@ returns the list of related groups:: */ public function serialize() { - return \serialize(array( + return serialize(array( $this->id, $this->username, $this->email, $this->salt, $this->password, - $this->isActive + $this->isActive, )); } @@ -475,8 +475,8 @@ returns the list of related groups:: $this->email, $this->salt, $this->password, - $this->isActive - ) = \unserialize($serialized); + $this->isActive, + ) = unserialize($serialized); } } @@ -542,10 +542,10 @@ that forces it to have a ``getRole()`` method:: */ public function serialize() { - return \serialize(array( + return serialize(array( $this->id, $this->name, - $this->role + $this->role, )); } @@ -557,8 +557,8 @@ that forces it to have a ``getRole()`` method:: list( $this->id, $this->name, - $this->role - ) = \unserialize($serialized); + $this->role, + ) = unserialize($serialized); } } From e6791dc05ae3e673880f1c4186fdb109d04b32f4 Mon Sep 17 00:00:00 2001 From: Alessandro Tagliapietra Date: Sat, 17 Nov 2012 09:58:44 +0100 Subject: [PATCH 3/4] Using id for serializing, refreshUser with id Serialize with only the id field, refreshed user is called by id --- cookbook/security/entity_provider.rst | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/cookbook/security/entity_provider.rst b/cookbook/security/entity_provider.rst index dc462b34e19..919a39e51ac 100644 --- a/cookbook/security/entity_provider.rst +++ b/cookbook/security/entity_provider.rst @@ -368,7 +368,7 @@ The code below shows the implementation of the throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class)); } - return $this->loadUserByUsername($user->getUsername()); + return $this->findOneById($user->getId()); } public function supportsClass($class) @@ -456,11 +456,6 @@ returns the list of related groups:: { return serialize(array( $this->id, - $this->username, - $this->email, - $this->salt, - $this->password, - $this->isActive, )); } @@ -471,11 +466,6 @@ returns the list of related groups:: { list ( $this->id, - $this->username, - $this->email, - $this->salt, - $this->password, - $this->isActive, ) = unserialize($serialized); } } From e686daa3cc7d02506a8fe95d558809dd90177874 Mon Sep 17 00:00:00 2001 From: Alessandro Tagliapietra Date: Wed, 21 Nov 2012 10:32:14 +0100 Subject: [PATCH 4/4] Remove serialisation from the Group entity --- cookbook/security/entity_provider.rst | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/cookbook/security/entity_provider.rst b/cookbook/security/entity_provider.rst index 919a39e51ac..ca7186bc95e 100644 --- a/cookbook/security/entity_provider.rst +++ b/cookbook/security/entity_provider.rst @@ -488,7 +488,7 @@ that forces it to have a ``getRole()`` method:: * @ORM\Table(name="acme_groups") * @ORM\Entity() */ - class Group implements RoleInterface, \Serializable + class Group implements RoleInterface { /** * @ORM\Column(name="id", type="integer") @@ -526,30 +526,6 @@ that forces it to have a ``getRole()`` method:: { return $this->role; } - - /** - * @see \Serializable::serialize() - */ - public function serialize() - { - return serialize(array( - $this->id, - $this->name, - $this->role, - )); - } - - /** - * @see \Serializable::unserialize() - */ - public function unserialize($serialized) - { - list( - $this->id, - $this->name, - $this->role, - ) = unserialize($serialized); - } } To improve performances and avoid lazy loading of groups when retrieving a user