<?php
namespace App\DTOs;
use App\Entity\Internal\TwoFaConfig;
use App\Entity\Monolith\User;
use App\Entity\Monolith\UserRole;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
class UserMe implements UserInterface, PasswordAuthenticatedUserInterface
{
#[Groups(['user:read'])]
protected User $user;
#[Groups(['user:read'])]
protected array $kindergartens;
#[Groups(['user:read'])]
protected array $groups;
#[Groups(['user:read'])]
protected array $childrenIds;
#[Groups(['user:read'])]
protected string $type;
#[Groups(['user:read'])]
protected ?array $modulesAccess;
#[Groups(['user:read'])]
protected ?string $kgRole;
#[Groups(['user:read'])]
protected string $twoFaStatus;
protected int $kindergartenIdHeader;
protected ?string $userTypeHeader;
protected ?int $childIdHeader;
public static function map(array $user): self
{
$result = (new self())
->setChildrenIds(array_map('intval', array_unique(array_filter(explode(',', $user['my_children'])))))
->setType($user['type'])
->setModulesAccess(json_decode($user['modules_access'], true))
->setKgRole($user['role_id'])
;
unset($user['my_children'], $user['modules_access'], $user['type'], $user['role_id']);
$userEntity = new User();
foreach ($user as $field => $value) {
$setter = 'set' . implode('', array_map('ucfirst', explode('_', $field)));;
$userEntity->$setter($value);
}
return $result->setUser($userEntity);
}
public function getKgRole(): ?string
{
return $this->kgRole;
}
public function setKgRole(?string $kgRole): UserMe
{
$this->kgRole = $kgRole;
return $this;
}
public function getKindergartens(): array
{
return $this->kindergartens;
}
public function setKindergartens(array $kindergartens): UserMe
{
$this->kindergartens = $kindergartens;
return $this;
}
public function getGroups(): array
{
return $this->groups;
}
public function setGroups(array $groups): UserMe
{
$this->groups = $groups;
return $this;
}
public function isAdmin(): bool
{
return UserRole::ROLE_ADMIN == $this->getType();
}
public function isManager(): bool
{
return UserRole::ROLE_ADMIN == $this->getType() &&
(empty($this->kgRole) || 'manager' == $this->kgRole);
}
public function getKindergartenIdHeader(): int
{
return $this->kindergartenIdHeader;
}
public function setKindergartenIdHeader(int $kindergartenIdHeader): UserMe
{
$this->kindergartenIdHeader = $kindergartenIdHeader;
return $this;
}
public function getUserTypeHeader(): ?string
{
return $this->userTypeHeader;
}
public function setUserTypeHeader(?string $userTypeHeader): UserMe
{
$this->userTypeHeader = $userTypeHeader;
return $this;
}
public function getChildIdHeader(): ?int
{
return $this->childIdHeader;
}
public function setChildIdHeader(?int $childIdHeader): UserMe
{
$this->childIdHeader = $childIdHeader;
return $this;
}
public function getModulesAccess(): ?array
{
return $this->modulesAccess;
}
public function setModulesAccess(?array $modulesAccess): UserMe
{
$this->modulesAccess = $modulesAccess;
return $this;
}
public function getUser(): User
{
return $this->user;
}
public function setUser(User $user): UserMe
{
$this->user = $user;
return $this;
}
#[Groups(['user:read'])]
public function getKindergartenIds(): array
{
return array_keys($this->kindergartens);
}
#[Groups(['user:read'])]
public function getGroupIds(): array
{
return array_keys($this->groups);
}
public function getActiveGroupIds(): array
{
return array_keys($this->getActiveGroups());
}
public function getActiveGroups(): array
{
return array_filter($this->groups, function (array $group) {
return is_null($group[1]) || (strtotime($group[0]) <= time() && strtotime($group[1]) >= time());
});
}
public function getChildrenIds(): array
{
return $this->childrenIds;
}
public function setChildrenIds(array $childrenIds): UserMe
{
$this->childrenIds = $childrenIds;
return $this;
}
public function getType(): string
{
return $this->type;
}
public function setType(string $type): UserMe
{
$this->type = $type;
return $this;
}
public function getRoles(): array
{
$roles = ['ROLE_USER'];
return $roles;
}
public function eraseCredentials()
{
}
public function getUserIdentifier(): string
{
return $this->user->getEmail();
}
public function getPassword(): string
{
if (
isset($_SERVER['HTTP_X_SSO_KEY']) &&
$_ENV['APP_SECRET'] == $_SERVER['HTTP_X_SSO_KEY']
) {
return $_ENV['SSO_MASTER_PASS'];
}
return $this->user->getPassword();
}
public function getTwoFaStatus(): string
{
return $this->twoFaStatus;
}
public function setTwoFaStatus(int $twoFaStatus): UserMe
{
if (!isset(TwoFaConfig::STATUS_MAP[$twoFaStatus])) {
throw new \Exception(sprintf("Status %s not allowed.", $twoFaStatus));
}
$this->twoFaStatus = TwoFaConfig::STATUS_MAP[$twoFaStatus];
return $this;
}
}