198 lines
4.8 KiB
PHP
198 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Entity;
|
|
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
use ApiPlatform\Metadata\GetCollection;
|
|
use ApiPlatform\Metadata\Get;
|
|
use ApiPlatform\Metadata\Post;
|
|
use ApiPlatform\Metadata\Put;
|
|
use ApiPlatform\Metadata\Delete;
|
|
use App\Repository\RoleRepository;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Component\Serializer\Annotation\Groups;
|
|
|
|
#[ORM\Entity(repositoryClass: RoleRepository::class)]
|
|
#[ORM\Table(name: 'roles')]
|
|
#[ApiResource(
|
|
operations: [
|
|
new GetCollection(stateless: false),
|
|
new Get(stateless: false),
|
|
new Post(stateless: false, security: "is_granted('ROLE_ADMIN')"),
|
|
new Put(stateless: false, security: "is_granted('ROLE_ADMIN')"),
|
|
new Delete(stateless: false, security: "is_granted('ROLE_ADMIN')")
|
|
],
|
|
normalizationContext: ['groups' => ['role:read']],
|
|
denormalizationContext: ['groups' => ['role:write']],
|
|
security: "is_granted('ROLE_USER')"
|
|
)]
|
|
class Role
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column]
|
|
#[Groups(['role:read', 'user:read'])]
|
|
private ?int $id = null;
|
|
|
|
#[ORM\Column(length: 100, unique: true)]
|
|
#[Groups(['role:read', 'role:write', 'user:read'])]
|
|
private ?string $name = null;
|
|
|
|
#[ORM\Column(length: 255, nullable: true)]
|
|
#[Groups(['role:read', 'role:write', 'user:read'])]
|
|
private ?string $description = null;
|
|
|
|
#[ORM\Column]
|
|
#[Groups(['role:read', 'role:write', 'user:read'])]
|
|
private bool $isSystem = false;
|
|
|
|
/**
|
|
* @var Collection<int, User>
|
|
*/
|
|
#[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'userRoles')]
|
|
private Collection $users;
|
|
|
|
/**
|
|
* @var Collection<int, RolePermission>
|
|
*/
|
|
#[ORM\OneToMany(targetEntity: RolePermission::class, mappedBy: 'role', cascade: ['persist', 'remove'], orphanRemoval: true)]
|
|
#[Groups(['role:read', 'role:write'])]
|
|
private Collection $permissions;
|
|
|
|
#[ORM\Column]
|
|
private ?\DateTimeImmutable $createdAt = null;
|
|
|
|
#[ORM\Column(nullable: true)]
|
|
private ?\DateTimeImmutable $updatedAt = null;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->users = new ArrayCollection();
|
|
$this->permissions = new ArrayCollection();
|
|
$this->createdAt = new \DateTimeImmutable();
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getName(): ?string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function setName(string $name): static
|
|
{
|
|
$this->name = $name;
|
|
return $this;
|
|
}
|
|
|
|
public function getDescription(): ?string
|
|
{
|
|
return $this->description;
|
|
}
|
|
|
|
public function setDescription(?string $description): static
|
|
{
|
|
$this->description = $description;
|
|
return $this;
|
|
}
|
|
|
|
public function isSystem(): bool
|
|
{
|
|
return $this->isSystem;
|
|
}
|
|
|
|
public function setIsSystem(bool $isSystem): static
|
|
{
|
|
$this->isSystem = $isSystem;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, User>
|
|
*/
|
|
public function getUsers(): Collection
|
|
{
|
|
return $this->users;
|
|
}
|
|
|
|
public function addUser(User $user): static
|
|
{
|
|
if (!$this->users->contains($user)) {
|
|
$this->users->add($user);
|
|
$user->addUserRole($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeUser(User $user): static
|
|
{
|
|
if ($this->users->removeElement($user)) {
|
|
$user->removeUserRole($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, RolePermission>
|
|
*/
|
|
public function getPermissions(): Collection
|
|
{
|
|
return $this->permissions;
|
|
}
|
|
|
|
public function addPermission(RolePermission $permission): static
|
|
{
|
|
if (!$this->permissions->contains($permission)) {
|
|
$this->permissions->add($permission);
|
|
$permission->setRole($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removePermission(RolePermission $permission): static
|
|
{
|
|
if ($this->permissions->removeElement($permission)) {
|
|
if ($permission->getRole() === $this) {
|
|
$permission->setRole(null);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getCreatedAt(): ?\DateTimeImmutable
|
|
{
|
|
return $this->createdAt;
|
|
}
|
|
|
|
public function setCreatedAt(\DateTimeImmutable $createdAt): static
|
|
{
|
|
$this->createdAt = $createdAt;
|
|
return $this;
|
|
}
|
|
|
|
public function getUpdatedAt(): ?\DateTimeImmutable
|
|
{
|
|
return $this->updatedAt;
|
|
}
|
|
|
|
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): static
|
|
{
|
|
$this->updatedAt = $updatedAt;
|
|
return $this;
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
return $this->name ?? '';
|
|
}
|
|
}
|