Access.php 10 KB

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