Symfony many role for user

Wednesday, November 4, 2015

I have problem, early my entity have one role and this is sufficiently for project. But now my entity need have many role like ROLE_DEVELOPER, ROLE_COMPANY and when user doing some operation for entity add new role. And how best away for this situation, create new entity or change field role in array or what ?



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

/**
* @Gedmo\Slug(fields={"firstName", "lastName"}, separator="-", updatable=true)
* @ORM\Column(name="name", type="string", options={"default": "Company"}, length=255, nullable=false)
* @Assert\Length(min=3, max=255)
*/
protected $username;

/**
* @var string
*
* @ORM\Column(name="password", type="string", length=80, nullable=true)
*/
protected $password;

/**
* @ORM\ManyToMany(targetEntity="Role")
* @ORM\JoinTable(name="security_users_roles")
*/
protected $roles;

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

//... existing setters/getters

/**
* Returns an ARRAY of Role objects with the default Role object appended.
* @return array
*/
public function getRoles()
{
return array_merge( $this->roles->toArray(), array( new Role( parent::ROLE_DEFAULT ) ) );
}

/**
* Returns the true ArrayCollection of Roles.
* @return Doctrine\Common\Collections\ArrayCollection
*/
public function getRolesCollection()
{
return $this->roles;
}

/**
* Pass a string, get the desired Role object or null.
* @param string $role
* @return Role|null
*/
public function getRole( $role )
{
foreach ( $this->getRoles() as $roleItem )
{
if ( $role == $roleItem->getRole() )
{
return $roleItem;
}
}
return null;
}

/**
* Pass a string, checks if we have that Role. Same functionality as getRole() except returns a real boolean.
* @param string $role
* @return boolean
*/
public function hasRole( $role )
{
if ( $this->getRole( $role ) )
{
return true;
}
return false;
}
/**
* Adds a Role OBJECT to the ArrayCollection. Can't type hint due to interface so throws Exception.
* @throws Exception
* @param Role $role
*/
public function addRole( $role )
{
if ( !$role instanceof Role )
{
throw new \Exception( "addRole takes a Role object as the parameter" );
}

if ( !$this->hasRole( $role->getRole() ) )
{
$this->roles->add( $role );
}
}

/**
* Pass a string, remove the Role object from collection.
* @param string $role
*/
public function removeRole( $role )
{
$roleElement = $this->getRole( $role );
if ( $roleElement )
{
$this->roles->removeElement( $roleElement );
}
}

/**
* Pass an ARRAY of Role objects and will clear the collection and re-set it with new Roles.
* Type hinted array due to interface.
* @param array $roles Of Role objects.
*/
public function setRoles( array $roles )
{
$this->roles->clear();
foreach ( $roles as $role )
{
$this->addRole( $role );
}
}

/**
* Directly set the ArrayCollection of Roles. Type hinted as Collection which is the parent of (Array|Persistent)Collection.
* @param Doctrine\Common\Collections\Collection $role
*/
public function setRolesCollection( Collection $collection )
{
$this->roles = $collection;
}
}


entity Role



class Role implements RoleInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", name="role", unique="true", length="70")
*/
private $role;
/**
* Populate the role field
* @param string $role ROLE_FOO etc
*/
public function __construct( $role )
{
$this->role = $role;
}
/**
* Return the role field.
* @return string
*/
public function getRole()
{
return $this->role;
}
/**
* Return the role field.
* @return string
*/
public function __toString()
{
return (string) $this->role;
}
}

0 comments:

Post a Comment