src/Entity/Users/User.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Users;
  3. use Symfony\Component\Security\Core\User\UserInterface;
  4. use Symfony\Component\Validator\Constraints AS Constraints;
  5. use Symfony\Component\Validator\Constraints AS Assert;
  6. use Doctrine\ORM\Mapping AS ORM;
  7. use Ramsey\Uuid\Uuid;
  8. /**
  9.  * @ORM\Entity(repositoryClass="App\Repository\Users\UserRepository")
  10.  * @ORM\Table(name="users_user")
  11.  * @ORM\HasLifecycleCallbacks()
  12.  * @ORM\InheritanceType("JOINED")
  13.  * @ORM\DiscriminatorColumn(name="class_name", type="string")
  14.  * @ORM\DiscriminatorMap({
  15.      "user" = "App\Entity\Users\User",
  16.      "admin" = "App\Entity\Users\Administrator",
  17.      "member" = "App\Entity\Members\Member"
  18.    })
  19.  */
  20. class User implements UserInterface
  21. {
  22.     /**
  23.     * @ORM\Id
  24.     * @ORM\GeneratedValue(strategy="AUTO")
  25.     * @ORM\Column(type="integer")
  26.     */
  27.     private $id;
  28.     /**
  29.      * @ORM\Column(type="uuid")
  30.      * @Assert\Uuid
  31.      */
  32.      protected $uuid;
  33.     /**
  34.      * @var \DateTime
  35.      *
  36.      * @ORM\Column(type="datetime", nullable=false)
  37.      */
  38.     private $created;
  39.     /**
  40.      * @var \DateTime
  41.      *
  42.      * @ORM\Column(type="datetime", nullable=true)
  43.      */
  44.     private $lastActive;
  45.     /**
  46.      * @ORM\Column(type="string", unique=true)
  47.      * @Constraints\Email()
  48.      */
  49.     private $email;
  50.     /**
  51.      * @ORM\Column(type="string", length=255, nullable=true)
  52.      */
  53.     private $password;
  54.     /**
  55.      * @ORM\Column(type="string", length=60, nullable=false)
  56.      */
  57.     private $firstNames;
  58.     /**
  59.      * @ORM\Column(type="string", length=60, nullable=true)
  60.      */
  61.     private $lastName;
  62.     /**
  63.      * @ORM\Column(type="string", length=32, nullable=false)
  64.      */
  65.     private $resetToken;
  66.     /**
  67.      * @ORM\ManyToOne(targetEntity="Group", inversedBy="users")
  68.      * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
  69.      **/
  70.     private $group;
  71.     /**
  72.      * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
  73.      * @ORM\JoinTable(name="users_userrole")
  74.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id", unique=false)
  75.      **/
  76.     private $userRoles;
  77.     public function __construct()
  78.     {
  79.         // UUID
  80.         if(!$this->getUuid())
  81.         {
  82.             $uuid Uuid::uuid1();
  83.             $this->setUuid($uuid->toString());
  84.         }
  85.         // Reset token
  86.         if(!$this->resetToken)
  87.             $this->resetToken MD5(uniqid());
  88.         // Set some defaults
  89.         $this->setCreated(new \DateTime());
  90.     }
  91.     
  92.     public function hasPermission($permissionIdentifier null)
  93.     {
  94.         if(!$permissionIdentifier)
  95.             return false;
  96.         
  97.         if($this->getGroup())
  98.         {
  99.             if(count($this->getGroup()->getPermissions()))
  100.             {
  101.                 foreach($this->getGroup()->getPermissions() AS $somePermission)
  102.                 {
  103.                     if($somePermission->getIdentifier() == $permissionIdentifier)
  104.                         return true;
  105.                 }
  106.             }
  107.         }
  108.         
  109.         return false;
  110.     }
  111.     public function getName()
  112.     {
  113.         return $this->getFirstNames();
  114.     }
  115.     public function getFullName()
  116.     {
  117.         return implode(" "array_filter(array(
  118.             $this->getFirstNames(),
  119.             $this->getLastName()
  120.         )));
  121.     }
  122.     public function getId()
  123.     {
  124.         return $this->id;
  125.     }
  126.     public function setUuid($uuid)
  127.     {
  128.         $this->uuid $uuid;
  129.         return $this;
  130.     }
  131.     public function getUuid()
  132.     {
  133.         return $this->uuid;
  134.     }
  135.     public function setResetToken($resetToken)
  136.     {
  137.         $this->resetToken strtoupper($resetToken);
  138.         return $this;
  139.     }
  140.     public function getResetToken()
  141.     {
  142.         return $this->resetToken;
  143.     }
  144.     public function getUsername()
  145.     {
  146.         return $this->email;
  147.     }
  148.     public function getRoles()
  149.     {
  150.         return array(
  151.             'ROLE_USER'
  152.         );
  153.     }
  154.     public function setPassword(string $password null)
  155.     {
  156.         $this->password $password;
  157.     }
  158.     public function getPassword()
  159.     {
  160.         return $this->password;
  161.     }
  162.     public function getSalt()
  163.     {
  164.         return null;
  165.     }
  166.     public function eraseCredentials()
  167.     {
  168.     }
  169.     public function setCreated(\DateTime $created)
  170.     {
  171.         $this->created $created;
  172.     }
  173.     public function getCreated()
  174.     {
  175.         return $this->created;
  176.     }
  177.     public function setLastActive(\DateTime $lastActive)
  178.     {
  179.         $this->lastActive $lastActive;
  180.     }
  181.     public function getLastActive()
  182.     {
  183.         return $this->lastActive;
  184.     }
  185.     public function getEmail()
  186.     {
  187.         return $this->email;
  188.     }
  189.     public function setEmail(string $email)
  190.     {
  191.         $this->email $email;
  192.     }
  193.     public function getLastName()
  194.     {
  195.         return $this->lastName;
  196.     }
  197.     public function setLastName(string $lastName)
  198.     {
  199.         $this->lastName $lastName;
  200.     }
  201.     public function getFirstNames()
  202.     {
  203.         return $this->firstNames;
  204.     }
  205.     public function setFirstNames(string $firstNames)
  206.     {
  207.         $this->firstNames $firstNames;
  208.     }
  209.     
  210.     public function setGroup(\App\Entity\Users\Group $group null)
  211.     {
  212.         $this->group $group;
  213.         return $this;
  214.     }
  215.     public function getGroup()
  216.     {
  217.         return $this->group;
  218.     }
  219.     public function getUserRoles()
  220.     {
  221.         return $this->userRoles;
  222.     }
  223.     public function addUserRole(\App\Entity\Users\Role $userRole)
  224.     {
  225.         if($this->userRoles->contains($userRole))
  226.             return true;
  227.         // Add it
  228.         $this->userRoles->add($userRole);
  229.         $userRole->addUser($this);
  230.     }
  231.     public function removeUserRole(\App\Entity\Users\Role $userRole)
  232.     {
  233.         $this->userRoles->removeElement($userRole);
  234.     }
  235. }