Search code examples
symfony-2.3

login an application according roles of an entity


I have a User class with roles (ROLE_ADMIN, ...) and a Class Group, the relationship between them is: Group(OneToMany) ------ User(ManyToOne). I remove the attribute "role" of the User class to put in the Group class, so the Group class contains the roles

when I create a user on my form

(the form contains a chexbox derive the group class with different roles, and at the database everything is normal, group_id is in the User table)

and I'm trying to connect me with this user I get an error:

"Catchable Fatal Error: Argument Passed 4 to Symfony \ Component \ Security \ Core \ Authentication \ Token ..."

How do I connect according the identifier of the entity Group (group_id) because group_id match a unique role?

Thank you in advance.(Excuse my English)


Solution

  • Probably you should create in your user entity method getRoles which return all your roles based on your assignment to groups something like that:

    public function getRoles()
    {
         $roles = array();
         foreach ($this->getGroups() as $group) {
               foreach($group->getRoles() as $role)
                      $roles[] = $role;
         }
         return $roles;
    }
    

    Your User Entity muest return array of User's roles - interface required it