src/Entity/User.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Controller\Api\UserCreation;
  4. use App\State\UserProcessor;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use ApiPlatform\Core\Annotation\ApiResource;
  7. use ApiPlatform\Metadata\ApiFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
  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. use Symfony\Component\Serializer\Annotation\Groups;
  15. use App\Entity\Client;
  16. use App\Entity\Docteur;
  17. use App\Entity\Laboratoire;
  18. use DateTime;
  19. /**
  20.  * @ORM\Table()
  21.  * @UniqueEntity("email")
  22.  * @ORM\Entity()
  23.  *
  24.  */
  25. #[ApiResource(
  26.     collectionOperations: [
  27.         'get' => ['method' => 'GET'],
  28.         'post' => [
  29.             'method' => 'POST',
  30.             'controller' => UserCreation::class
  31.             /*'route_name' => 'api_user_create', */
  32.             /*'processor' => UserProcessor::class,*/
  33.         ],
  34.     ],
  35.     itemOperations: [
  36.         'get' => ['method' => 'GET'],
  37.         'put' => ['method' => 'PUT'],
  38.         'patch' => ['method' => 'PATCH'],
  39.         'delete' => ['method' => 'DELETE']
  40.     ],
  41.     attributes: [
  42.         'pagination_client_items_per_page' => true,
  43.         'force_eager' => false,
  44.         'normalization_context' => ['groups' => ['user''read']],
  45.         'denormalization_context' => ['groups' => ['user''user-write''read']]
  46.     ]
  47. )]
  48. #[ApiFilter(SearchFilter::class, properties: ["email" => "exact""type" => "exact"])]
  49. #[ApiFilter(BooleanFilter::class,properties: ["docteur.actif"])]
  50. #[ApiFilter(BooleanFilter::class, properties: ["client.actif"])]
  51.  class User implements UserInterfacePasswordAuthenticatedUserInterface
  52. {
  53.     const TYPE_DOCTOR 'docteur';
  54.     /**
  55.      * @var integer $id
  56.      *
  57.      * @ORM\Id
  58.      * @ORM\Column(type="integer")
  59.      * @ORM\GeneratedValue(strategy="AUTO")
  60.      *
  61.      * @Groups({"user"})
  62.      */
  63.     protected $id;
  64.     /**
  65.      * @ORM\Column(name="email", type="string", length=180, nullable=true)
  66.      * @Groups({"user"})
  67.      */
  68.     protected $email;
  69.     /**
  70.      * @ORM\Column(name="salt", type="string", length=1000, nullable=true)
  71.      */
  72.     private $salt;
  73.     /**
  74.      * @ORM\Column(name="password", type="string", length=255, nullable=true)
  75.      */
  76.     private ?string $password null;
  77.     /**
  78.      * @ORM\Column(name="newPassword", type="string", length=255, nullable=true)
  79.      */
  80.     private $newPassword;
  81.     /**
  82.      * @Groups({"user-write"})
  83.      */
  84.     protected $plainPassword;
  85.     /**
  86.      * @ORM\Column(name="firstname", type="string", length=64, nullable=true)
  87.      * @var string
  88.      * @Groups({"user"})
  89.      */
  90.     protected $firstname;
  91.     /**
  92.      * @ORM\Column(name="lastname", type="string", length=64, nullable=true)
  93.      * @var string
  94.      * @Groups({"user"})
  95.      */
  96.     protected $lastname;
  97.     /**
  98.      * @ORM\Column(name="phone", type="string", length=64, nullable=true)
  99.      * @var string
  100.      * @Groups({"user"})
  101.      */
  102.     protected $phone;
  103.     /**
  104.      * @ORM\Column(name="date_of_birth", type="datetime", nullable=true)
  105.      * @var ?DateTime
  106.      * @Groups({"user"})
  107.      */
  108.     protected $dateOfBirth;
  109.     /**
  110.      * @ORM\Column(name="enabled", type="boolean", nullable=true)
  111.      * @var bool
  112.      */
  113.     protected $enabled;
  114.     /**
  115.      * @ORM\Column(name="gender", type="string", length=1, nullable=true)
  116.      * @var string
  117.      * @Groups({"user"})
  118.      */
  119.     protected $gender;
  120.     /**
  121.      * @var array
  122.      *
  123.      * @ORM\Column(name="roles", type="array", nullable=false)
  124.      */
  125.     private $roles;
  126.     /**
  127.      * @var string
  128.      *
  129.      * @ORM\Column(name="type", type="string", length=30, nullable=true)
  130.      * @Assert\NotBlank()
  131.      * @Groups({"user"})
  132.      */
  133.     private $type;
  134.     /**
  135.      * @var Client
  136.      *
  137.      * @ORM\OneToOne(
  138.      *   targetEntity=Client::class,
  139.      *   cascade={"persist"},
  140.      *   inversedBy="user"
  141.      * )
  142.      * @ORM\JoinColumn(name="client_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
  143.      * @Groups({"user", "read"})
  144.      */
  145.     private $client;
  146.     /**
  147.      * @var Docteur
  148.      * EN UTILISATION.
  149.      * @ORM\OneToOne(
  150.      *   targetEntity="App\Entity\Docteur",
  151.      *   cascade={"persist"},
  152.      *   inversedBy="user"
  153.      * )
  154.      * @ORM\JoinColumn(name="docteur_id", referencedColumnName="id", onDelete="CASCADE")
  155.      * @Groups({"user"})
  156.      */
  157.     private $docteur;
  158.     /**
  159.      * @var Laboratoire
  160.      *
  161.      * @ORM\OneToOne(
  162.      *   targetEntity="App\Entity\Laboratoire",
  163.      *   cascade={"persist"},
  164.      *   inversedBy="user"
  165.      * )
  166.      * @ORM\JoinColumn(name="laboratoire_id", referencedColumnName="id", onDelete="CASCADE")
  167.      * @Groups({"user"})
  168.      */
  169.     private $laboratoire;
  170.     /**
  171.      * @var string
  172.      *
  173.      * @ORM\Column(type="text", nullable=true)
  174.      * @Groups({"user"})
  175.      */
  176.     private $photo;
  177.     /**
  178.      * @ORM\Column(name="reset_password_token", type="string", length=255, nullable=true)
  179.      */
  180.     private $resetPasswordToken;
  181.     /**
  182.      * @ORM\Column(name="reset_password_token_expires_at", type="datetime", nullable=true)
  183.      */
  184.     private $resetPasswordTokenExpiresAt;
  185.     /**
  186.      * @var datetime|null
  187.      *
  188.      * @ORM\Column(name="dateCreation", type="datetime", nullable=true)
  189.      *
  190.      * @Groups({"user"})
  191.      */
  192.     private $dateCreation;
  193.     /**
  194.      * @var datetime|null
  195.      *
  196.      * @ORM\Column(name="created_at", type="datetime", nullable=true)
  197.      *
  198.      * @Groups({"user"})
  199.      */
  200.     private $createdAt;
  201.     /**
  202.      * Constructor of Docteur Entity
  203.      */
  204.     public function __construct()
  205.     {
  206.         $this->type 'user'// user / client / docteur / laboratoire
  207.         $this->dateCreation = new \DateTime();
  208.     }
  209.     public function __toString(): string
  210.        {
  211.            return $this->firstname ' ' $this->lastname;
  212.        }
  213.     /**
  214.      * Get id
  215.      *
  216.      * @return integer $id
  217.      */
  218.     public function getId()
  219.     {
  220.         return $this->id;
  221.     }
  222.     /**
  223.      * Get type
  224.      *
  225.      * @return string
  226.      */
  227.     public function getType()
  228.     {
  229.         return $this->type;
  230.     }
  231.     /**
  232.      * Set type
  233.      *
  234.      * @param string $type
  235.      *
  236.      * @return User
  237.      */
  238.     public function setType($type)
  239.     {
  240.         $this->type $type;
  241.         return $this;
  242.     }
  243.     /**
  244.      * Get client
  245.      *
  246.      * @return Client
  247.      */
  248.     public function getClient()
  249.     {
  250.         return $this->client;
  251.     }
  252.     /**
  253.      * Set client
  254.      *
  255.      * @param Client $client
  256.      *
  257.      * @return User
  258.      */
  259.     public function setClient(?Client $client)
  260.     {
  261.         $this->client $client;
  262.         return $this;
  263.     }
  264.     /**
  265.      * Get docteur
  266.      *
  267.      * @return Docteur
  268.      */
  269.     public function getDocteur()
  270.     {
  271.         return $this->docteur;
  272.     }
  273.     /**
  274.      * Set docteur
  275.      *
  276.      * @param Docteur $docteur
  277.      *
  278.      * @return User
  279.      */
  280.     public function setDocteur($docteur)
  281.     {
  282.         $this->docteur $docteur;
  283.         return $this;
  284.     }
  285.     /**
  286.      * Get laboratoire
  287.      *
  288.      * @return stdClass
  289.      */
  290.     public function getLaboratoire()
  291.     {
  292.         return $this->laboratoire;
  293.     }
  294.     /**
  295.      * @return mixed
  296.      */
  297.     public function getEmail()
  298.        {
  299.            return $this->email;
  300.        }
  301.     /**
  302.      * @param mixed $email
  303.      */
  304.     public function setEmail($email): void
  305.        {
  306.            $this->email $email;
  307.        }
  308.     /**
  309.      * @return string
  310.      */
  311.     public function getFirstname(): string
  312.        {
  313.            return $this->firstname;
  314.        }
  315.     /**
  316.      * @param string $firstname
  317.      */
  318.     public function setFirstname(?string $firstname): void
  319.        {
  320.            $this->firstname $firstname;
  321.        }
  322.     /**
  323.      * @return string
  324.      */
  325.     public function getLastname(): string
  326.        {
  327.            return $this->lastname;
  328.        }
  329.     /**
  330.      * @param string $lastname
  331.      */
  332.     public function setLastname(?string $lastname): void
  333.        {
  334.            $this->lastname $lastname;
  335.        }
  336.     /**
  337.      * @return string
  338.      */
  339.     public function getPhone(): ?string
  340.        {
  341.            return $this->phone;
  342.        }
  343.     /**
  344.      * @param string $phone
  345.      */
  346.     public function setPhone(?string $phone): void
  347.        {
  348.            $this->phone $phone;
  349.        }
  350.     /**
  351.      * @return string|null
  352.      */
  353.     public function getGender(): ?string
  354.        {
  355.            return $this->gender;
  356.        }
  357.     /**
  358.      * @param string $gender
  359.      */
  360.     public function setGender(?string $gender): void
  361.        {
  362.            $this->gender $gender;
  363.        }
  364.     /**
  365.      * Set laboratoire
  366.      *
  367.      * @param Laboratoire $laboratoire
  368.      *
  369.      * @return User
  370.      */
  371.     public function setLaboratoire($laboratoire)
  372.     {
  373.         $this->laboratoire $laboratoire;
  374.         return $this;
  375.     }
  376.     /**
  377.      * Set photo
  378.      *
  379.      * @param string photo
  380.      *
  381.      * @return User
  382.      */
  383.     public function setPhoto($photo)
  384.     {
  385.         $this->photo $photo;
  386.         return $this;
  387.     }
  388.     /**
  389.      * Get photo
  390.      *
  391.      * @return string
  392.      */
  393.     public function getPhoto()
  394.     {
  395.         return $this->photo;
  396.     }
  397.     /**
  398.      * @return mixed
  399.      */
  400.     public function getPlainPassword()
  401.        {
  402.            return $this->plainPassword;
  403.        }
  404.     /**
  405.      * @param mixed $plainPassword
  406.      */
  407.     public function setPlainPassword($plainPassword)
  408.        {
  409.            $this->plainPassword $plainPassword;
  410.        }
  411.     /**
  412.      * @return ?DateTime
  413.      */
  414.     public function getDateOfBirth()
  415.        {
  416.            return $this->dateOfBirth;
  417.        }
  418.     /**
  419.      * @param DateTime $dateOfBirth
  420.      */
  421.     public function setDateOfBirth(?DateTime $dateOfBirth): void
  422.        {
  423.            $this->dateOfBirth $dateOfBirth;
  424.        }
  425.     /**
  426.      * @return bool
  427.      */
  428.     public function isEnabled(): bool
  429.        {
  430.            return $this->enabled;
  431.        }
  432.     /**
  433.      * @param bool $enabled
  434.      */
  435.     public function setEnabled(bool $enabled): void
  436.        {
  437.            $this->enabled $enabled;
  438.        }
  439.     /**
  440.      * @inheritDoc
  441.      */
  442.     public function getPassword(): ?string
  443.     {
  444.         return $this->password;
  445.     }
  446.     /**
  447.      * @inheritDoc
  448.      */
  449.     public function setPassword(?string $password): self
  450.     {
  451.         if (!is_null($password)) {
  452.             $this->password $password;
  453.         }
  454.         return $this;
  455.     }
  456.     /**
  457.      * @return mixed
  458.      */
  459.     public function getSalt()
  460.     {
  461.         return $this->salt;
  462.     }
  463.     /**
  464.      * @param mixed $salt
  465.      */
  466.     public function setSalt($salt): void
  467.     {
  468.         $this->salt $salt;
  469.     }
  470.     /**
  471.      * @return mixed
  472.      */
  473.     public function getNewPassword()
  474.     {
  475.         return $this->newPassword;
  476.     }
  477.     /**
  478.      * @param mixed $newPassword
  479.      */
  480.     public function setNewPassword($newPassword): void
  481.     {
  482.         $this->newPassword $newPassword;
  483.     }
  484.     public function getRoles(): array
  485.     {
  486.         $roles $this->roles;
  487.         // guarantee every user at least has ROLE_USER
  488.         $roles[] = 'ROLE_USER';
  489.         return array_unique($roles);
  490.     }
  491.     public function getUsername()
  492.     {
  493.         return $this->email;
  494.     }
  495.         /**
  496.      * Set the reset password token.
  497.      *
  498.      * @param string|null $resetPasswordToken
  499.      * @return $this
  500.      */
  501.     public function setResetPasswordToken(?string $resetPasswordToken): self
  502.     {
  503.         $this->resetPasswordToken $resetPasswordToken;
  504.         return $this;
  505.     }
  506.     /**
  507.      * Get the reset password token.
  508.      *
  509.      * @return string|null
  510.      */
  511.     public function getResetPasswordToken(): ?string
  512.     {
  513.         return $this->resetPasswordToken;
  514.     }
  515.     /**
  516.      * Set the reset password token expiration date.
  517.      *
  518.      * @param \DateTimeInterface|null $resetPasswordTokenExpiresAt
  519.      * @return $this
  520.      */
  521.     public function setResetPasswordTokenExpiresAt(?\DateTimeInterface $resetPasswordTokenExpiresAt): self
  522.     {
  523.         $this->resetPasswordTokenExpiresAt $resetPasswordTokenExpiresAt;
  524.         return $this;
  525.     }
  526.     /**
  527.      * Get the reset password token expiration date.
  528.      *
  529.      * @return \DateTimeInterface|null
  530.      */
  531.     public function getResetPasswordTokenExpiresAt(): ?\DateTimeInterface
  532.     {
  533.         return $this->resetPasswordTokenExpiresAt;
  534.     }
  535.     /**
  536.      * Get dateCreation
  537.      *
  538.      * @return datetime
  539.      */
  540.     public function getDateCreation()
  541.     {
  542.         return $this->dateCreation;
  543.     }
  544.     /**
  545.      * Set dateCreation
  546.      *
  547.      * @param datetime $dateCreation
  548.      *
  549.      * @return User
  550.      */
  551.     public function setDateCreation($dateCreation)
  552.     {
  553.         $this->dateCreation $dateCreation;
  554.         return $this;
  555.     }
  556.     /**
  557.      * Check if the reset password token is valid.
  558.      *
  559.      * @return bool
  560.      */
  561.     public function isResetPasswordTokenValid(): bool
  562.     {
  563.         // Check if the reset password token exists and has not expired
  564.         if ($this->resetPasswordToken && $this->resetPasswordTokenExpiresAt instanceof \DateTimeInterface) {
  565.             $currentDateTime = new \DateTime();
  566.             return $currentDateTime $this->resetPasswordTokenExpiresAt;
  567.         }
  568.         return false;
  569.     }
  570.     public function eraseCredentials() : void {
  571.     }
  572.     public function getUserIdentifier() : string {
  573.         return $this->getEmail();
  574.     }
  575.     /**
  576.      * @return DateTime|null
  577.      */
  578.     public function getCreatedAt(): ?DateTime
  579.     {
  580.         return $this->createdAt;
  581.     }
  582.     /**
  583.      * @param DateTime|null $createdAt
  584.      */
  585.     public function setCreatedAt(?DateTime $createdAt): void
  586.     {
  587.         $this->createdAt $createdAt;
  588.     }
  589. }