lastLogin = new \DateTime(); $this->enabled = false; $this->twoFactorEmailAuthentication = true; $this->authorized_ips = array(); $this->remember_places = false; $this->failedAttempt = 0; $this->logMe = false; $this->gender = self::GENDER_NA; $this->publicKey = $this->generateToken(); } protected function generateToken($length = 64){ $hash = ""; while(strlen($hash) < $length){ try { $hash .= hash('sha512', random_bytes($length)); } catch (\Exception $e) { $hash .= hash('sha512', mt_rand(0,$length*10000)); } } return substr($hash, 0,$length); } /** * use to change the pk if its not unique */ public function regeneratePublicKey(){ $this->publicKey = $this->generateToken(); } /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Set username * * @param string $username * * @return $this */ public function setUsername($username) { $this->username = $username; return $this; } /** * Get username * * @return string */ public function getUsername() { if(empty($this->username)) return $this->email; return $this->username; } /** * Set password * * @param string $password * * @return $this */ public function setPassword($password) { $this->password = $password; return $this; } /** * Get password * * @return string */ public function getPassword() { return $this->password; } /** * Set salt * * @param string $salt * * @return $this */ public function setSalt($salt) { $this->salt = $salt; return $this; } /** * Get salt * * @return string */ public function getSalt() { return $this->salt; } /** * Set roles * * @param array $roles * * @return $this */ public function setRoles($roles) { $this->roles = $roles; return $this; } /** * Set roles * * @param string $role * * @return $this */ public function addRole($role) { $this->roles[] = $role; return $this; } /** * Set roles * * @param string $role * * @return $this */ public function removeRole($role) { if (($key = array_search($role, $this->roles)) !== false) { unset($this->roles[$key]); } return $this; } /** * @param $role * @return bool */ public function hasRole($role): bool { if (($key = array_search($role, $this->roles)) !== false) { return true; } return false; } /** * Get roles * * @return array */ public function getRoles() { return $this->roles; } public function eraseCredentials() { } /** * @return mixed */ public function getPublicKey() { return $this->publicKey; } /** * @param mixed $gender * @return $this */ public function setGender($gender) { $this->gender = $gender; return $this; } /** * @return int */ public function getGender() { return $this->gender; } /** * @param string $lastname * @return $this */ public function setLastname(string $lastname) { $this->lastname = $lastname; return $this; } /** * @return string */ public function getLastname() { return $this->lastname; } /** * @param string $firstname * @return $this */ public function setFirstname(string $firstname) { $this->firstname = $firstname; return $this; } /** * @return string */ public function getFirstname() { return $this->firstname; } /** * @param string $phone * @return $this */ public function setPhone(string $phone) { $this->phone = $phone; return $this; } /** * @return string */ public function getPhone() { return $this->phone; } /** * @param mixed $email * @return $this */ public function setEmail($email) { $this->email = $email; return $this; } /** * @return null|string */ public function getEmail(): ?string { return $this->email; } /** * @param string $position * @return $this */ public function setPosition(string $position) { $this->position = $position; return $this; } /** * @return string */ public function getPosition() { return $this->position; } /** * @param string $hashIdentifier * @return $this */ public function setHashIdentifier(string $hashIdentifier) { $this->hashIdentifier = $hashIdentifier; return $this; } /** * @return string */ public function getHashIdentifier() { return $this->hashIdentifier; } /** * @param string $middlename * @return $this */ public function setMiddlename(string $middlename) { $this->middlename = $middlename; return $this; } /** * @return string */ public function getMiddlename() { return $this->middlename; } /** * @param bool $enabled * @return $this */ public function setEnabled(bool $enabled) { $this->enabled = $enabled; return $this; } /** * @return bool */ public function isEnabled(): bool { return $this->enabled; } /** * @param \DateTime $lastLogin * @return $this */ public function setLastLogin(\DateTime $lastLogin) { $this->lastLogin = $lastLogin; return $this; } /** * @return \DateTime */ public function getLastLogin(): \DateTime { return $this->lastLogin; } /** * @param string $confirmationToken * @return $this */ public function setConfirmationToken(string $confirmationToken) { $this->confirmationToken = $confirmationToken; return $this; } /** * @return string */ public function getConfirmationToken() { return $this->confirmationToken; } /** * @param bool $twoFactorEmailAuthentication * @return $this */ public function setTwoFactorEmailAuthentication(bool $twoFactorEmailAuthentication) { $this->twoFactorEmailAuthentication = $twoFactorEmailAuthentication; return $this; } /** * @return bool */ public function isTwoFactorEmailAuthentication(): bool { return $this->twoFactorEmailAuthentication; } /** * @param int $twoFactorEmailCode * @return $this */ public function setTwoFactorEmailCode(int $twoFactorEmailCode) { $this->twoFactorEmailCode = $twoFactorEmailCode; return $this; } /** * @return $this */ public function generateTwoFactorEmailCode() { //TODO remove before prod !!!! $this->twoFactorEmailCode = 999999;//rand(100000, 999999); return $this; } /** * @return int */ public function getTwoFactorEmailCode(): ?int { return $this->twoFactorEmailCode; } /** * @param array $authorized_ips * @return $this */ public function setAuthorizedIps(array $authorized_ips) { $this->authorized_ips = $authorized_ips; return $this; } /** * @return array */ public function getAuthorizedIps(): array { return $this->authorized_ips; } /** * @param $ip * @param $computerName * @return $this */ public function addAuthorizedIp($ip,$computerName) { if(!is_array($this->authorized_ips)) $this->authorized_ips = array(); $this->authorized_ips[] = array( 'ip' => $ip, 'computer' => $computerName, ); return $this; } /** * @param $ip * @param $computerName * @return $this */ public function removeAuthorizedIp($ip,$computerName) { if(!is_array($this->authorized_ips)) return $this; foreach($this->authorized_ips as $k=>$itm){ if(isset($itm['ip']) && $itm['ip'] == $ip){ if(isset($itm['computer']) && $itm['computer'] == $computerName){ unset($this->authorized_ips[$k]); } } } return $this; } /** * @param bool $remember_places * @return $this */ public function setRememberPlaces(bool $remember_places) { $this->remember_places = $remember_places; return $this; } /** * @return bool */ public function isRememberPlaces(): bool { return $this->remember_places; } /** * @param int $failedAttempt * @return $this */ public function setFailedAttempt(int $failedAttempt) { $this->failedAttempt = $failedAttempt; return $this; } /** * set failed attempt to 0 * @return $this */ public function resetFailedAttempt() { $this->setFailedAttempt(0); return $this; } /** * increment failed attempt +1 * @return $this */ public function incrementFailedAttempt() { if(is_null($this->failedAttempt)) $this->failedAttempt =0; $this->failedAttempt++; return $this; } /** * @return int */ public function getFailedAttempt(): ?int { return $this->failedAttempt; } /** * @param bool $logMe * @return $this */ public function setLogMe(bool $logMe) { $this->logMe = $logMe; return $this; } /** * @return bool */ public function isLogMe(): bool { return $this->logMe; } /** * @param \DateTime $birthdate * @return $this|null */ public function setBirthdate(?\DateTime $birthdate) { $this->birthdate = $birthdate; return $this; } /** * @return \DateTime|null */ public function getBirthdate(): ?\DateTime { return $this->birthdate; } /** * @param string $countryCode * @return $this */ public function setCountryCode(string $countryCode) { $this->countryCode = $countryCode; return $this; } /** * @return string|null */ public function getCountryCode() { return $this->countryCode; } /** * @return string */ public function getLocale(){ //todo ... implements dis shit return "en"; } }