Access.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 JetBrains\PhpStorm\Pure;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. /**
  18. * Fais le lien entre une Person et une Organization
  19. */
  20. #[ApiResource(
  21. collectionOperations:[
  22. 'cget_students'=> [
  23. 'method' => 'GET',
  24. 'path' => '/students',
  25. 'security' => 'is_granted("ROLE_USERS_VIEW")'
  26. ],
  27. 'cget_admin'=> [
  28. 'method' => 'GET',
  29. 'path' => '/admin'
  30. ],
  31. 'get'
  32. ],
  33. itemOperations: [
  34. 'get' => [
  35. 'security' => '(is_granted("ROLE_USERS_VIEW") and object.getOrganization().getId() == user.getOrganization().getId()) or (object.getId() == user.getId())'
  36. ],
  37. 'put' => [
  38. 'security' => 'is_granted("ROLE_USERS") or (object.getId() == user.getId())'
  39. ],
  40. 'delete'
  41. ]
  42. )]
  43. #[ORM\Entity(repositoryClass: AccessRepository::class)]
  44. class Access implements UserInterface
  45. {
  46. #[ORM\Id]
  47. #[ORM\Column]
  48. #[ORM\GeneratedValue]
  49. private ?int $id = null;
  50. #[ORM\Column(options: ['default' => false])]
  51. private bool $adminAccess = false;
  52. #[ORM\Column(nullable: true)]
  53. private ?int $activityYear = null;
  54. #[ORM\ManyToOne(cascade: ['persist'])]
  55. #[ORM\JoinColumn(nullable: false)]
  56. private Person $person;
  57. #[ORM\ManyToOne]
  58. #[ORM\JoinColumn(nullable: false)]
  59. private Organization $organization;
  60. #[ORM\Column(type: 'json', length: 4294967295, nullable: true)]
  61. private ?array $roles = [];
  62. #[Groups(['my_access:input'])]
  63. #[ORM\Column(type: 'json', length: 4294967295, nullable: true)]
  64. private ?array $setting = [];
  65. #[Groups(['my_access:input'])]
  66. private array $historical = [];
  67. #[ORM\OneToMany(mappedBy: 'access', targetEntity: PersonActivity::class, cascade: ['persist'], orphanRemoval: true)]
  68. #[ApiSubresource]
  69. private Collection $personActivity;
  70. #[ORM\OneToMany(mappedBy: 'access', targetEntity: OrganizationFunction::class, cascade: ['persist'], orphanRemoval: true)]
  71. #[ApiSubresource]
  72. private Collection $organizationFunction;
  73. #[ORM\OneToMany(mappedBy: 'licensee', targetEntity: OrganizationLicence::class, cascade: ['persist'], orphanRemoval: true)]
  74. private Collection $organizationLicences;
  75. #[ORM\OneToMany(mappedBy: 'access', targetEntity: PersonalizedList::class, cascade: ['persist'], orphanRemoval: true)]
  76. private Collection $personalizedLists;
  77. #[ORM\ManyToMany(targetEntity: Access::class, mappedBy: 'children', cascade: ['persist'])]
  78. private Collection $guardians;
  79. #[ORM\ManyToMany(targetEntity: Access::class, inversedBy: 'guardians', cascade: ['persist'])]
  80. #[ORM\JoinTable(name: 'children_guardians')]
  81. #[ORM\JoinColumn(name: 'guardians_id', referencedColumnName: 'id')]
  82. #[ORM\InverseJoinColumn(name: 'children_id', referencedColumnName: 'id')]
  83. private Collection $children;
  84. #[Pure] public function __construct()
  85. {
  86. $this->personActivity = new ArrayCollection();
  87. $this->organizationFunction = new ArrayCollection();
  88. $this->organizationLicences = new ArrayCollection();
  89. $this->personalizedLists = new ArrayCollection();
  90. $this->guardians = new ArrayCollection();
  91. $this->children = new ArrayCollection();
  92. }
  93. public function getId(): ?int
  94. {
  95. return $this->id;
  96. }
  97. public function getAdminAccess(): ?bool
  98. {
  99. return $this->adminAccess;
  100. }
  101. public function setAdminAccess(bool $adminAccess): self
  102. {
  103. $this->adminAccess = $adminAccess;
  104. return $this;
  105. }
  106. public function getPerson(): ?Person
  107. {
  108. return $this->person;
  109. }
  110. public function setPerson(?Person $person): self
  111. {
  112. $this->person = $person;
  113. return $this;
  114. }
  115. public function getOrganization(): ?Organization
  116. {
  117. return $this->organization;
  118. }
  119. public function setOrganization(?Organization $organization): self
  120. {
  121. $this->organization = $organization;
  122. return $this;
  123. }
  124. public function getActivityYear(): ?int
  125. {
  126. return $this->activityYear;
  127. }
  128. public function setActivityYear(?int $activityYear): self
  129. {
  130. $this->activityYear = $activityYear;
  131. return $this;
  132. }
  133. public function getHistorical(): array
  134. {
  135. return array_key_exists('historical', $this->setting) && $this->setting['historical'] ? $this->setting['historical'] : ['present' => true];
  136. }
  137. public function setHistorical(array $historical): self
  138. {
  139. if(!$historical['past'] && !$historical['present'] && !$historical['future'])
  140. $historical['present'] = true;
  141. $this->setting['historical'] = $historical;
  142. return $this;
  143. }
  144. public function setRoles(?array $roles): self
  145. {
  146. $this->roles = $roles;
  147. return $this;
  148. }
  149. public function getRoles(): ?array
  150. {
  151. $roles = $this->roles;
  152. return array_unique($roles);
  153. }
  154. public function getPersonActivity(): Collection
  155. {
  156. return $this->personActivity;
  157. }
  158. public function addPersonActivity(PersonActivity $personActivity): self
  159. {
  160. if (!$this->personActivity->contains($personActivity)) {
  161. $this->personActivity[] = $personActivity;
  162. $personActivity->setAccess($this);
  163. }
  164. return $this;
  165. }
  166. public function removePersonActivity(PersonActivity $personActivity): self
  167. {
  168. if ($this->personActivity->removeElement($personActivity)) {
  169. // set the owning side to null (unless already changed)
  170. if ($personActivity->getAccess() === $this) {
  171. $personActivity->setAccess(null);
  172. }
  173. }
  174. return $this;
  175. }
  176. public function getOrganizationFunction(): Collection
  177. {
  178. return $this->organizationFunction;
  179. }
  180. public function addOrganizationFunction (OrganizationFunction $organizationFunction): self
  181. {
  182. if (!$this->organizationFunction->contains($organizationFunction)) {
  183. $this->organizationFunction[] = $organizationFunction;
  184. $organizationFunction->setAccess($this);
  185. }
  186. return $this;
  187. }
  188. public function removeOrganizationFunction(OrganizationFunction $organizationFunction): self
  189. {
  190. if ($this->organizationFunction->removeElement($organizationFunction)) {
  191. // set the owning side to null (unless already changed)
  192. if ($organizationFunction->getAccess() === $this) {
  193. $organizationFunction->setAccess(null);
  194. }
  195. }
  196. return $this;
  197. }
  198. public function getOrganizationLicences(): Collection
  199. {
  200. return $this->organizationLicences;
  201. }
  202. public function addOrganizationLicence(OrganizationLicence $organizationLicence): self
  203. {
  204. if (!$this->organizationLicences->contains($organizationLicence)) {
  205. $this->organizationLicences[] = $organizationLicence;
  206. $organizationLicence->setLicensee($this);
  207. }
  208. return $this;
  209. }
  210. public function removeOrganizationLicence(OrganizationLicence $organizationLicence): self
  211. {
  212. if ($this->organizationLicences->removeElement($organizationLicence)) {
  213. // set the owning side to null (unless already changed)
  214. if ($organizationLicence->getLicensee() === $this) {
  215. $organizationLicence->setLicensee(null);
  216. }
  217. }
  218. return $this;
  219. }
  220. public function getPersonalizedLists(): Collection
  221. {
  222. return $this->personalizedLists;
  223. }
  224. public function addPersonalizedList(PersonalizedList $personalizedList): self
  225. {
  226. if (!$this->personalizedLists->contains($personalizedList)) {
  227. $this->personalizedLists[] = $personalizedList;
  228. $personalizedList->setAccess($this);
  229. }
  230. return $this;
  231. }
  232. public function removePersonalizedList(PersonalizedList $personalizedList): self
  233. {
  234. if ($this->personalizedLists->removeElement($personalizedList)) {
  235. // set the owning side to null (unless already changed)
  236. if ($personalizedList->getAccess() === $this) {
  237. $personalizedList->setAccess(null);
  238. }
  239. }
  240. return $this;
  241. }
  242. public function getChildren(): Collection
  243. {
  244. return $this->children;
  245. }
  246. public function addChild (Access $access): self
  247. {
  248. if (!$this->children->contains($access)) {
  249. $this->children[] = $access;
  250. $access->addGuardian($this);
  251. }
  252. return $this;
  253. }
  254. public function removeChild(Access $access): self
  255. {
  256. if ($this->children->removeElement($access)) {
  257. $access->removeGuardian($this);
  258. }
  259. return $this;
  260. }
  261. public function getGuardians(): Collection
  262. {
  263. return $this->guardians;
  264. }
  265. public function addGuardian(Access $access): self
  266. {
  267. if (!$this->guardians->contains($access)) {
  268. $this->guardians[] = $access;
  269. $access->addChild($this);
  270. }
  271. return $this;
  272. }
  273. public function removeGuardian(Access $access): self
  274. {
  275. if ($this->guardians->removeElement($access)) {
  276. $access->removeChild($this);
  277. }
  278. return $this;
  279. }
  280. #[Pure] public function getUserIdentifier(): string
  281. {
  282. return $this->person->getUsername();
  283. }
  284. public function getPassword()
  285. {
  286. // TODO: Implement getPassword() method.
  287. }
  288. public function getSalt()
  289. {
  290. // TODO: Implement getSalt() method.
  291. }
  292. public function getUsername()
  293. {
  294. // TODO: Implement getUsername() method.
  295. }
  296. public function eraseCredentials()
  297. {
  298. // TODO: Implement eraseCredentials() method.
  299. }
  300. }