src/Entity/User.php line 41

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Controller\Api\Action\User\UserMenuVisibilityAction;
  6. use App\Enum\BaseRoleEnum;
  7. use App\Repository\UserRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. #[ORM\Entity(
  15.     repositoryClassUserRepository::class
  16. )]
  17. #[ORM\Table(
  18.     name'user'
  19. )]
  20. #[UniqueEntity(
  21.     fields'email',
  22.     message'user.email',
  23.     groups: ['registration''password_reset''admin-action-create']
  24. )]
  25. #[ApiResource(
  26.     collectionOperations: [],
  27.     itemOperations: [
  28.         'get',
  29.         'menu_visibility' => [
  30.             'access_control' => 'is_granted("ROLE_USER")',
  31.             'method' => 'PATCH',
  32.             'path' => '/users/menu/visibility/{id}',
  33.             'controller' => UserMenuVisibilityAction::class
  34.         ]
  35.     ]
  36. )]
  37. class User implements UserInterfacePasswordAuthenticatedUserInterface
  38. {
  39.     #[ORM\Id]
  40.     #[ORM\GeneratedValue]
  41.     #[ORM\Column(
  42.         type'integer'
  43.     )]
  44.     private ?int $id null;
  45.     #[ORM\Column(
  46.         name'email',
  47.         type'string',
  48.         length180,
  49.         uniquetrue,
  50.         nullablefalse
  51.     )]
  52.     #[Assert\NotBlank(
  53.         groups: ['registration''request_password_reset''admin-action-create']
  54.     )]
  55.     #[Assert\Email(
  56.         groups: ['password_reset''admin-action-create']
  57.     )]
  58.     private string $email;
  59.     #[ORM\Column(
  60.         type'simple_array'
  61.     )]
  62.     private array $roles = [];
  63.     #[ORM\Column(
  64.         name'password',
  65.         type'string',
  66.         nullablefalse
  67.     )]
  68.     private string $password;
  69.     #[ORM\Column(
  70.         name'first_name',
  71.         type'string',
  72.         nullablefalse
  73.     )]
  74.     #[Assert\NotBlank(
  75.         groups: ['registration''update''admin-action-create''admin-action-update']
  76.     )]
  77.     private ?string $firstName null;
  78.     #[ORM\Column(
  79.         name'last_name',
  80.         type'string',
  81.         nullablefalse
  82.     )]
  83.     #[Assert\NotBlank(
  84.         groups: ['registration''update''admin-action-create''admin-action-update']
  85.     )]
  86.     private ?string $lastName null;
  87.     #[Assert\NotBlank(
  88.         groups: ['registration''password_reset''admin-action-create']
  89.     )]
  90.     #[Assert\Length(
  91.         max4096,
  92.         groups: ['registration''update''action']
  93.     )]
  94.     private ?string $plainPassword null;
  95.     #[ORM\Column(
  96.         name'registration_token',
  97.         type'string',
  98.         length191,
  99.         nullabletrue
  100.     )]
  101.     private ?string $registrationToken null;
  102.     #[ORM\Column(
  103.         name'password_reset_token',
  104.         type'string',
  105.         length191,
  106.         nullabletrue
  107.     )]
  108.     private ?string $passwordResetToken null;
  109.     #[ORM\Column(
  110.         name'menu_visibility',
  111.         type'boolean',
  112.         nullabletrue
  113.     )]
  114.     private ?bool $menuVisibility false;
  115.     #[ORM\Column(
  116.         name'active',
  117.         type'boolean',
  118.         nullabletrue
  119.     )]
  120.     private ?bool $active true;
  121.     #[ORM\OneToMany(
  122.         mappedBy'recipient',
  123.         targetEntityNotification::class,
  124.         cascade: ['persist'],
  125.         fetch'EXTRA_LAZY',
  126.         orphanRemovaltrue
  127.     )]
  128.     private iterable $notification;
  129.     #[ORM\OneToMany(
  130.         mappedBy'user',
  131.         targetEntityUserInstitution::class,
  132.         cascade: ['persist'],
  133.         fetch'EXTRA_LAZY',
  134.         orphanRemovaltrue
  135.     )]
  136.     #[Assert\Valid(
  137.         groups: ['admin-action-create''admin-action-update']
  138.     )]
  139.     #[Assert\Count(
  140.         min1,
  141.         minMessage'user.minInstitution',
  142.         groups: ['admin-action-create''admin-action-update']
  143.     )]
  144.     private iterable $userInstitution;
  145.     #[ORM\OneToMany(
  146.         mappedBy'user',
  147.         targetEntityItemUser::class,
  148.         cascade: ['persist'],
  149.         fetch'EXTRA_LAZY',
  150.         orphanRemovaltrue
  151.     )]
  152.     private ?iterable $itemUser;
  153.     #[ORM\OneToMany(
  154.         mappedBy'user',
  155.         targetEntityPartnerUser::class,
  156.         cascade: ['persist''remove'],
  157.         fetch'EXTRA_LAZY',
  158.         orphanRemovaltrue
  159.     )]
  160.     private ?iterable $partnerUser;
  161.     public function __construct()
  162.     {
  163.         $this->notification = new ArrayCollection();
  164.         $this->userInstitution = new ArrayCollection();
  165.         $this->itemUser = new ArrayCollection();
  166.         $this->partnerUser = new ArrayCollection();
  167.     }
  168.     public function getId(): ?int
  169.     {
  170.         return $this->id;
  171.     }
  172.     public function getEmail(): ?string
  173.     {
  174.         return $this->email;
  175.     }
  176.     public function setEmail(string $email): self
  177.     {
  178.         $this->email $email;
  179.         return $this;
  180.     }
  181.     public function getRoles(): array
  182.     {
  183.         $roles $this->roles;
  184.         $roles[] = BaseRoleEnum::ROLE_USER;
  185.         return array_unique($roles);
  186.     }
  187.     public function setRoles(array $roles): self
  188.     {
  189.         $this->roles $roles;
  190.         return $this;
  191.     }
  192.     public function getPassword(): string
  193.     {
  194.         return $this->password;
  195.     }
  196.     public function setPassword(string $password): self
  197.     {
  198.         $this->password $password;
  199.         return $this;
  200.     }
  201.     public function getFirstName(): ?string
  202.     {
  203.         return $this->firstName;
  204.     }
  205.     public function setFirstName(?string $firstName): self
  206.     {
  207.         $this->firstName $firstName;
  208.         return $this;
  209.     }
  210.     public function getLastName(): ?string
  211.     {
  212.         return $this->lastName;
  213.     }
  214.     public function setLastName(?string $lastName): self
  215.     {
  216.         $this->lastName $lastName;
  217.         return $this;
  218.     }
  219.     public function getFullName(): string
  220.     {
  221.         return $this->getFirstName() . ' ' $this->getLastName();
  222.     }
  223.     public function getPlainPassword(): ?string
  224.     {
  225.         return $this->plainPassword;
  226.     }
  227.     public function setPlainPassword(?string $plainPassword): self
  228.     {
  229.         $this->plainPassword $plainPassword;
  230.         return $this;
  231.     }
  232.     public function getRegistrationToken(): ?string
  233.     {
  234.         return $this->registrationToken;
  235.     }
  236.     public function setRegistrationToken(?string $registrationToken): self
  237.     {
  238.         $this->registrationToken $registrationToken;
  239.         return $this;
  240.     }
  241.     public function getPasswordResetToken(): ?string
  242.     {
  243.         return $this->passwordResetToken;
  244.     }
  245.     public function setPasswordResetToken(?string $passwordResetToken): self
  246.     {
  247.         $this->passwordResetToken $passwordResetToken;
  248.         return $this;
  249.     }
  250.     public function isMenuVisibility(): ?bool
  251.     {
  252.         return $this->menuVisibility;
  253.     }
  254.     public function setMenuVisibility(?bool $menuVisibility): self
  255.     {
  256.         $this->menuVisibility $menuVisibility;
  257.         return $this;
  258.     }
  259.     public function getActive(): ?bool
  260.     {
  261.         return $this->active;
  262.     }
  263.     public function setActive(?bool $active): self
  264.     {
  265.         $this->active $active;
  266.         return $this;
  267.     }
  268.     public function isNotificationsUnread(): bool
  269.     {
  270.         /** @var Notification $n */
  271.         foreach ($this->getNotification() as $n) {
  272.             if (null === $n->getReadDate()) {
  273.                 return true;
  274.             }
  275.         }
  276.         return false;
  277.     }
  278.     public function getNotification(): iterable
  279.     {
  280.         return $this->notification;
  281.     }
  282.     public function removeUserInstitution(UserInstitution $userInstitution): void
  283.     {
  284.         if (!$this->userInstitution->contains($userInstitution)) {
  285.             return;
  286.         }
  287.         $this->userInstitution->removeElement($userInstitution);
  288.         $userInstitution->setUser(null);
  289.     }
  290.     public function addUserInstitution(UserInstitution $userInstitution): void
  291.     {
  292.         if ($this->userInstitution->contains($userInstitution)) {
  293.             return;
  294.         }
  295.         $this->userInstitution[] = $userInstitution;
  296.         $userInstitution->setUser($this);
  297.     }
  298.     public function getCurrentRoleInstitution(): ?UserInstitutionRole
  299.     {
  300.         /** @var UserInstitution $userInstitution */
  301.         foreach ($this->getUserInstitution() as $userInstitution) {
  302.             /** @var UserInstitutionRole $userRoleInstitution */
  303.             foreach ($userInstitution->getRoles() as $userRoleInstitution)
  304.                 if ($userRoleInstitution->isCurrent()) {
  305.                     return $userRoleInstitution;
  306.                 }
  307.         }
  308.         return null;
  309.     }
  310.     public function getCurrentRolePartnerUser(): ?PartnerUser
  311.     {
  312.         /** @var PartnerUser $partnerUser */
  313.         foreach ($this->getPartnerUser() as $partnerUser) {
  314.             if ($partnerUser->isCurrent()) {
  315.                 return $partnerUser;
  316.             }
  317.         }
  318.         return null;
  319.     }
  320.     public function getCurrentInstitution(): ?Institution
  321.     {
  322.         $currentRoleInstitution $this->getCurrentRoleInstitution();
  323.         return $currentRoleInstitution?->getUserInstitution()->getInstitution();
  324.     }
  325.     public function getCurrentRole(): ?Role
  326.     {
  327.         $currentRoleInstitution $this->getCurrentRoleInstitution();
  328.         return $currentRoleInstitution?->getRole();
  329.     }
  330.     public function getUserInstitution(): iterable
  331.     {
  332.         return $this->userInstitution;
  333.     }
  334.     public function setUserInstitution(iterable $userInstitutions): self
  335.     {
  336.         $this->userInstitution $userInstitutions;
  337.         return $this;
  338.     }
  339.     public function getItemUser(): iterable
  340.     {
  341.         return $this->itemUser;
  342.     }
  343.     public function getPartnerUser(): iterable
  344.     {
  345.         return $this->partnerUser;
  346.     }
  347.     public function addPartnerUser(PartnerUser $partnerUser): void
  348.     {
  349.         if ($this->partnerUser->contains($partnerUser)) {
  350.             return;
  351.         }
  352.         $this->partnerUser[] = $partnerUser;
  353.         $partnerUser->setUser($this);
  354.     }
  355.     public function removePartnerUser(PartnerUser $partnerUser): void
  356.     {
  357.         if (!$this->partnerUser->contains($partnerUser)) {
  358.             return;
  359.         }
  360.         $this->partnerUser->removeElement($partnerUser);
  361.         $partnerUser->setUser(null);
  362.     }
  363.     public function getSalt(): ?string
  364.     {
  365.         return null;
  366.     }
  367.     public function eraseCredentials(): void
  368.     {
  369.     }
  370.     public function getUsername(): string
  371.     {
  372.         return $this->email;
  373.     }
  374.     public function getUserIdentifier(): string
  375.     {
  376.         return $this->email;
  377.     }
  378.     public function __sleep(): array
  379.     {
  380.         return ['id''password''email'];
  381.     }
  382. }