Subdomain.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Organization;
  4. use ApiPlatform\Core\Annotation\ApiFilter;
  5. use App\Annotation\OrganizationDefaultValue;
  6. use App\Repository\Organization\SubdomainRepository;
  7. use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use ApiPlatform\Core\Annotation\ApiResource;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15. * Sous-domaine enregistré par une organisation
  16. */
  17. #[Auditable]
  18. #[ApiResource(
  19. collectionOperations: [
  20. 'get',
  21. 'post'
  22. ],
  23. itemOperations: [
  24. 'get' => [
  25. 'security' => '(is_granted("ROLE_ORGANIZATION_VIEW") or is_granted("ROLE_ORGANIZATION")) and object.getOrganization().getId() == user.getOrganization().getId()'
  26. ],
  27. 'put' => [
  28. 'security' => 'is_granted("ROLE_ORGANIZATION") and object.getOrganization().getId() == user.getOrganization().getId()'
  29. ]
  30. ]
  31. )]
  32. #[ORM\Entity(repositoryClass: SubdomainRepository::class)]
  33. #[OrganizationDefaultValue(fieldName: "organization")]
  34. #[ApiFilter(SearchFilter::class, properties: ['subdomain' => 'exact'])]
  35. #[UniqueEntity('subdomain')]
  36. class Subdomain
  37. {
  38. #[ORM\Id]
  39. #[ORM\Column]
  40. #[ORM\GeneratedValue]
  41. private ?int $id = null;
  42. #[ORM\ManyToOne(inversedBy: 'subdomains')]
  43. private Organization $organization;
  44. #[ORM\Column(type: 'string', length: 60, unique: true, nullable: false)]
  45. #[Groups("subdomain")]
  46. #[Assert\Regex('/^[\w\-]+$/', 'Subdomains can not contains whitespaces or special characters')]
  47. private string $subdomain;
  48. #[ORM\Column(options: ['default' => false])]
  49. #[Groups("subdomain")]
  50. private bool $active = false;
  51. /**
  52. * @return int|null
  53. */
  54. public function getId(): ?int
  55. {
  56. return $this->id;
  57. }
  58. /**
  59. * @param int|null $id
  60. */
  61. public function setId(?int $id): void
  62. {
  63. $this->id = $id;
  64. }
  65. /**
  66. * @return Organization
  67. */
  68. public function getOrganization(): Organization
  69. {
  70. return $this->organization;
  71. }
  72. /**
  73. * @param Organization $organization
  74. */
  75. public function setOrganization(Organization $organization): void
  76. {
  77. $this->organization = $organization;
  78. }
  79. /**
  80. * @return string
  81. */
  82. public function getSubdomain(): string
  83. {
  84. return $this->subdomain;
  85. }
  86. /**
  87. * @param string $subdomain
  88. */
  89. public function setSubdomain(string $subdomain): void
  90. {
  91. $this->subdomain = $subdomain;
  92. }
  93. /**
  94. * @return bool
  95. */
  96. public function isActive(): bool
  97. {
  98. return $this->active;
  99. }
  100. /**
  101. * @param bool $active
  102. */
  103. public function setActive(bool $active): void
  104. {
  105. $this->active = $active;
  106. }
  107. }