<?php
/*
* Eventfix - User.php
* --------------------------------------------------------------------------
* Created by: mhack
* Created on: 15.1.2024
* --------------------------------------------------------------------------
* Copyright (c) 2024 | Michael Hack Software e.K. | www.mh-s.de
*/
namespace App\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* User
* @ORM\Table(name="users")
* @ORM\Entity
*/
class User extends Base\User implements UserInterface, PasswordAuthenticatedUserInterface, \Serializable {
public function __toString() {
return trim($this->firstname . ' ' . $this->lastname);
}
/**
* Zugangsdaten löschen
*/
public function eraseCredentials() {
}
/**
* Gebuchtes, aktives Paket des Benutzers
* @return UserPlan|null
*/
public function getActivePlan() : ?UserPlan {
$result = $this->plans->matching(
Criteria::create()->andWhere(Criteria::expr()->eq('state', UserPlan::STATE_ACTIVE))
);
return $result->count() ? $result->first() : null;
}
/**
* Offene Rechnung ein es Typs
* @param string $type
* @return Invoice|null
*/
public function getOpenInvoice(string $type = Invoice::TYPE_INVOICE): ?Invoice {
$result = $this->invoices->matching(
Criteria::create()
->andWhere(Criteria::expr()->eq('type', $type))
->andWhere(Criteria::expr()->eq('state', Invoice::STATE_OPEN))
);
return $result->count() ? $result->first() : null;
}
/**
* Vollständiger Name
* @return string
*/
public function getFullname() : string {
return trim("{$this->firstname} {$this->lastname}");
}
/**
* Alle tatsächlich ausgeführten Rechnungen
* @return Invoice[]|Collection
*/
public function getInvoices() : Collection {
return $this->invoices->matching(
Criteria::create()->where(Criteria::expr()->neq('state', Invoice::STATE_OPEN))
);
}
/**
* (Aktuelles) Lastschriftmandat ermitteln
* @param int $id
* @return UserMandate|null
*/
public function getMandate(int $id = 0) : ?UserMandate {
// Einen spezfisches Mandat
if ($id) {
return $this->_getItem($this->mandates, $id);
}
// Aktuelles Mandat
$result = $this->mandates->matching(
Criteria::create()->where(Criteria::expr()->eq('state', UserMandate::STATE_ACTIVE))
);
return $result->count() ? $result->first() : null;
}
/**
* Erstes (und aktuell einziges) Profil zurückgeben
* @param int $id
* @return UserProfile|null
*/
public function getProfile(int $id = 0) : ?UserProfile {
if ($id) {
return parent::getProfile($id);
}
if ($this->profiles->isEmpty()) {
return null;
}
return $this->profiles->first();
}
/**
* Berechtigung zur Authentifizierung
* @return array
*/
public function getRoles() {
$roles = [];
switch ($this->state) {
case self::STATE_NEW:
$roles[] = 'ROLE_NEW';
break;
case self::STATE_ACTIVE:
$roles[] = 'ROLE_USER';
if ($this->is_admin) {
$roles[] = 'ROLE_ADMIN';
}
break;
case self::STATE_DELETED:
$roles[] = 'ROLE_ARCHIVE';
break;
}
return $roles;
}
/**
* Anrede der Person
* @return string
*/
public function getSalutation() : string {
return "Sehr geehrte/r " . trim($this->firstname . ' ' . $this->lastname);
}
/**
* Status ermitteln
* @return string
*/
public function getState() : string {
if ($this->is_locked) {
return User::STATE_LOCKED;
}
return parent::getState();
}
/**
* Aktive Vertragsbedingungen
* @return UserTerm[]|Collection
*/
public function getTerms() : Collection {
return $this->terms->matching(
Criteria::create()->where(Criteria::expr()->neq('state', UserTerm::STATE_ACTIVE))
);
}
/**
* Offene Todos
* @return UserTodo[]|Collection
*/
public function getTodos() : Collection {
$criteria = Criteria::create()->where(Criteria::expr()->eq('is_done', false));
return $this->todos->matching($criteria);
}
/**
* Benutzername zur Authentifizierung
* @return string
*/
public function getUserIdentifier() {
return $this->username;
}
/**
* Wartendes, nächstes Paket des Benutzers
* @return UserPlan|null
*/
public function getWaitingPlan() : ?UserPlan {
$result = $this->plans->matching(
Criteria::create()->andWhere(Criteria::expr()->eq('state', UserPlan::STATE_NEW))
);
return $result->count() ? $result->first() : null;
}
/**
* Hatte der Benutzer einen Probezeitraum?
* @return bool
*/
public function hasTrial() : bool {
$result = $this->plans->matching(
Criteria::create()->andWhere(Criteria::expr()->eq('is_trial', true))
);
return $result->count() > 0;
}
/**
* Aktiver, abrechnungsfähiger Benutzer
* @return bool
*/
public function isActive() : bool {
if (!$this->username) return false;
return $this->state == self::STATE_ACTIVE;
}
/**
* Deaktivierter und eingeschränkter Benutzer
* @return bool
*/
public function isDisabled() : bool {
// Kein Benutzername hinterlegt
if (!$this->username) return true;
// Gesperrt, Deaktiviert bzw in Löschung
if ($this->is_locked) return true;
if ($this->state == self::STATE_DEACTIVATED) return true;
if ($this->state == self::STATE_DELETED) return true;
return false;
}
/**
* Serializer für die Session
* @return string
*/
public function serialize() {
return serialize([
$this->id,
$this->firstname,
$this->lastname,
$this->username,
$this->password,
$this->state,
$this->is_admin,
$this->is_locked
]);
}
/**
* Passwort setzen
*
* @param string|null $password
*
* @return User
*/
public function setPassword($password) : self {
$this->password = password_hash($password, PASSWORD_BCRYPT);
return $this;
}
/**
* Status setzen
* @param string $state
*
* @return User
*/
public function setState(string $state) {
if ($state == self::STATE_LOCKED) {
$this->setIsLocked(true);
return $this;
}
$this->setIsLocked(false);
return parent::setState($state);
}
/**
* Unserializer für die Session
* @param string $data
*/
public function unserialize($data) {
[
$this->id,
$this->firstname,
$this->lastname,
$this->username,
$this->password,
$this->state,
$this->is_admin,
$this->is_locked
] = unserialize($data);
}
// ---------------------------------------------------------------------------------------------
// Deprecated
// ---------------------------------------------------------------------------------------------
public function getSalt() {
return '';
}
}