2014-02-21 10:25:10 +01:00
|
|
|
<?php
|
2020-10-15 00:19:31 +02:00
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
declare(strict_types=1);
|
2020-12-08 19:40:33 +01:00
|
|
|
|
2018-08-05 00:05:14 +02:00
|
|
|
namespace App\Entity;
|
2014-02-21 10:25:10 +01:00
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
use App\Entity\Interfaces\IdentifiableEntityInterface;
|
2017-01-24 01:35:16 +01:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2017-08-17 20:28:48 +02:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
2018-12-22 01:01:04 +01:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2019-09-04 20:00:51 +02:00
|
|
|
use JsonSerializable;
|
2021-12-28 19:26:41 +01:00
|
|
|
use OpenApi\Attributes as OA;
|
2021-05-30 20:55:26 +02:00
|
|
|
use Stringable;
|
2019-01-05 13:02:49 +01:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
2014-02-21 10:25:10 +01:00
|
|
|
|
2021-05-30 20:55:26 +02:00
|
|
|
#[
|
2021-12-19 19:50:37 +01:00
|
|
|
OA\Schema(type: "object"),
|
2021-05-30 20:55:26 +02:00
|
|
|
ORM\Entity,
|
|
|
|
ORM\Table(name: 'role'),
|
2021-05-31 03:15:34 +02:00
|
|
|
Attributes\Auditable
|
2021-05-30 20:55:26 +02:00
|
|
|
]
|
2021-07-19 07:53:45 +02:00
|
|
|
class Role implements JsonSerializable, Stringable, IdentifiableEntityInterface
|
2014-02-21 10:25:10 +01:00
|
|
|
{
|
2021-05-30 20:55:26 +02:00
|
|
|
use Traits\HasAutoIncrementId;
|
2018-04-06 23:29:27 +02:00
|
|
|
use Traits\TruncateStrings;
|
|
|
|
|
2021-12-19 19:50:37 +01:00
|
|
|
#[
|
|
|
|
OA\Property(example: "Super Administrator"),
|
|
|
|
ORM\Column(length: 100),
|
|
|
|
Assert\NotBlank
|
|
|
|
]
|
2021-05-30 20:55:26 +02:00
|
|
|
protected string $name;
|
2014-02-21 10:25:10 +01:00
|
|
|
|
2022-05-31 13:41:35 +02:00
|
|
|
/** @var Collection<int, User> */
|
2021-05-30 20:55:26 +02:00
|
|
|
#[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'roles')]
|
|
|
|
protected Collection $users;
|
2014-02-21 10:25:10 +01:00
|
|
|
|
2022-05-31 13:41:35 +02:00
|
|
|
/** @var Collection<int, RolePermission> */
|
2021-12-19 19:50:37 +01:00
|
|
|
#[
|
|
|
|
OA\Property(type: "array", items: new OA\Items()),
|
|
|
|
ORM\OneToMany(mappedBy: 'role', targetEntity: RolePermission::class)
|
|
|
|
]
|
2021-05-30 20:55:26 +02:00
|
|
|
protected Collection $permissions;
|
2017-08-17 20:28:48 +02:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2020-10-15 00:19:31 +02:00
|
|
|
$this->users = new ArrayCollection();
|
|
|
|
$this->permissions = new ArrayCollection();
|
2017-08-17 20:28:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2019-01-31 18:54:17 +01:00
|
|
|
public function setName(string $name): void
|
2017-08-17 20:28:48 +02:00
|
|
|
{
|
2020-03-29 09:16:41 +02:00
|
|
|
$this->name = $this->truncateString($name, 100);
|
2017-08-17 20:28:48 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 13:41:35 +02:00
|
|
|
/**
|
|
|
|
* @return Collection<int, User>
|
|
|
|
*/
|
2017-08-17 20:28:48 +02:00
|
|
|
public function getUsers(): Collection
|
|
|
|
{
|
|
|
|
return $this->users;
|
|
|
|
}
|
|
|
|
|
2022-05-31 13:41:35 +02:00
|
|
|
/**
|
|
|
|
* @return Collection<int, RolePermission>
|
|
|
|
*/
|
2017-08-17 20:28:48 +02:00
|
|
|
public function getPermissions(): Collection
|
|
|
|
{
|
|
|
|
return $this->permissions;
|
|
|
|
}
|
2018-12-22 01:01:04 +01:00
|
|
|
|
2020-10-15 00:19:31 +02:00
|
|
|
/**
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
public function jsonSerialize(): array
|
2018-12-22 01:01:04 +01:00
|
|
|
{
|
|
|
|
$return = [
|
2019-09-04 20:00:51 +02:00
|
|
|
'id' => $this->id,
|
|
|
|
'name' => $this->name,
|
2018-12-22 01:01:04 +01:00
|
|
|
'permissions' => [
|
|
|
|
'global' => [],
|
|
|
|
'station' => [],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2019-09-04 20:00:51 +02:00
|
|
|
foreach ($this->permissions as $permission) {
|
2018-12-22 01:01:04 +01:00
|
|
|
/** @var RolePermission $permission */
|
|
|
|
|
2021-07-19 07:53:45 +02:00
|
|
|
$station = $permission->getStation();
|
|
|
|
if (null !== $station) {
|
|
|
|
$return['permissions']['station'][$station->getIdRequired()][] = $permission->getActionName();
|
2018-12-22 01:01:04 +01:00
|
|
|
} else {
|
|
|
|
$return['permissions']['global'][] = $permission->getActionName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2021-05-30 20:55:26 +02:00
|
|
|
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
2018-08-05 00:05:14 +02:00
|
|
|
}
|