FOSUserBundle, the relationship between the user (roles) and the group (roles)?

I am a bit confused with the roles in the FOSUserBundle. The user object also has a role column through which we can assign several roles to the user. According to the answer posted in User Management / Roles / Groups in FOSUserBundle , we do not need separate entities or tables to manage user roles.

I also implemented the FOSUserBundle group, which also contains a set of roles, and the user can be assigned these roles. So, I already realized that we can create a group to assign a set of roles so that similar groups can be accessed using the group.

Now that I’m stuck with the goal of using groups, if the set of roles can only be controlled from the User Entity, or how can I combine roles from the User object and the Group object or something that I’m missing here?

+4
source share
1 answer

Q : what is the purpose of using groups if role management can only be managed from a user object

A : The answer can be extracted from Using Groups with FOSUserBundle :

Symfony2 , . , ( ).


Q: : ? , ?

A. , "" , (, ). , ROLE_ADMIN MANAGER, REVISER ( , ). . , , , , .


Q: User Group entity - ?

A. , "", User → Group → Role, :

:

use Doctrine\Common\Collections\ArrayCollection;

/**
 * USER ManyToMany with GROUP Unidirectional association
 * @var ArrayCollection
 * @ORM\ManyToMany(targetEntity="Group")
 * @ORM\JoinTable(name="user_groups")
 * @Assert\Valid
 */
protected $groups;

public function __construct()
{
    $this->groups = new ArrayCollection(); <-- add this line to the constructor
}

/**
 * Get all Groups added to the administrator
 * @return ArrayCollection $groups
 */
public function getGroups()
{
    return $this->groups;
}

/**
 * Add Group $group to the administrator
 *
 * @param \Group $group
 * @return void
 */
public function addGroup(Group $group)
{
    if (!$this->groups->contains($group)) {
        $this->groups->add($group);
    }
}

/**
 * Remove Group from the user
 * @param \Group $group
 * @return void
 */
public function removeGroup(Group $group)
{
    if ($this->groups->contains($group)) {
        $this->groups->removeElement($group);
    }
}

/**
 * Get all Roles added to the User
 * @return array
 */
public function getRoles()
{
    $roles = [];
    /** @var ArrayCollection $groups */
    $groups = $this->getGroups();

    foreach ($groups as $group) {
        $roles = array_merge($roles, $group->getRoles()->toArray());
    }

    $roles = array_unique($roles);

    # store the roles in the property to be serialized
    $this->roles = $roles;

    return $roles;
}

:

/**
 * GROUP ManyToMany with ROLE Unidirectional Association
 * @var ArrayCollection
 * @ORM\ManyToMany(targetEntity="Role")
 * @ORM\JoinTable(name="user_group_roles")
 * @Assert\Valid
 */
protected $roles;

public function __construct()
{
    $this->roles       = new ArrayCollection();
}

/**
 * Return all Roles added to this Group
 *
 * @return \Doctrine\Common\Collections\ArrayCollection $roles
 */
public function getRoles()
{
    return $this->roles;
}

/**
 * Add Role $role to the Group
 *
 * @param Role $role
 * @return void
 */
public function addRole(Role $role)
{
    if (! $this->roles->contains($role)) {
        $this->roles->add($role);
    }
}

/**
 * Remove Role from the Group
 *
 * @param Role $role
 * @return void
 */
public function removeRole(Role $role)
{
    if ($this->roles->contains($role)) {
        $this->roles->removeElement($role);
    }
}

. , .

+2

All Articles