Subdomain.php 2.8 KB

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