<?php
namespace App\Entity;
use App\Repository\UserRoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=UserRoleRepository::class)
*/
class UserRole
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="user_role")
*/
private $users;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $permissions = [];
public function __construct()
{
$this->users = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setUserRole($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getUserRole() === $this) {
$user->setUserRole(null);
}
}
return $this;
}
/**
* @param string $permission
* @return bool
*/
public function hasPermission($permission): bool
{
return in_array($permission, $this->getPermissions(), true);
}
public function getPermissions(): ?array
{
return $this->permissions;
}
public function setPermissions(?array $permissions): self
{
$this->permissions = $permissions;
return $this;
}
}