<?php
/**
* Copyright (c) 2019, MND Next GmbH - www.mndnext.de
*/
namespace App\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
* @ORM\InheritanceType("JOINED")
*/
class User extends BaseUser
{
const REGISTERED_SELF = 1; // registered themself but not confirm
const REGISTERED_ADMIN = 2; // registered by admin but doesn't put password yet
const REGISTERED_RESET = 3; // user pwd was resettet by admin or by contractor of his company
const REGISTERED_CONFIRMED = 0; // fully registered and confirmed email
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var integer
* @ORM\Column(name="register_state", type="integer", nullable=true)
*/
protected $register_state;
/**
* @var bool
* @ORM\Column(name="admin_confirmed", type="boolean", nullable=false, options={"default":false})
*/
private $admin_confirmed = false;
/**
* @ORM\Column(name="register_date", type="datetime", options={"default":"CURRENT_TIMESTAMP"})
*
* @var \DateTime
*/
private $register_date;
/**
* @var integer
* @ORM\Column(type="integer", nullable=true)
*/
private $login_attempts;
/**
* @var \DateTime
* @ORM\Column(type="datetime", nullable=true)
*/
private $locked_until;
/**
* @var \DateTime
* @ORM\Column(type="datetime", nullable=true)
*/
private $last_failed_login;
public function __construct()
{
$this->register_date = new \DateTime();
parent::__construct();
}
private function hasRoleConfirmed() : bool
{
if ($this->hasRole('ROLE_NEW_ADMIN') || $this->hasRole('ROLE_NEW_CONTACT') || $this->hasRole('ROLE_NEW_EMPLOYEE')) {
return false;
}
return true;
}
private function setRoleConfirmed($state)
{
if ($state) {
if ($this->hasRole('ROLE_NEW_ADMIN')) {
$this->removeRole('ROLE_NEW_ADMIN');
$this->addRole('ROLE_ADMIN');
}
if ($this->hasRole('ROLE_NEW_CONTACT')) {
$this->removeRole('ROLE_NEW_CONTACT');
$this->addRole('ROLE_CONTACT');
}
if ($this->hasRole('ROLE_NEW_EMPLOYEE')) {
$this->removeRole('ROLE_NEW_EMPLOYEE');
$this->addRole('ROLE_EMPLOYEE');
}
} else {
if ($this->hasRole('ROLE_ADMIN')) {
$this->removeRole('ROLE_ADMIN');
$this->addRole('ROLE_NEW_ADMIN');
}
if ($this->hasRole('ROLE_CONTACT')) {
$this->removeRole('ROLE_CONTACT');
$this->addRole('ROLE_NEW_CONTACT');
}
if ($this->hasRole('ROLE_EMPLOYEE')) {
$this->removeRole('ROLE_EMPLOYEE');
$this->addRole('ROLE_NEW_EMPLOYEE');
}
}
}
public function getAdminConfirmed()
{
$state = $this->hasRoleConfirmed();
if ($this->admin_confirmed != $state) {
$this->admin_confirmed = $state;
}
return $state;
}
public function setAdminConfirmed($state)
{
$this->setRoleConfirmed($state);
$this->admin_confirmed = $state;
}
public function getRegisterDate() : ?\DateTime
{
return $this->register_date;
}
public function setRegisterDate(\DateTime $date)
{
$this->register_date = $date;
}
public function getLoginAttempts()
{
return $this->login_attempts;
}
public function setLockedUntil(\DateTime $date = null)
{
$this->locked_until = $date;
}
public function getLockedUntil()
{
return $this->locked_until;
}
public function getLastFailedLogin()
{
return $this->last_failed_login;
}
public function isAccountNonLocked()
{
if ($this->locked_until instanceof \DateTime) {
$now = new \DateTime();
if ($this->locked_until > $now) {
return false;
}
}
return true;
}
/**
* check if user has confirmed is registration
*
* @return bool
*/
public function getHasPasswordSet() : bool
{
if ($this->getRegisterState() !== self::REGISTERED_CONFIRMED) {
return false;
}
return true;
}
public function getRegisterState()
{
return $this->register_state;
}
public function setRegisterState($state)
{
$this->register_state = $state;
}
public function setRandomSalt()
{
$this->salt = $this->getToken(10);
}
private function getToken($length) {
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet.= "0123456789";
$max = strlen($codeAlphabet); // edited
for ($i=0; $i < $length; $i++) {
$token .= $codeAlphabet[random_int(0, $max-1)];
}
return $token;
}
/**
* blocks account for given interval
*
* @param $interval \DateInterval
*/
public function bockTemporary(\DateInterval $interval)
{
$date = new \DateTime();
$date->add($interval);
$this->setLockedUntil($date);
}
/**
* count failed login attempts +1
*/
public function addFailedLoginAttempt()
{
if (is_int($this->login_attempts)) {
$this->login_attempts++;
} else {
$this->login_attempts = 1;
}
$this->last_failed_login = new \DateTime();
}
/**
* reset failed login attempts counter to 0
*/
public function resetLoginAttempts()
{
$this->login_attempts = 0;
$this->last_failed_login = null;
}
}