Files
myep-team/src/Entity/User.php
T

474 lines
12 KiB
PHP

<?php
namespace App\Entity;
use App\Entity\Traits\SoftDeletableEntity;
use App\Entity\Traits\TimestampableEntity;
use App\Repository\UserRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
class User implements UserInterface, TimestampableEntityInterface, SoftDeletableEntityInterface
{
use TimestampableEntity;
use SoftDeletableEntity;
/**
* Assignable roles and their labels.
*/
public const ROLES = [
'ROLE_ADMIN' => 'Admin',
'ROLE_MANAGER' => 'Reisemanager',
'ROLE_HOUSE_MANAGER' => 'Hausleitung',
'ROLE_TEAMER' => 'Teamer',
];
/**
* Markers for administrative roles a user holds in the BusPro CRM, keyed by the role
* they stand for. They grant no privileges whatsoever and merely make the user show
* up for approval, because administrative roles may only ever be granted manually by
* a super admin.
*/
public const PENDING_ROLES = [
'ROLE_ADMIN' => 'ROLE_ADMIN_PENDING',
'ROLE_MANAGER' => 'ROLE_MANAGER_PENDING',
'ROLE_HOUSE_MANAGER' => 'ROLE_HOUSE_MANAGER_PENDING',
];
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 36, unique: true)]
private string $uuid;
#[ORM\Column]
private ?int $busProAddressId = null;
#[ORM\Column]
private ?int $busProPersonId = null;
#[ORM\Column(length: 255)]
private ?string $firstName = null;
#[ORM\Column(length: 255)]
private ?string $lastName = null;
#[ORM\Column(length: 255)]
private ?string $email = null;
#[ORM\Column(type: 'json')]
private array $roles = [];
#[ORM\Column]
private bool $superAdmin = false;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $lastLoginAt = null;
#[ORM\OneToOne(inversedBy: 'user', cascade: ['all'])]
private ?Teamer $teamer = null;
#[ORM\OneToOne(inversedBy: 'user', cascade: ['all'])]
private ?Contact $contact = null;
#[ORM\Column(type: 'json')]
private array $hotelCodes = [];
#[ORM\Column]
private bool $muteNotifications = false;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $disabledAt = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $disabledReason = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $disabledReasonInternal = null;
public function __construct()
{
$this->uuid = Uuid::v4();
}
public function __toString()
{
return $this->getFullName();
}
public function getId(): ?int
{
return $this->id;
}
public function getUuid(): string
{
return $this->uuid;
}
public function getBusProAddressId(): ?int
{
return $this->busProAddressId;
}
public function setBusProAddressId(int $busProAddressId): static
{
$this->busProAddressId = $busProAddressId;
return $this;
}
public function getBusProPersonId(): ?int
{
return $this->busProPersonId;
}
public function setBusProPersonId(int $busProPersonId): static
{
$this->busProPersonId = $busProPersonId;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): static
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName): static
{
$this->lastName = $lastName;
return $this;
}
public function getFullName(bool $formal = false): string
{
if (true === $formal) {
return sprintf('%s, %s', $this->getLastName(), $this->getFirstName());
}
return sprintf('%s %s', $this->getFirstName(), $this->getLastName());
}
public function getInitials(): string
{
$token = substr($this->getFirstName(), 0, 1).substr($this->getLastName(), 0, 2);
return strtoupper($token);
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): static
{
$this->email = $email;
return $this;
}
public function getRoles(): array
{
$roles = ['ROLE_USER', ...$this->roles];
if (true === $this->isSuperAdmin()) {
$roles[] = 'ROLE_SUPER_ADMIN';
}
return array_unique($roles);
}
public function getRolesLabels(): array
{
$labels = self::ROLES;
foreach (self::PENDING_ROLES as $role => $pendingRole) {
$labels[$pendingRole] = self::ROLES[$role].' (nicht freigeschaltet)';
}
return array_map(
static fn (string $role): string => $labels[$role] ?? $role,
$this->roles,
);
}
public function setRoles(array $roles): static
{
$this->roles = $roles;
return $this;
}
/**
* The manually assignable roles held by the user, i.e. without the implicit ROLE_USER
* and ROLE_SUPER_ADMIN added by getRoles() and without any pending marker. Used to
* edit role assignments: saving them resolves the pending approvals.
*/
public function getAssignedRoles(): array
{
return array_values(array_intersect($this->roles, array_keys(self::ROLES)));
}
/**
* The roles this user is nominated for but does not hold, as role => label.
*
* The markers are storage; this is what an approver acts on, so it is keyed by the real
* role rather than by the marker standing in for it.
*
* @return array<string, string>
*/
public function getNominatedRoles(): array
{
$roles = [];
foreach (self::PENDING_ROLES as $role => $pendingRole) {
if (true === in_array($pendingRole, $this->roles, true)) {
$roles[$role] = self::ROLES[$role];
}
}
return $roles;
}
/**
* The pending markers currently held by the user.
*/
public function getPendingRoles(): array
{
return array_values(array_intersect($this->roles, array_values(self::PENDING_ROLES)));
}
public function setAssignedRoles(array $roles): static
{
return $this->setRoles($roles);
}
public function hasRole(string $role): bool
{
return in_array($role, $this->getRoles());
}
public function isSuperAdmin(): bool
{
return $this->superAdmin;
}
public function setSuperAdmin(bool $superAdmin): static
{
$this->superAdmin = $superAdmin;
return $this;
}
/**
* Super admin is an elevation of ROLE_ADMIN, never a standalone grant.
*/
#[Assert\Callback]
public function validateSuperAdmin(ExecutionContextInterface $context): void
{
if (true === $this->superAdmin && false === in_array('ROLE_ADMIN', $this->roles, true)) {
$context
->buildViolation('Nur Admins können zu Superadmins ernannt werden.')
->atPath('superAdmin')
->addViolation()
;
}
}
public function getLastLoginAt(): ?\DateTimeImmutable
{
return $this->lastLoginAt;
}
public function setLastLoginAt(?\DateTimeImmutable $lastLoginAt): static
{
$this->lastLoginAt = $lastLoginAt;
return $this;
}
public function getDefaultRoute(): string
{
if ($this->hasRole('ROLE_ADMIN')) {
return 'app_admin_index';
} elseif ($this->hasRole('ROLE_MANAGER')) {
return 'app_manager_index';
} elseif ($this->hasRole('ROLE_HOUSE_MANAGER')) {
return 'app_house_manager_index';
}
return 'app_teamer_index';
}
public function eraseCredentials(): void
{
}
public function getUserIdentifier(): string
{
return $this->email;
}
public function getTeamer(): ?Teamer
{
return $this->teamer;
}
public function setTeamer(?Teamer $teamer): static
{
// keep the inverse side in sync: it is only ever hydrated from the database, so
// without this a teamer attached during the request has no user to point back to
$this->teamer?->setUser(null);
$teamer?->setUser($this);
$this->teamer = $teamer;
return $this;
}
public function getContact(): ?Contact
{
return $this->contact;
}
public function setContact(?Contact $contact): static
{
$this->contact = $contact;
return $this;
}
public function getHotelCodes(): array
{
return $this->hotelCodes;
}
public function setHotelCodes(array $hotelCodes): static
{
$this->hotelCodes = $hotelCodes;
return $this;
}
public function isMuteNotifications(): bool
{
return $this->muteNotifications;
}
public function setMuteNotifications(bool $muteNotifications): static
{
$this->muteNotifications = $muteNotifications;
return $this;
}
public function hasHotelCodeMatch(string $hotelCode): bool
{
foreach ($this->hotelCodes as $userHotelCode) {
if (
str_starts_with($hotelCode, $userHotelCode)
|| str_ends_with($hotelCode, $userHotelCode)
) {
return true;
}
}
return false;
}
public function getDisabledAt(): ?\DateTimeImmutable
{
return $this->disabledAt;
}
public function setDisabledAt(?\DateTimeImmutable $disabledAt): static
{
$this->disabledAt = $disabledAt;
return $this;
}
public function isDisabled(): bool
{
return null !== $this->disabledAt;
}
/**
* Blocks or unblocks the account, keeping the reasons in sync. Unchanged state is a
* no-op so that saving an unrelated change does not reset the timestamp.
*/
public function setDisabled(bool $disabled): static
{
if ($disabled === $this->isDisabled()) {
return $this;
}
if (false === $disabled) {
return $this
->setDisabledAt(null)
->setDisabledReason(null)
->setDisabledReasonInternal(null)
;
}
return $this->setDisabledAt(new \DateTimeImmutable());
}
/**
* Whether the account is barred from logging in, for either reason. A block and a
* deletion are independent states: a disciplinary block must survive a deletion and
* the restore that follows it, so the two are only ever combined for decisions,
* never merged into one another.
*/
public function isBlocked(): bool
{
return $this->isDeleted() || $this->isDisabled();
}
public function getDisabledReason(): ?string
{
return $this->disabledReason;
}
public function setDisabledReason(?string $disabledReason): static
{
$this->disabledReason = $disabledReason;
return $this;
}
public function getDisabledReasonInternal(): ?string
{
return $this->disabledReasonInternal;
}
public function setDisabledReasonInternal(?string $disabledReasonInternal): static
{
$this->disabledReasonInternal = $disabledReasonInternal;
return $this;
}
}