新用户注册时,如果未设置角色,则自动设置默认角色(层次结构中最低的角色)。我已经通过迁移创建了“多对多”关系,并具有三个表:用户,角色, user_roles 。因此,主要目标是在user_roles
中为每个用户至少拥有一个关系。
下面列出了用户和角色的实体 用户实体
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext={
* "groups"={"read"}
* }
* )
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity("username")
* @UniqueEntity("email")
*/
class User implements UserInterface
{
public function __construct()
{
$this->roles = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=100)
* @Groups({"read"})
* @Assert\NotBlank()
* @Assert\Length(min=3, max=100)
*/
private $username;
/**
* @ORM\Column(type="string", length=150)
* @Assert\NotBlank()
* @Assert\Length(min=3, max=100)
*/
private $password;
/**
* @ORM\Column(type="string", length=150)
* @Groups({"read"})
* @Assert\NotBlank()
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="string", length=150)
* @Groups({"read"})
* @Assert\NotBlank()
* @Assert\Length(min=3, max=100)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=150)
* @Groups({"read"})
* @Assert\NotBlank()
* @Assert\Length(min=3, max=100)
*/
private $lastname;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Roles", inversedBy="users")
* @Groups({"read"})
*/
private $roles;
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getLastname(): string
{
return $this->lastname;
}
public function setLastname(string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
/**
* @return array
*/
public function getRoles(): array
{
return $this->roles->toArray();
}
/**
* @param Roles $role
* @return $this
*/
public function addRole(Roles $role) : self
{
if(!$this->roles->contains($role)) {
$this->roles[] = $role;
}
return $this;
}
/**
* @param Roles $role
* @return $this
*/
public function deleteRole(Roles $role) : self
{
if($this->roles->contains($role)) {
$this->roles->removeElement($role);
}
return $this;
}
}
角色实体:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\RolesRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* normalizationContext={
* "groups"={"read"}
* }
* )
* @ORM\Entity(repositoryClass=RolesRepository::class)
*/
class Roles
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
* @Groups({"read"})
*/
private $sysName;
/**
* @ORM\Column(type="string", length=50)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="roles")
*/
private $users;
public function getId(): ?int
{
return $this->id;
}
public function getSysName(): ?string
{
return $this->sysName;
}
public function setSysName(string $sysName): self
{
$this->sysName = $sysName;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}
答案 0 :(得分:0)
如果您不希望每个默认用户都拥有关系,则可以通过添加默认角色来扩展getRoles()
方法。
我在一个项目中做了类似的事情,看起来像这样:
public function getRoles()
{
if (count($this->getRole())) {
foreach ($this->getRole() as $role) {
$roles[] = $role->getRole();
}
} else {
$roles = ['ROLE_USER'];
}
return $roles;
}