Access.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Access;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiSubresource;
  6. use App\Entity\Organization\Organization;
  7. use App\Entity\Organization\OrganizationLicence;
  8. use App\Repository\Access\AccessRepository;
  9. use App\Entity\Person\Person;
  10. use App\Entity\Person\PersonActivity;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. /**
  16. * Fais le lien entre une Person et une Organization
  17. * @ApiResource(
  18. * collectionOperations={
  19. * "get"={"security"="is_granted('ROLE_USERS_VIEW')"},
  20. * "post"
  21. * },
  22. * itemOperations={
  23. * "get"={"security"="(is_granted('ROLE_USERS_VIEW') and object.getOrganization().getId() == user.getOrganization().getId()) or (object.getId() == user.getId())"},
  24. * "put"={"security"="is_granted('ROLE_USERS')"},
  25. * "delete"
  26. * }
  27. * )
  28. * @ORM\Entity(repositoryClass=AccessRepository::class)
  29. */
  30. class Access implements UserInterface
  31. {
  32. /**
  33. * @ORM\Id
  34. * @ORM\GeneratedValue
  35. * @ORM\Column(type="integer")
  36. */
  37. private $id;
  38. /**
  39. * @ORM\Column(type="boolean", options={"default" : false})
  40. */
  41. private $adminAccess = false;
  42. /**
  43. * @ORM\ManyToOne(targetEntity=Person::class, cascade={"persist"})
  44. * @ORM\JoinColumn(nullable=false)
  45. */
  46. private $person;
  47. /**
  48. * @ORM\ManyToOne(targetEntity=Organization::class)
  49. * @ORM\JoinColumn(nullable=false)
  50. */
  51. public $organization;
  52. /**
  53. * @var array
  54. * @ORM\Column(type="json_array", length=4294967295, nullable=true)
  55. */
  56. private $roles = [];
  57. /**
  58. * @ORM\OneToMany(targetEntity=PersonActivity::class, mappedBy="access")
  59. * @ApiSubresource()
  60. */
  61. private $personActivity;
  62. /**
  63. * @ORM\OneToMany(targetEntity=OrganizationLicence::class, mappedBy="licensee", orphanRemoval=true)
  64. */
  65. private $organizationLicences;
  66. public function __construct()
  67. {
  68. $this->personActivity = new ArrayCollection();
  69. $this->organizationLicences = new ArrayCollection();
  70. }
  71. public function getId(): ?int
  72. {
  73. return $this->id;
  74. }
  75. public function getAdminAccess(): ?bool
  76. {
  77. return $this->adminAccess;
  78. }
  79. public function setAdminAccess(bool $adminAccess): self
  80. {
  81. $this->adminAccess = $adminAccess;
  82. return $this;
  83. }
  84. public function getPerson(): ?Person
  85. {
  86. return $this->person;
  87. }
  88. public function setPerson(?Person $person): self
  89. {
  90. $this->person = $person;
  91. return $this;
  92. }
  93. public function getOrganization(): ?Organization
  94. {
  95. return $this->organization;
  96. }
  97. public function setOrganization(?Organization $organization): self
  98. {
  99. $this->organization = $organization;
  100. return $this;
  101. }
  102. /**
  103. * @return Collection|PersonActivity[]
  104. */
  105. public function getPersonActivity(): Collection
  106. {
  107. return $this->personActivity;
  108. }
  109. public function addPersonActivity(PersonActivity $personActivity): self
  110. {
  111. if (!$this->personActivity->contains($personActivity)) {
  112. $this->personActivity[] = $personActivity;
  113. $personActivity->setAccess($this);
  114. }
  115. return $this;
  116. }
  117. public function removePersonActivity(PersonActivity $personActivity): self
  118. {
  119. if ($this->personActivity->removeElement($personActivity)) {
  120. // set the owning side to null (unless already changed)
  121. if ($personActivity->getAccess() === $this) {
  122. $personActivity->setAccess(null);
  123. }
  124. }
  125. return $this;
  126. }
  127. /**
  128. * A visual identifier that represents this user.
  129. *
  130. * @see string
  131. */
  132. public function getUserIdentifier(): string
  133. {
  134. return $this->getPerson()->getUsername();
  135. }
  136. /**
  137. * @inheritDoc
  138. */
  139. public function getRoles()
  140. {
  141. $roles = $this->roles;
  142. return array_unique($roles);
  143. }
  144. /**
  145. * @inheritDoc
  146. */
  147. public function getPassword()
  148. {
  149. // TODO: Implement getPassword() method.
  150. }
  151. /**
  152. * @inheritDoc
  153. */
  154. public function getSalt()
  155. {
  156. // TODO: Implement getSalt() method.
  157. }
  158. /**
  159. * @inheritDoc
  160. */
  161. public function getUsername()
  162. {
  163. // TODO: Implement getUsername() method.
  164. }
  165. /**
  166. * @inheritDoc
  167. */
  168. public function eraseCredentials()
  169. {
  170. // TODO: Implement eraseCredentials() method.
  171. }
  172. /**
  173. * @return Collection|OrganizationLicence[]
  174. */
  175. public function getOrganizationLicences(): Collection
  176. {
  177. return $this->organizationLicences;
  178. }
  179. public function addOrganizationLicence(OrganizationLicence $organizationLicence): self
  180. {
  181. if (!$this->organizationLicences->contains($organizationLicence)) {
  182. $this->organizationLicences[] = $organizationLicence;
  183. $organizationLicence->setLicensee($this);
  184. }
  185. return $this;
  186. }
  187. public function removeOrganizationLicence(OrganizationLicence $organizationLicence): self
  188. {
  189. if ($this->organizationLicences->removeElement($organizationLicence)) {
  190. // set the owning side to null (unless already changed)
  191. if ($organizationLicence->getLicensee() === $this) {
  192. $organizationLicence->setLicensee(null);
  193. }
  194. }
  195. return $this;
  196. }
  197. }