2014-02-21 09:25:10 +00:00
|
|
|
<?php
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Entity;
|
2014-02-21 09:25:10 +00:00
|
|
|
|
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;
|
2019-08-14 23:50:53 +00:00
|
|
|
use App\Annotations\AuditLog;
|
2018-12-22 00:01:04 +00:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2019-01-05 12:02:49 +00:00
|
|
|
use OpenApi\Annotations as OA;
|
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
2014-02-21 09:25:10 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Table(name="role")
|
|
|
|
* @ORM\Entity
|
2019-01-05 12:02:49 +00:00
|
|
|
*
|
2019-08-14 23:50:53 +00:00
|
|
|
* @AuditLog\Auditable
|
|
|
|
*
|
2019-01-05 12:02:49 +00:00
|
|
|
* @OA\Schema(type="object")
|
2014-02-21 09:25:10 +00:00
|
|
|
*/
|
2018-12-22 00:01:04 +00:00
|
|
|
class Role implements \JsonSerializable
|
2014-02-21 09:25:10 +00:00
|
|
|
{
|
2019-02-18 00:52:01 +00:00
|
|
|
public const SUPER_ADMINISTRATOR_ROLE_ID = 1;
|
|
|
|
|
2018-04-06 21:29:27 +00:00
|
|
|
use Traits\TruncateStrings;
|
|
|
|
|
2014-02-21 09:25:10 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="id", type="integer")
|
|
|
|
* @ORM\Id
|
|
|
|
* @ORM\GeneratedValue(strategy="IDENTITY")
|
2019-01-05 12:02:49 +00:00
|
|
|
* @OA\Property(example=1)
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var int
|
2014-02-21 09:25:10 +00:00
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="name", type="string", length=100)
|
2019-01-05 12:02:49 +00:00
|
|
|
* @OA\Property(example="Super Administrator")
|
|
|
|
* @Assert\NotBlank
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2014-02-21 09:25:10 +00:00
|
|
|
protected $name;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var Collection
|
|
|
|
*/
|
2014-02-21 09:25:10 +00:00
|
|
|
protected $users;
|
2016-10-03 07:53:51 +00:00
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\OneToMany(targetEntity="RolePermission", mappedBy="role")
|
2019-01-05 12:02:49 +00:00
|
|
|
* @OA\Property(@OA\Items)
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var Collection
|
|
|
|
*/
|
2016-10-07 20:57:42 +00:00
|
|
|
protected $permissions;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Role constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->users = new ArrayCollection;
|
|
|
|
$this->permissions = new ArrayCollection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-31 17:54:17 +00:00
|
|
|
* @return int
|
2017-08-17 18:28:48 +00:00
|
|
|
*/
|
|
|
|
public function getId(): int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-14 23:50:53 +00:00
|
|
|
* @AuditLog\AuditIdentifier()
|
|
|
|
*
|
2017-08-17 18:28:48 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2019-01-31 17:54:17 +00:00
|
|
|
public function setName(string $name): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2018-04-06 21:29:27 +00:00
|
|
|
$this->name = $this->_truncateString($name, 100);
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getUsers(): Collection
|
|
|
|
{
|
|
|
|
return $this->users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getPermissions(): Collection
|
|
|
|
{
|
|
|
|
return $this->permissions;
|
|
|
|
}
|
2018-12-22 00:01:04 +00:00
|
|
|
|
|
|
|
public function jsonSerialize()
|
|
|
|
{
|
|
|
|
$return = [
|
|
|
|
'id' => $this->id,
|
|
|
|
'name' => $this->name,
|
|
|
|
'permissions' => [
|
|
|
|
'global' => [],
|
|
|
|
'station' => [],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach($this->permissions as $permission) {
|
|
|
|
/** @var RolePermission $permission */
|
|
|
|
|
|
|
|
if ($permission->hasStation()) {
|
2019-01-05 12:02:49 +00:00
|
|
|
$return['permissions']['station'][$permission->getStation()->getId()][] = $permission->getActionName();
|
2018-12-22 00:01:04 +00:00
|
|
|
} else {
|
|
|
|
$return['permissions']['global'][] = $permission->getActionName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|