<?php
namespace App\Entity\Account;
use App\Entity\Channel\Channel;
use App\Entity\Channel\Group;
use App\Entity\Channel\Service;
use App\Entity\Channel\UserFromChannel;
use App\Entity\ChannelUserData\ChannelUserData;
use App\Entity\Common\Address;
use App\Entity\Common\ImportableData\ImportableDataInterface;
use App\Entity\Common\ImportableData\ImportableDataTrait;
use App\Entity\SyncableWithExcludedPropertiesInterface;
use App\Repository\Account\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use DrosalysWeb\ObjectExtensions\Blame\Model\BlameInterface;
use DrosalysWeb\ObjectExtensions\Blame\Model\BlameTrait;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampInterface;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampTrait;
use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
use Nellapp\Bundle\SDKBundle\Auth\Entity\UserMethodTrait;
use Nellapp\Bundle\SDKBundle\Sync\EntityKeys;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Serializer\Annotation as Serial;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @method self|null getCreatedBy()
* @method self setCreatedBy(self $createdBy)
* @method self|null getUpdatedBy()
* @method self setUpdatedBy(self $updatedBy)
*/
#[UniqueEntity(fields: 'email', message: 'user.entity.email.UniqueEntity', groups: ['register', 'user_edition', 'super_admin_user_edition', 'admin_user_edition', 'flow_channel_user_create_step1'])]
#[UniqueEntity(fields: 'id', message: 'user.entity.id.UniqueEntity', groups: ['register', 'flow_channel_user_create_step1'])]
#[
ORM\Entity(repositoryClass: UserRepository::class),
ORM\Table(name: 'account_users'),
ORM\Index(fields: ['email'], flags: ['fulltext']),
ORM\Index(fields: ['firstName'], flags: ['fulltext']),
ORM\Index(fields: ['lastName'], flags: ['fulltext']),
]
class User implements UserInterface, PasswordAuthenticatedUserInterface, TimestampInterface, BlameInterface, SyncableWithExcludedPropertiesInterface, ImportableDataInterface
{
use BlameTrait;
use TimestampTrait;
use UserMethodTrait;
use ImportableDataTrait;
#[
ORM\Id, ORM\GeneratedValue(strategy: 'CUSTOM'),
ORM\CustomIdGenerator(class: UuidGenerator::class),
ORM\Column(type: 'string', length: 36)
]
#[Serial\Groups(['Profile', 'Manager', 'Sync', 'GlobalJSVariable'])]
private ?string $id = null;
#[ORM\Column(type: 'string', length: 180, unique: true)]
#[
Assert\NotBlank(message: 'user.entity.email.NotBlank', groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step1', 'admin_learner_profile']),
Assert\Email(message: 'user.entity.email.Email', mode: 'strict', groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step1', 'admin_learner_profile']),
Assert\Length(
min: 6,
max: 64,
minMessage: 'user.entity.email.Length.min',
maxMessage: 'user.entity.email.Length.max',
groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step1', 'admin_learner_profile'])
]
#[Serial\Groups(['Profile', 'Manager', 'UserPostManager', 'Sync'])]
private ?string $email = null;
/**
* @var string[]
*/
#[ORM\Column(type: 'json')]
#[Serial\Groups(['Profile', 'Sync'])]
private array $roles = [];
#[ORM\Column(type: 'string')]
#[
Assert\NotBlank(message: 'user.entity.password.NotBlank', groups: ['register', 'user_edition']),
Assert\Length(
min: 8,
max: 255,
minMessage: 'user.entity.password.Length.min',
maxMessage: 'user.entity.password.Length.max',
groups: ['register', 'user_edition']),
Assert\Regex(
pattern: '/(?=.*[0-9])/',
message: 'user.entity.password.Regex.digit',
groups: ['register', 'user_edition']),
Assert\Regex(
pattern: '/(?=.*[A-Z])/',
message: 'user.entity.password.Regex.up',
groups: ['register', 'user_edition']),
Assert\Regex(
pattern: '/(?=.*[a-z])/',
message: 'user.entity.password.Regex.low',
groups: ['register', 'user_edition']),
]
private ?string $password = null;
#[ORM\Column(type: 'boolean')]
#[
Assert\NotNull(message: 'user.entity.enabled.NotNull'),
Assert\Type(type: 'boolean', message: 'user.entity.enabled.Type')
]
private bool $enabled = true;
#[ORM\Column(type: 'string', length: 63, nullable: true)]
private ?string $passwordToken = null;
#[ORM\Column(type: 'datetime', nullable: true)]
private ?\DateTimeInterface $passwordTokenAt = null;
#[ORM\Column(type: 'string', nullable: true)]
#[
Assert\NotBlank(message: 'user.entity.firstName.NotBlank', groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step2', 'admin_learner_profile']),
Assert\Length(
min: 2,
max: 255,
minMessage: 'user.entity.firstName.Length.min',
maxMessage: 'user.entity.firstName.Length.max',
groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step2', 'admin_learner_profile'])
]
#[Serial\Groups(['Profile', 'Manager', 'UserPostManager', 'Sync'])]
private ?string $firstName = null;
#[ORM\Column(type: 'string', nullable: true)]
#[
Assert\NotBlank(message: 'user.entity.lastName.NotBlank', groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step2', 'admin_learner_profile']),
Assert\Length(
min: 2,
max: 255,
minMessage: 'user.entity.lastName.Length.min',
maxMessage: 'user.entity.lastName.Length.max',
groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step2', 'admin_learner_profile'])
]
#[Serial\Groups(['Profile', 'Manager', 'UserPostManager', 'Sync'])]
private ?string $lastName = null;
#[ORM\Column(type: 'string', length: 20, nullable: true)]
#[Assert\Length(
min: 8,
max: 20,
minMessage: 'user.entity.phone.Length.min',
maxMessage: 'user.entity.phone.Length.max',
groups: ['user_edition', 'admin_user_edition', 'super_admin_user_edition', 'flow_channel_user_create_step2', 'admin_learner_profile'])
]
private ?string $phoneNumber = null;
#[ORM\Column(type: 'text', nullable: true)]
#[Serial\Groups(['Profile', 'Sync'])]
private ?string $description = null;
#[ORM\Column(type: 'string', nullable: true)]
#[Assert\Length(max: 255, groups: ['user_edition'])]
private ?string $roleDescription = null;
#[ORM\Column(type: 'boolean', nullable: true)]
private ?bool $avatar = null;
#[ORM\Column(type: 'text', nullable: true)]
#[Serial\Groups(['Sync', 'Manager'])]
private ?string $avatarPath = null;
#[ORM\Column(type: 'string', length: 2, nullable: true)]
#[Assert\Choice(choices: ['m', 'f', 'o'], message: 'user.entity.gender.Choice', groups: ['user_edition', 'admin_user_edition', 'super_admin_user_edition', 'admin_learner_profile'])]
private ?string $gender = null;
#[ORM\Column(type: 'date', nullable: true)]
private ?\DateTimeInterface $birthAt = null;
#[ORM\Embedded(class: Address::class)]
#[Assert\Valid(groups: ['user_edition', 'flow_channel_user_create_step2', 'admin_learner_profile'])]
private ?Address $address = null;
#[ORM\Column(type: 'boolean')]
private bool $isVerified = false;
#[ORM\OneToOne(mappedBy: 'user', targetEntity: UserFromChannel::class, cascade: ['remove'], orphanRemoval: true)]
private ?UserFromChannel $userFromChannel = null;
#[ORM\Column(type: 'datetime', nullable: true)]
#[Serial\Groups(['Profile', 'Sync', 'UserPutSigningDate'])]
private ?\DateTime $signingLimitDate = null;
#[ORM\Column(type: 'datetime', nullable: true)]
private ?\DateTime $lastConnectedAt = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: ChannelUserData::class)]
private ?Collection $channelUserDatas;
#[ORM\ManyToMany(targetEntity: Group::class, mappedBy: 'users', fetch: 'EXTRA_LAZY')]
private ?Collection $groups;
#[ORM\ManyToMany(targetEntity: Service::class, mappedBy: 'users', fetch: 'EXTRA_LAZY')]
private ?Collection $services;
public function __construct()
{
$this->channelUserDatas = new ArrayCollection();
$this->groups = new ArrayCollection();
$this->services = new ArrayCollection();
}
public static function getSyncKey(): string
{
return EntityKeys::USER;
}
public static function getExcludedProperties(): array
{
return [
'lastConnectedAt',
];
}
#[
Serial\SerializedName('username'),
Serial\Groups(['Profile', 'Manager', 'Sync'])
]
public function getUsername(): string
{
return $this;
}
public function getRoles(): array
{
return array_unique(array_merge($this->roles, [
'ROLE_USER',
]));
}
public function setRoles(array $roles): static
{
$this->roles = array_unique(array_values($roles));
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): static
{
$this->password = $password;
return $this;
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): static
{
$this->enabled = $enabled;
return $this;
}
public function toggleEnabled(): static
{
$this->enabled = !$this->enabled;
return $this;
}
public function getPasswordToken(): ?string
{
return $this->passwordToken;
}
public function setPasswordToken(?string $passwordToken): static
{
$this->passwordToken = $passwordToken;
return $this;
}
public function getPasswordTokenAt(): ?\DateTimeInterface
{
return $this->passwordTokenAt;
}
public function setPasswordTokenAt(?\DateTimeInterface $passwordTokenAt): static
{
$this->passwordTokenAt = $passwordTokenAt;
return $this;
}
public function getPhoneNumber(): ?string
{
return $this->phoneNumber;
}
public function setPhoneNumber(?string $phoneNumber): static
{
$this->phoneNumber = $phoneNumber;
return $this;
}
public function getRoleDescription(): ?string
{
return $this->roleDescription;
}
public function setRoleDescription(?string $roleDescription): static
{
$this->roleDescription = $roleDescription;
return $this;
}
public function getGender(): ?string
{
return $this->gender;
}
public function setGender(?string $gender): static
{
$this->gender = $gender;
return $this;
}
public function getBirthAt(): ?\DateTimeInterface
{
return $this->birthAt;
}
public function setBirthAt(?\DateTimeInterface $birthAt): static
{
$this->birthAt = $birthAt;
return $this;
}
/**
* @return \DateTime|null
*/
public function getLastConnectedAt(): ?\DateTime
{
return $this->lastConnectedAt;
}
/**
* @param \DateTime|null $lastConnectedAt
*/
public function setLastConnectedAt(?\DateTime $lastConnectedAt): static
{
$this->lastConnectedAt = $lastConnectedAt;
return $this;
}
public function getAddress(): Address
{
return $this->address ?: $this->address = new Address();
}
public function setAddress(Address $address): static
{
$this->address = $address;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): static
{
$this->isVerified = $isVerified;
return $this;
}
public function getGroups(): ?Collection
{
return $this->groups;
}
public function getGroupsByChannel(Channel $channel): ?array
{
return array_filter($this->groups->toArray(), function (Group $group) use ($channel) {
return $group->getChannel() === $channel;
});
}
public function setGroups(?Collection $groups): User
{
$this->groups = $groups;
return $this;
}
public function getServices(): ?Collection
{
return $this->services;
}
public function getUserFromChannel(): ?UserFromChannel
{
return $this->userFromChannel;
}
public function setUserFromChannel(?UserFromChannel $userFromChannel): self
{
$this->userFromChannel = $userFromChannel;
return $this;
}
public function serialize(): ?string
{
return serialize([
$this->id,
$this->email,
$this->password,
$this->enabled,
]);
}
public function unserialize($data): void
{
list(
$this->id,
$this->email,
$this->password,
$this->enabled
) = unserialize($data);
}
}