src/Entity/Firm.php line 53

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use MedBrief\MSR\Controller\Firm\RotateSecret;
  9. use MedBrief\MSR\Repository\FirmRepository;
  10. use Ramsey\Uuid\UuidInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. /**
  13. * A Firm represents an over-arching company or business that comprises one or more
  14. * Client Areas - referred to as Clients on the frontend and {@see Accounts} in the
  15. * code - and is unique in that it has the capability to authenticate itself for
  16. * Machine-to-Machine transactions such as consuming the external API.
  17. *
  18. * This is currently a very rudimentary entity but at a later point we can look at
  19. * migrating some common data shared between multiple client areas to a single source
  20. * of truth here within a Firm.
  21. *
  22. * @author S Honour <[email protected]>
  23. *
  24. * @ApiResource(
  25. * itemOperations={
  26. * "rotate_secret"={
  27. * "method"="POST",
  28. * "path"="/firm/rotate_secret",
  29. * "controller"=RotateSecret::class,
  30. * "deserialize"=false,
  31. * "defaults"={"_api_receive"=false},
  32. * "openapi_context"={
  33. * "summary"="Rotate a Client Secret.",
  34. * "description"="Rotates a client secret for the authenticated API user.",
  35. * "responses"={
  36. * "200"={
  37. * "description"="The new client secret"
  38. * },
  39. * "400"={
  40. * "description"="The authenticated user is not a valid Firm and cannot utilise this endpoint"
  41. * }
  42. * }
  43. * }
  44. * }
  45. * }
  46. * )
  47. *
  48. * @ORM\Entity(repositoryClass=FirmRepository::class)
  49. */
  50. class Firm implements UserInterface
  51. {
  52. /**
  53. * @ORM\Column(name="id", type="uuid")
  54. *
  55. * @ORM\Id
  56. *
  57. * @ORM\GeneratedValue(strategy="CUSTOM")
  58. *
  59. * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator")
  60. */
  61. private ?UuidInterface $id = null;
  62. /**
  63. * @ORM\Column
  64. */
  65. private string $name;
  66. /**
  67. * @Gedmo\Slug(fields={"name"})
  68. *
  69. * @ORM\Column(length=128, unique=true)
  70. */
  71. private ?string $slug;
  72. /**
  73. * @ORM\OneToMany(targetEntity=Account::class, mappedBy="firm", cascade={"persist"})
  74. */
  75. private $clientAreas;
  76. /**
  77. * @ORM\Column
  78. */
  79. private array $roles = [];
  80. /**
  81. * @ORM\Column(type="string", length=255, nullable=true)
  82. */
  83. private ?string $auth0ClientId;
  84. public function __construct()
  85. {
  86. $this->clientAreas = new ArrayCollection();
  87. }
  88. /**
  89. * @return UuidInterface|null
  90. */
  91. public function getId(): ?UuidInterface
  92. {
  93. return $this->id;
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. public function getRoles()
  99. {
  100. return $this->roles;
  101. }
  102. /**
  103. * A Firm does not have an associated password. Instead, it is authenticated via
  104. * Bearer tokens through an external authentication system.
  105. */
  106. public function getPassword()
  107. {
  108. return null;
  109. }
  110. /**
  111. * As a Firm does not have a password, there is no need for a salt either
  112. */
  113. public function getSalt()
  114. {
  115. return null;
  116. }
  117. /**
  118. * @inheritDoc
  119. */
  120. public function getUsername()
  121. {
  122. return $this->auth0ClientId;
  123. }
  124. /**
  125. * Imports antigravity
  126. */
  127. public function eraseCredentials()
  128. {
  129. // https://xkcd.com/353/
  130. }
  131. /**
  132. * @return Collection<int, Account>
  133. */
  134. public function getClientAreas(): Collection
  135. {
  136. return $this->clientAreas;
  137. }
  138. public function addClientArea(Account $clientArea): self
  139. {
  140. if (!$this->clientAreas->contains($clientArea)) {
  141. $this->clientAreas[] = $clientArea;
  142. $clientArea->setFirm($this);
  143. }
  144. return $this;
  145. }
  146. public function removeClientArea(Account $clientArea): self
  147. {
  148. if ($this->clientAreas->removeElement($clientArea)) {
  149. // set the owning side to null (unless already changed)
  150. if ($clientArea->getFirm() === $this) {
  151. $clientArea->setFirm(null);
  152. }
  153. }
  154. return $this;
  155. }
  156. public function addRole(string $role): self
  157. {
  158. if (!in_array($role, $this->roles)) {
  159. $this->roles[] = $role;
  160. }
  161. return $this;
  162. }
  163. public function removeRole(string $role): self
  164. {
  165. if (in_array($role, $this->roles)) {
  166. $key = array_search($role, $this->roles);
  167. unset($this->roles[$key]);
  168. }
  169. return $this;
  170. }
  171. public function getAuth0ClientId(): ?string
  172. {
  173. return $this->auth0ClientId;
  174. }
  175. public function setAuth0ClientId(?string $auth0ClientId): self
  176. {
  177. $this->auth0ClientId = $auth0ClientId;
  178. return $this;
  179. }
  180. /**
  181. * @return string
  182. */
  183. public function getName(): string
  184. {
  185. return $this->name;
  186. }
  187. /**
  188. * @param string $name
  189. *
  190. * @return Firm
  191. */
  192. public function setName(string $name): Firm
  193. {
  194. $this->name = $name;
  195. return $this;
  196. }
  197. /**
  198. * @return string|null
  199. */
  200. public function getSlug(): ?string
  201. {
  202. return $this->slug;
  203. }
  204. /**
  205. * @param string|null $slug
  206. *
  207. * @return Firm
  208. */
  209. public function setSlug(?string $slug): Firm
  210. {
  211. $this->slug = $slug;
  212. return $this;
  213. }
  214. public function isApiEnabled(): bool
  215. {
  216. return (bool) $this->auth0ClientId;
  217. }
  218. }