Symfony对用户有很多作用

shu*_*van 2 php security symfony doctrine-orm

我有问题,早期我的实体有一个角色,这对项目来说已经足够了.但是现在我的实体需要有很多角色,比如ROLE_DEVELOPER,ROLE_COMPANY,当用户为实体做一些操作时添加新角色.如何最好地解决这种情况,创建新实体或更改数组中的字段角色或什么?我在表用户中有实体,并且有一个ROLE_USER,现在用户想要创建团队,团队需要角色ROLE_TEAM,以及如何设置新角色?然后用户想要创建客户端实体,我需要设置ROLE_CLIENT.如果我创建ManyToMany关系我的用户,谁有多个角色身份验证?在用户配置文件中我想要创建,如果用户有ROLE_TEAM - 可见按钮用于转到配置文件团队,可以创建他的多个角色,我需要foreach数组角色?

/**
 * Users
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\UsersRepository")
 * @ExclusionPolicy("all")
 */

 class Users 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="user_role",
 *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 * )
 *
 * @var ArrayCollection $userRoles
 */
protected $userRoles;

/**
 * Constructor
 */
public function __construct()
{
    $this->userRoles = new ArrayCollection();
}

/**
 * Get salt
 *
 * @return string
 */
public function getSalt()
{
    return '';
}

/**
 * @inheritDoc
 */
public function eraseCredentials() { }

/**
 * ?????? ??? ????? ????????????.
 *
 * @return ArrayCollection A Doctrine ArrayCollection
 */
public function getUserRoles()
{
    return $this->userRoles;
}

/**
 * ?????? ??? ??????? ?????.
 *
 * @return array An array of Role objects
 */
public function getRoles()
{
    return $this->getUserRoles()->toArray();
}
}
Run Code Online (Sandbox Code Playgroud)

实体角色

/**
* @ORM\Entity
* @ORM\Table(name="role")
*/
class Role implements RoleInterface
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 *
 * @var integer $id
 */
protected $id;

/**
 * @ORM\Column(type="string", length=255)
 *
 * @var string $name
 */
protected $name;

/**
 * @ORM\Column(type="datetime", name="created_at")
 *
 * @var DateTime $createdAt
 */
protected $createdAt;

/**
 * ?????? ??? id.
 *
 * @return integer The id.
 */
public function getId()
{
    return $this->id;
}

/**
 * ?????? ??? ???????? ????.
 *
 * @return string The name.
 */
public function getName()
{
    return $this->name;
}

/**
 * ?????? ??? ???????? ????.
 *
 * @param string $value The name.
 */
public function setName($value)
{
    $this->name = $value;
}

/**
 * ?????? ??? ???? ???????? ????.
 *
 * @return DateTime A DateTime object.
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * ??????????? ??????
 */
public function __construct()
{
    $this->createdAt = new \DateTime();
}

/**
 * ?????????? ??????, ?????????? ??????????? RoleInterface.
 *
 * @return string The role.
 */
public function getRole()
{
    return $this->getName();
}
}
Run Code Online (Sandbox Code Playgroud)

现在我装箱但有错误:

    Artel\ProfileBundle\Entity\Role:
role0:
name: 'ROLE_FREELANCER'
role1:
name: 'ROLE_COMPANY'
role2:
name: 'ROLE_DEVELOPER'

    Artel\ProfileBundle\Entity\Users:
users0:
........
userRoles: @role0

users{1..100}:
.......
userRoles: 2x @role*

[Symfony\Component\Debug\Exception\ContextErrorException]                                                                                                                                                                                                
Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections  \ArrayCollection::__construct() must be of the type array, object given, called in /var/www/aog-code/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605 and defined  
Run Code Online (Sandbox Code Playgroud)

Ser*_*zzo 5

首先,您可以使用分层角色,因此您可以添加以下内容:

# app/config/security.yml
security:
    # ...

    role_hierarchy:
        ROLE_DEVELOPER:       ROLE_USER
        ROLE_COMPANY: [ROLE_ADMIN, ROLE_DEVELOPER]
Run Code Online (Sandbox Code Playgroud)

这意味着如果您的用户有ROLE_COMPANY,他还有ROLE_ADMIN,ROLE_DEVELOPERROLE_USER(因为ROLE_DEVELOPERROLE_USER)

如果您希望为每个用户分别设置角色,则可以创建角色实体,该实体应该实现Symfony\Component\Security\Core\Role\RoleInterface.

然后,您可以在RoleUser之间创建多对多引用.

如果您有任何疑问,请告诉我.