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