src/Entity/User.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  11.  * @UniqueEntity(fields={"email"}, message="user.email.exist")
  12.  * @ORM\Table(indexes={
  13.  *    @ORM\Index(name="createdAt",columns={"created_at"}),
  14.  *    @ORM\Index(name="updatedAt",columns={"updated_at"}),
  15.  *    @ORM\Index(name="deletedAt",columns={"deleted_at"}),
  16.  *    @ORM\Index(name="created",columns={"created"}),
  17.  * })
  18.  * @ORM\HasLifecycleCallbacks
  19.  */
  20. class User implements UserInterface {
  21.     const ROLE_USER 'ROLE_USER';
  22.     const ROLE_MANAGER 'ROLE_MANAGER';
  23.     const ROLE_ADMIN 'ROLE_ADMIN';
  24.     const ENABLED 'Habiltiado';
  25.     const DISABLED 'No habilitado';
  26.     const STR_ROLE_USER 'USUARIO';
  27.     const STR_ROLE_MANAGER 'MANAGER';
  28.     const STR_ROLE_ADMIN 'ADMINISTRADOR';
  29.     /**
  30.      *
  31.      * @ORM\Id()
  32.      * @ORM\GeneratedValue()
  33.      * @ORM\Column(name="id", type="integer")
  34.      */
  35.     protected $id;
  36.     /**
  37.      * @ORM\Column(type="string", length=255, nullable=true)
  38.      */
  39.     protected $firebaseId;
  40.     /**
  41.      * One User has One Profile.
  42.      * @ORM\OneToOne(targetEntity="Profile", cascade={"persist"})
  43.      * @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
  44.      */
  45.     protected $profile;
  46.     /**
  47.      * Encrypted password. Must be persisted.
  48.      * @ORM\Column(name="username", type="string", length=180)
  49.      * @var string
  50.      */
  51.     protected $username;
  52.     /**
  53.      * Encrypted password. Must be persisted.
  54.      * @ORM\Column(name="email", type="string", length=180)
  55.      * @var string
  56.      */
  57.     protected $email;
  58.     /**
  59.      * @var bool
  60.      * @ORM\Column(name="enabled", type="boolean")
  61.      */
  62.     protected $enabled;
  63.     /**
  64.      * The salt to use for hashing.
  65.      *
  66.      * @var string
  67.      * @ORM\Column(name="salt", type="string", nullable=true)
  68.      */
  69.     protected $salt;
  70.     /**
  71.      * Encrypted password. Must be persisted.
  72.      *
  73.      * @var string
  74.      * @ORM\Column(name="password", type="string")
  75.      */
  76.     protected $password;
  77.     /**
  78.      * Encrypted password. Must be persisted.
  79.      *
  80.      * @var string
  81.      * @ORM\Column(type="boolean", nullable=true)
  82.      */
  83.     protected $taasEnabled false;
  84.     /**
  85.      * Encrypted password. Must be persisted.
  86.      *
  87.      * @var bool
  88.      * @ORM\Column(type="boolean", nullable=true)
  89.      */
  90.     protected $demo false;
  91.     /**
  92.      * @var \DateTime|null
  93.      * @ORM\Column(name="last_login", type="datetime", nullable=true)
  94.      */
  95.     protected $lastLogin;
  96.     /**
  97.      * @var \DateTime|null
  98.      * @ORM\Column(name="last_cloud_login", type="datetime", nullable=true)
  99.      */
  100.     protected $lastCloudLogin;
  101.     /**
  102.      * @var bool
  103.      * @ORM\Column(name="first_cloud_login", type="boolean", nullable=true)
  104.      */
  105.     protected $firstCloudLogin;
  106.     /**
  107.      * Random string sent to the user email address in order to verify it.
  108.      *
  109.      * @var string|null
  110.      * @ORM\Column(name="confirmation_token", type="string", nullable=true, unique=true, length=180)
  111.      */
  112.     protected $confirmationToken;
  113.     /**
  114.      * @var \DateTime|null
  115.      * @ORM\Column(name="password_requested_at", type="datetime", nullable=true)
  116.      */
  117.     protected $passwordRequestedAt;
  118.     /**
  119.      * @var array
  120.      * @ORM\Column(name="roles", type="array")
  121.      */
  122.     protected $roles;
  123.     /**
  124.      * @ORM\OneToMany(targetEntity="Treatment", mappedBy="user")
  125.      */
  126.     protected $treatments;
  127.     /** Many users has one userType
  128.      * @ORM\ManyToOne(targetEntity="UserType")
  129.      * @ORM\JoinColumn(name="user_type_id", referencedColumnName="id",nullable=true)
  130.      */
  131.     protected $userType;
  132.     /**
  133.      * @ORM\ManyToOne(targetEntity="Organization", inversedBy="users")
  134.      * @ORM\JoinColumn(name="organization_id", referencedColumnName="id",nullable=true)
  135.      */
  136.     protected $organization;
  137.     /**
  138.      * @ORM\Column(type="datetime", nullable=true)
  139.      */
  140.     protected $created;
  141.     /**
  142.      * @ORM\Column(type="string", length=255, nullable=true)
  143.      */
  144.     protected $provider;
  145.     /**
  146.      * @ORM\Column(type="string", nullable=true)
  147.      */
  148.     protected $locale;
  149.     /**
  150.      * Many Users have One Address
  151.      * @ORM\ManyToOne(targetEntity="Address")
  152.      * @ORM\JoinColumn(name="address_id", referencedColumnName="id")
  153.      */
  154.     private $address;
  155.     /**
  156.      * @ORM\Column(type="string", length=180, nullable=true)
  157.      */
  158.     private $secondEmail;
  159.     /**
  160.      * @ORM\Column(type="integer", nullable=true)
  161.      */
  162.     private $userIntoFarm;
  163.     /**
  164.      * @ORM\Column(type="datetime")
  165.      */
  166.     private $updatedAt;
  167.     /**
  168.      * @ORM\Column(type="datetime")
  169.      */
  170.     private $createdAt;
  171.     /**
  172.      * @ORM\Column(type="datetime", nullable=true)
  173.      */
  174.     private $deletedAt;
  175.     /**
  176.      * @ORM\OneToMany(targetEntity=Media::class, mappedBy="user")
  177.      */
  178.     private $media;
  179.     /**
  180.      * @ORM\Column(type="boolean", nullable=true)
  181.      */
  182.     private $isOrganization;
  183.     /** @ORM\PreUpdate */
  184.     public function PreUpdate()
  185.     {
  186.         $this->setUpdatedAt( new \DateTime() );
  187.     }
  188.     /** @ORM\PrePersist */
  189.     public function PrePersist()
  190.     {
  191.         $this->setCreatedAt( new \DateTime() );
  192.         $this->setUpdatedAt( new \DateTime() );
  193.     }
  194.     public function __construct() {
  195.         $this->enabled false;
  196.         $this->roles = array(self::ROLE_USER);
  197.         $this->treatments = array();
  198.         $this->created = new \DateTime();
  199.         $this->media = new ArrayCollection();
  200.     }
  201.     public function __toString() {
  202.         return $this->username ?? $this->email;
  203.     }
  204.     public function getShortDescription() {
  205.         return $this->profile->getName();
  206.     }
  207.     public function getEnabledString() {
  208.         if ($this->enabled) {
  209.             return self::ENABLED;
  210.         } else {
  211.             return self::DISABLED;
  212.         }
  213.     }
  214.     public function getListRoles() {
  215.         $list = [];
  216.         foreach ($this->getRoles() as $rol) {
  217.             array_push($list$this->getStrRole($rol));
  218.         }
  219.        return $list;
  220.     }
  221.     private function getStrRole($rol) {
  222.         switch ($rol) {
  223.             case self::ROLE_USER:
  224.                 return self::STR_ROLE_USER;
  225.             case self::ROLE_MANAGER:
  226.                 return self::STR_ROLE_MANAGER;
  227.             case self::ROLE_ADMIN:
  228.                 return self::STR_ROLE_ADMIN;
  229.         }
  230.     }
  231.     public function hasRole$rol 
  232.                                                     {/*{{{*/
  233.                                                         return in_array$rol$this->getRoles());
  234.                                                     }/*}}}*/
  235.     public function isAdmin() {
  236.         if (in_array(self::ROLE_ADMIN$this->getRoles())) {
  237.             return true;
  238.         }
  239.         return false;
  240.     }
  241.     public function isManger() {
  242.         if (in_array(self::ROLE_MANAGER$this->getRoles())) {
  243.             return true;
  244.         }
  245.         return false;
  246.     }
  247.     public function getFirebaseId() {
  248.         return $this->firebaseId;
  249.     }
  250.     public function setFirebaseId(string $firebaseId): self {
  251.         $this->firebaseId $firebaseId;
  252.         return $this;
  253.     }
  254.     /**
  255.      * @return mixed
  256.      */
  257.     public function getProfile() {
  258.         return $this->profile;
  259.     }
  260.     /**
  261.      * @param mixed $profile
  262.      */
  263.     public function setProfileProfile $profile ) {
  264.         $this->profile $profile;
  265.         return $this;
  266.     }
  267.     /**
  268.      * Returns the roles granted to the user.
  269.      *
  270.      * @return (Role|string)[] The user roles
  271.      */
  272.     public function getRoles() {
  273.         $roles $this->roles;
  274.         // guarantee every user at least has ROLE_USER
  275.         $roles[] = self::ROLE_USER;
  276.         return array_unique($roles);
  277.     }
  278.     /**
  279.      * @see UserInterface
  280.      */
  281.     public function getPassword() {
  282.         return (string) $this->password;
  283.     }
  284.     /**
  285.      * Returns the salt that was originally used to encode the password.
  286.      *
  287.      * This can return null if the password was not encoded using a salt.
  288.      *
  289.      * @return string|null The salt
  290.      */
  291.     public function getSalt() {
  292.         return $this->salt;
  293.     }
  294.     /**
  295.      * Returns the username used to authenticate the user.
  296.      *
  297.      * @return string The username
  298.      */
  299.     public function getUsername() {
  300.         return $this->username;
  301.     }
  302.     /**
  303.      * @param string $username
  304.      */
  305.     public function setUsername(string $username) {
  306.         $this->username $username;
  307.     }
  308.     /**
  309.      * @param string $email
  310.      */
  311.     public function setEmail(string $email) {
  312.         $this->email $email;
  313.         $this->username $email;
  314.     }
  315.     /**
  316.      * @param bool $enabled
  317.      */
  318.     public function setEnabled(bool $enabled) {
  319.         $this->enabled $enabled;
  320.     }
  321.     /**
  322.      * @param bool $firstCloudLogin
  323.      */
  324.     public function setFirstCloudLogin(bool $firstCloudLogin) {
  325.         $this->firstCloudLogin $firstCloudLogin;
  326.     }
  327.     /**
  328.      * @param string $salt
  329.      */
  330.     public function setSalt(string $salt) {
  331.         $this->salt $salt;
  332.     }
  333.     /**
  334.      * @param string $password
  335.      */
  336.     public function setPassword(?string $password) {
  337.                                  
  338.                                          /* nunca un password null ni '' */
  339.                                          $password and $this->password $password;
  340.                                      }
  341.     /**
  342.      * @param \DateTime|null $lastLogin
  343.      */
  344.     public function setLastLogin(?\DateTime $lastLogin) {
  345.         $this->lastLogin $lastLogin;
  346.     }
  347.     /**
  348.      * @param \DateTime|null $lastCloudLogin
  349.      */
  350.     public function setLastCloudLogin(?\DateTime $lastCloudLogin) {
  351.         $this->lastCloudLogin $lastCloudLogin;
  352.     }
  353.     /**
  354.      * @param string|null $confirmationToken
  355.      */
  356.     public function setConfirmationToken(?string $confirmationToken) {
  357.         $this->confirmationToken $confirmationToken;
  358.     }
  359.     /**
  360.      * @param \DateTime|null $passwordRequestedAt
  361.      */
  362.     public function setPasswordRequestedAt(?\DateTime $passwordRequestedAt) {
  363.         $this->passwordRequestedAt $passwordRequestedAt;
  364.     }
  365.     /**
  366.      * @param array $roles
  367.      */
  368.     public function setRoles(array $roles) {
  369.         if (sizeof($roles) == 0)
  370.             $roles[] = 'ROLE_USER';
  371.         $this->roles $roles;
  372.     }
  373.     /**
  374.      * @return mixed
  375.      */
  376.     public function getProvider() {
  377.         return $this->provider;
  378.     }
  379.     /**
  380.      * @param mixed $provider
  381.      */
  382.     public function setProvider($provider) {
  383.         $this->provider $provider;
  384.     }
  385.     /**
  386.      * @return string
  387.      */
  388.     public function getEmail() {
  389.         return $this->email;
  390.     }
  391.     /**
  392.      * @return bool
  393.      */
  394.     public function isEnabled() {
  395.         return $this->enabled;
  396.     }
  397.     /**
  398.      * @return bool
  399.      */
  400.     public function isFirstCloudLogin() {
  401.         return $this->firstCloudLogin;
  402.     }
  403.     /**
  404.      * @return \DateTime|null
  405.      */
  406.     public function getLastLogin() {
  407.         return $this->lastLogin;
  408.     }
  409.     /**
  410.      * @return \DateTime|null
  411.      */
  412.     public function getLastCloudLogin() {
  413.         return $this->lastCloudLogin;
  414.     }
  415.     /**
  416.      * @return string|null
  417.      */
  418.     public function getConfirmationToken() {
  419.         return $this->confirmationToken;
  420.     }
  421.     /**
  422.      * @return \DateTime|null
  423.      */
  424.     public function getPasswordRequestedAt() {
  425.         return $this->passwordRequestedAt;
  426.     }
  427.     /**
  428.      * Removes sensitive data from the user.
  429.      *
  430.      * This is important if, at any given point, sensitive information like
  431.      * the plain-text password is stored on this object.
  432.      */
  433.     public function eraseCredentials() {
  434.         // TODO: Implement eraseCredentials() method.
  435.     }
  436.     /**
  437.      * @return mixed
  438.      */
  439.     public function getId() {
  440.         return $this->id;
  441.     }
  442.     /**
  443.      * @param mixed $id
  444.      */
  445.     public function setId($id) {
  446.         $this->id $id;
  447.     }
  448.     /**
  449.      * @return mixed
  450.      */
  451.     public function getTreatments() {
  452.         return $this->treatments;
  453.     }
  454.     /**
  455.      * @param mixed $treatments
  456.      */
  457.     public function setTreatments($treatments) {
  458.         $this->treatments $treatments;
  459.     }
  460.     /**
  461.      * @return mixed
  462.      */
  463.     public function getCreated() {
  464.         return $this->created;
  465.     }
  466.     /**
  467.      * @param mixed $created
  468.      */
  469.     public function setCreated($created): void {
  470.         $this->created $created;
  471.     }
  472.     /**
  473.      * @return mixed
  474.      */
  475.     public function getUserType() {
  476.         return $this->userType;
  477.     }
  478.     /**
  479.      * @param mixed $userType
  480.      */
  481.     public function setUserType($userType): void {
  482.         $this->userType $userType;
  483.     }
  484.     public function getEnabled(): ?bool {
  485.         return $this->enabled;
  486.     }
  487.     public function addTreatment(Treatment $treatment): self {
  488.         if (!$this->treatments->contains($treatment)) {
  489.             $this->treatments[] = $treatment;
  490.             $treatment->setUser($this);
  491.         }
  492.         return $this;
  493.     }
  494.     public function removeTreatment(Treatment $treatment): self {
  495.         if ($this->treatments->contains($treatment)) {
  496.             $this->treatments->removeElement($treatment);
  497.             // set the owning side to null (unless already changed)
  498.             if ($treatment->getUser() === $this) {
  499.                 $treatment->setUser(null);
  500.             }
  501.         }
  502.         return $this;
  503.     }
  504.     public function getOrganization(): ?Organization {
  505.         return $this->organization;
  506.     }
  507.     public function setOrganization(?Organization $organization): self {
  508.         $this->organization $organization;
  509.         return $this;
  510.     }
  511.     public function getTaasEnabled(): ?bool {
  512.         return $this->taasEnabled;
  513.     }
  514.     public function setTaasEnabled(?bool $taasEnabled): self {
  515.         $this->taasEnabled $taasEnabled;
  516.         return $this;
  517.     }
  518.     /**
  519.      * @return bool
  520.      */
  521.     public function isDemo(): ?bool {
  522.         return $this->demo;
  523.     }
  524.     /**
  525.      * @param bool $demo
  526.      */
  527.     public function setDemo(?bool $demo): void {
  528.         $this->demo $demo;
  529.     }
  530.     public function getDemo(): ?bool
  531.     {
  532.         return $this->demo;
  533.     }
  534.     public function getFirstCloudLogin(): ?bool
  535.     {
  536.         return $this->firstCloudLogin;
  537.     }
  538.     public function getLocale(): ?string
  539.     {
  540.         return $this->locale;
  541.     }
  542.     public function setLocale(?string $locale): self
  543.     {
  544.         $this->locale $locale;
  545.         return $this;
  546.     }
  547.     public function getAddress(): ?Address
  548.     {
  549.         return $this->address;
  550.     }
  551.     public function setAddress(?Address $address): self
  552.     {
  553.         $this->address $address;
  554.         return $this;
  555.     }
  556.     public function isTaasEnabled(): ?bool
  557.     {
  558.         return $this->taasEnabled;
  559.     }
  560.     public function getSecondEmail(): ?string
  561.     {
  562.         return $this->secondEmail;
  563.     }
  564.     public function setSecondEmail(?string $secondEmail): self
  565.     {
  566.         $this->secondEmail $secondEmail;
  567.         return $this;
  568.     }
  569.     public function getUserIntoFarm(): ?int
  570.     {
  571.         return $this->userIntoFarm;
  572.     }
  573.     public function setUserIntoFarm(?int $userIntoFarm): self
  574.     {
  575.         $this->userIntoFarm $userIntoFarm;
  576.         return $this;
  577.     }
  578.     public function getUpdatedAt(): ?\DateTime
  579.     {
  580.         return $this->updatedAt;
  581.     }
  582.     public function setUpdatedAt(\DateTime $updatedAt): self
  583.     {
  584.         $this->updatedAt $updatedAt;
  585.         return $this;
  586.     }
  587.     public function getCreatedAt(): ?\DateTime
  588.     {
  589.         return $this->createdAt;
  590.     }
  591.     public function setCreatedAt(\DateTime $createdAt): self
  592.     {
  593.         $this->createdAt $createdAt;
  594.         return $this;
  595.     }
  596.     public function getDeletedAt(): ?\DateTime
  597.     {
  598.         return $this->deletedAt;
  599.     }
  600.     public function setDeletedAt(?\DateTime $deletedAt): self
  601.     {
  602.         $this->deletedAt $deletedAt;
  603.         return $this;
  604.     }
  605.     /**
  606.      * @return Collection<int, Media>
  607.      */
  608.     public function getMedia(): Collection
  609.     {
  610.         return $this->media;
  611.     }
  612.     public function addMedium(Media $medium): self
  613.     {
  614.         if (!$this->media->contains($medium)) {
  615.             $this->media[] = $medium;
  616.             $medium->setUser($this);
  617.         }
  618.         return $this;
  619.     }
  620.     public function removeMedium(Media $medium): self
  621.     {
  622.         if ($this->media->removeElement($medium)) {
  623.             // set the owning side to null (unless already changed)
  624.             if ($medium->getUser() === $this) {
  625.                 $medium->setUser(null);
  626.             }
  627.         }
  628.         return $this;
  629.     }
  630.     public function isIsOrganization(): ?bool
  631.     {
  632.         return $this->isOrganization;
  633.     }
  634.     public function setIsOrganization(?bool $isOrganization): self
  635.     {
  636.         $this->isOrganization $isOrganization;
  637.         return $this;
  638.     }
  639. }