src/Entity/User.php line 16

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2019, MND Next GmbH - www.mndnext.de
  4.  */
  5. namespace App\Entity;
  6. use FOS\UserBundle\Model\User as BaseUser;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * @ORM\Entity
  10.  * @ORM\Table(name="fos_user")
  11.  * @ORM\InheritanceType("JOINED")
  12.  */
  13. class User extends BaseUser
  14. {
  15.     const REGISTERED_SELF 1;  // registered themself but not confirm
  16.     const REGISTERED_ADMIN 2;  // registered by admin but doesn't put password yet
  17.     const REGISTERED_RESET 3;  // user pwd was resettet by admin or by contractor of his company
  18.     const REGISTERED_CONFIRMED 0;  // fully registered and confirmed email
  19.     /**
  20.      * @ORM\Id
  21.      * @ORM\Column(type="integer")
  22.      * @ORM\GeneratedValue(strategy="AUTO")
  23.      */
  24.     protected $id;
  25.     /**
  26.      * @var integer
  27.      * @ORM\Column(name="register_state", type="integer", nullable=true)
  28.      */
  29.     protected $register_state;
  30.     /**
  31.      * @var bool
  32.      * @ORM\Column(name="admin_confirmed", type="boolean", nullable=false, options={"default":false})
  33.      */
  34.     private $admin_confirmed false;
  35.     /**
  36.      * @ORM\Column(name="register_date", type="datetime", options={"default":"CURRENT_TIMESTAMP"})
  37.      *
  38.      * @var \DateTime
  39.      */
  40.     private $register_date;
  41.     /**
  42.      * @var integer
  43.      * @ORM\Column(type="integer", nullable=true)
  44.      */
  45.     private $login_attempts;
  46.     /**
  47.      * @var \DateTime
  48.      * @ORM\Column(type="datetime", nullable=true)
  49.      */
  50.     private $locked_until;
  51.     /**
  52.      * @var \DateTime
  53.      * @ORM\Column(type="datetime", nullable=true)
  54.      */
  55.     private $last_failed_login;
  56.     public function __construct()
  57.     {
  58.         $this->register_date = new \DateTime();
  59.         parent::__construct();
  60.     }
  61.     private function hasRoleConfirmed() : bool
  62.     {
  63.         if ($this->hasRole('ROLE_NEW_ADMIN') || $this->hasRole('ROLE_NEW_CONTACT') || $this->hasRole('ROLE_NEW_EMPLOYEE')) {
  64.             return false;
  65.         }
  66.         return true;
  67.     }
  68.     private function setRoleConfirmed($state)
  69.     {
  70.         if ($state) {
  71.             if ($this->hasRole('ROLE_NEW_ADMIN')) {
  72.                 $this->removeRole('ROLE_NEW_ADMIN');
  73.                 $this->addRole('ROLE_ADMIN');
  74.             }
  75.             if ($this->hasRole('ROLE_NEW_CONTACT')) {
  76.                 $this->removeRole('ROLE_NEW_CONTACT');
  77.                 $this->addRole('ROLE_CONTACT');
  78.             }
  79.             if ($this->hasRole('ROLE_NEW_EMPLOYEE')) {
  80.                 $this->removeRole('ROLE_NEW_EMPLOYEE');
  81.                 $this->addRole('ROLE_EMPLOYEE');
  82.             }
  83.         } else {
  84.             if ($this->hasRole('ROLE_ADMIN')) {
  85.                 $this->removeRole('ROLE_ADMIN');
  86.                 $this->addRole('ROLE_NEW_ADMIN');
  87.             }
  88.             if ($this->hasRole('ROLE_CONTACT')) {
  89.                 $this->removeRole('ROLE_CONTACT');
  90.                 $this->addRole('ROLE_NEW_CONTACT');
  91.             }
  92.             if ($this->hasRole('ROLE_EMPLOYEE')) {
  93.                 $this->removeRole('ROLE_EMPLOYEE');
  94.                 $this->addRole('ROLE_NEW_EMPLOYEE');
  95.             }
  96.         }
  97.     }
  98.     public function getAdminConfirmed()
  99.     {
  100.         $state $this->hasRoleConfirmed();
  101.         if ($this->admin_confirmed != $state) {
  102.             $this->admin_confirmed $state;
  103.         }
  104.         return $state;
  105.     }
  106.     public function setAdminConfirmed($state)
  107.     {
  108.         $this->setRoleConfirmed($state);
  109.         $this->admin_confirmed $state;
  110.     }
  111.     public function getRegisterDate() : ?\DateTime
  112.     {
  113.         return $this->register_date;
  114.     }
  115.     public function setRegisterDate(\DateTime $date)
  116.     {
  117.         $this->register_date $date;
  118.     }
  119.     public function getLoginAttempts()
  120.     {
  121.         return $this->login_attempts;
  122.     }
  123.     public function setLockedUntil(\DateTime $date null)
  124.     {
  125.         $this->locked_until $date;
  126.     }
  127.     public function getLockedUntil()
  128.     {
  129.         return $this->locked_until;
  130.     }
  131.     public function getLastFailedLogin()
  132.     {
  133.         return $this->last_failed_login;
  134.     }
  135.     public function isAccountNonLocked()
  136.     {
  137.         if ($this->locked_until instanceof \DateTime) {
  138.             $now = new \DateTime();
  139.             if ($this->locked_until $now) {
  140.                 return false;
  141.             }
  142.         }
  143.         return true;
  144.     }
  145.     /**
  146.      * check if user has confirmed is registration
  147.      *
  148.      * @return bool
  149.      */
  150.     public function getHasPasswordSet() : bool
  151.     {
  152.         if ($this->getRegisterState() !== self::REGISTERED_CONFIRMED) {
  153.             return false;
  154.         }
  155.         return true;
  156.     }
  157.     public function getRegisterState()
  158.     {
  159.         return $this->register_state;
  160.     }
  161.     public function setRegisterState($state)
  162.     {
  163.         $this->register_state $state;
  164.     }
  165.     public function setRandomSalt()
  166.     {
  167.         $this->salt $this->getToken(10);
  168.     }
  169.     private function getToken($length) {
  170.         $token "";
  171.         $codeAlphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  172.         $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
  173.         $codeAlphabet.= "0123456789";
  174.         $max strlen($codeAlphabet); // edited
  175.         for ($i=0$i $length$i++) {
  176.             $token .= $codeAlphabet[random_int(0$max-1)];
  177.         }
  178.         return $token;
  179.     }
  180.     /**
  181.      * blocks account for given interval
  182.      *
  183.      * @param $interval \DateInterval
  184.      */
  185.     public function bockTemporary(\DateInterval $interval)
  186.     {
  187.         $date = new \DateTime();
  188.         $date->add($interval);
  189.         $this->setLockedUntil($date);
  190.     }
  191.     /**
  192.      * count failed login attempts +1
  193.      */
  194.     public function addFailedLoginAttempt()
  195.     {
  196.         if (is_int($this->login_attempts)) {
  197.             $this->login_attempts++;
  198.         } else {
  199.             $this->login_attempts 1;
  200.         }
  201.         $this->last_failed_login = new \DateTime();
  202.     }
  203.     /**
  204.      * reset failed login attempts counter to 0
  205.      */
  206.     public function resetLoginAttempts()
  207.     {
  208.         $this->login_attempts 0;
  209.         $this->last_failed_login null;
  210.     }
  211. }