AzuraCast/src/Entity/Role.php

130 lines
2.7 KiB
PHP
Raw Normal View History

2014-02-21 09:25:10 +00:00
<?php
namespace App\Entity;
2014-02-21 09:25:10 +00:00
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use App\Annotations\AuditLog;
use Doctrine\ORM\Mapping as ORM;
use OpenApi\Annotations as OA;
use Symfony\Component\Validator\Constraints as Assert;
2014-02-21 09:25:10 +00:00
/**
* @ORM\Table(name="role")
* @ORM\Entity
*
* @AuditLog\Auditable
*
* @OA\Schema(type="object")
2014-02-21 09:25:10 +00:00
*/
class Role implements \JsonSerializable
2014-02-21 09:25:10 +00:00
{
public const SUPER_ADMINISTRATOR_ROLE_ID = 1;
use Traits\TruncateStrings;
2014-02-21 09:25:10 +00:00
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @OA\Property(example=1)
* @var int
2014-02-21 09:25:10 +00:00
*/
protected $id;
/**
* @ORM\Column(name="name", type="string", length=100)
* @OA\Property(example="Super Administrator")
* @Assert\NotBlank
* @var string
*/
2014-02-21 09:25:10 +00:00
protected $name;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
* @var Collection
*/
2014-02-21 09:25:10 +00:00
protected $users;
/**
* @ORM\OneToMany(targetEntity="RolePermission", mappedBy="role")
* @OA\Property(@OA\Items)
* @var Collection
*/
protected $permissions;
/**
* Role constructor.
*/
public function __construct()
{
$this->users = new ArrayCollection;
$this->permissions = new ArrayCollection;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @AuditLog\AuditIdentifier()
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $this->_truncateString($name, 100);
}
/**
* @return Collection
*/
public function getUsers(): Collection
{
return $this->users;
}
/**
* @return Collection
*/
public function getPermissions(): Collection
{
return $this->permissions;
}
public function jsonSerialize()
{
$return = [
'id' => $this->id,
'name' => $this->name,
'permissions' => [
'global' => [],
'station' => [],
],
];
foreach($this->permissions as $permission) {
/** @var RolePermission $permission */
if ($permission->hasStation()) {
$return['permissions']['station'][$permission->getStation()->getId()][] = $permission->getActionName();
} else {
$return['permissions']['global'][] = $permission->getActionName();
}
}
return $return;
}
}