1 <?php
declare(strict_types
=1);
4 * this file is part of the rapsys packbundle package.
6 * (c) raphaël gertz <symfony@rapsys.eu>
8 * for the full copyright and license information, please view the license
9 * file that was distributed with this source code.
12 namespace Rapsys\UserBundle\Entity
;
14 use Doctrine\Common\Collections\Collection
;
15 use Doctrine\Common\Collections\ArrayCollection
;
16 use Doctrine\ORM\Event\PreUpdateEventArgs
;
17 use Symfony\Component\Security\Core\User\UserInterface
;
18 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
;
20 use Rapsys\UserBundle\Entity\Civility
;
21 use Rapsys\UserBundle\Entity\Group
;
26 class User
implements UserInterface
, PasswordAuthenticatedUserInterface
{
35 protected \DateTime
$created;
40 protected \DateTime
$updated;
43 * @var Doctrine\Common\Collections\Collection
45 protected Collection
$groups;
50 * @param string $mail The user mail
51 * @param string $password The user password
52 * @param ?Civility $civility The user civility
53 * @param ?string $forename The user forename
54 * @param ?string $surname The user surname
55 * @param bool $active The user active
56 * @param bool $enable The user enable
58 public function __construct(protected string $mail, protected string $password, protected ?Civility
$civility = null, protected ?string $forename = null, protected ?string $surname = null, protected bool $active = false, protected bool $enable = true) {
60 $this->created
= new \
DateTime('now');
61 $this->updated
= new \
DateTime('now');
64 $this->groups
= new ArrayCollection();
72 public function getId(): ?int {
82 public function setMail(string $mail): User
{
94 public function getMail(): string {
101 * @param ?string $forename
105 public function setForename(?string $forename): User
{
106 $this->forename
= $forename;
116 public function getForename(): ?string {
117 return $this->forename
;
123 * @param ?string $surname
127 public function setSurname(?string $surname): User
{
128 $this->surname
= $surname;
138 public function getSurname(): ?string {
139 return $this->surname
;
145 * @param string $password
149 public function setPassword(string $password): User
{
151 $this->password
= $password;
163 public function getPassword(): string {
164 return $this->password
;
170 * @param bool $active
174 public function setActive(bool $active): User
{
175 $this->active
= $active;
185 public function getActive(): bool {
186 return $this->active
;
192 * @param bool $enable
196 public function setEnable(bool $enable): User
{
197 $this->enable
= $enable;
207 public function getEnable(): bool {
208 return $this->enable
;
214 * @param \DateTime $created
218 public function setCreated(\DateTime
$created): User
{
219 $this->created
= $created;
229 public function getCreated(): \DateTime
{
230 return $this->created
;
236 * @param \DateTime $updated
240 public function setUpdated(\DateTime
$updated): User
{
241 $this->updated
= $updated;
251 public function getUpdated(): \DateTime
{
252 return $this->updated
;
258 public function setCivility(?Civility
$civility = null): User
{
259 $this->civility
= $civility;
267 public function getCivility(): ?Civility
{
268 return $this->civility
;
274 * @param Group $group
278 public function addGroup(Group
$group): User
{
279 $this->groups
[] = $group;
287 * @param Group $group
289 * @return Doctrine\Common\Collections\Collection
291 public function removeGroup(Group
$group): Collection
{
292 return $this->groups
->removeElement($group);
298 * @return Doctrine\Common\Collections\Collection
300 public function getGroups(): Collection
{
301 return $this->groups
;
307 public function getRoles(): array {
308 //Get the unique roles list by id
309 return array_unique(array_reduce(
310 //Cast groups as array
311 $this->groups
->toArray(),
312 //Reduce to an array of id => group tuples
313 function ($array, $group) {
314 $array[$group->getId()] = $group->getRole();
317 //Init with empty array
318 //XXX: on registration, add each group present in rapsys_user.default.group array to user
319 //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450
327 public function getRole(): ?string {
329 $roles = $this->getRoles();
331 //With roles array empty
337 //Return the role with max id
338 //XXX: should be rewriten if it change in your configuration
339 return $roles[array_reduce(
341 function($cur, $id) {
342 if ($cur === null || $id > $cur) {
354 public function getSalt(): ?string {
355 //No salt required with bcrypt
362 public function getUsername(): string {
369 public function getUserIdentifier(): string {
376 public function eraseCredentials(): void {}
381 public function __serialize(): array {
398 public function __unserialize(array $data): void {
413 * Check if account is activated
415 * @see vendor/rapsys/userbundle/Checker/UserChecker.php
417 public function isActivated(): bool {
418 return $this->active
;
422 * Check if account is enabled
424 * @see vendor/symfony/security-core/User/InMemoryUserChecker.php
426 public function isEnabled(): bool {
427 return $this->enable
;
433 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
434 //Check that we have an user instance
435 if (($user = $eventArgs->getEntity()) instanceof User
) {
437 $user->setUpdated(new \
DateTime('now'));
442 * Returns a recipient name of the user
446 public function getRecipientName(): string {
447 //Without forename and surname
448 if (empty($this->forename
) && empty($this->surname
)) {
449 //Return recipient name from mail
450 return ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $this->mail
)))));
453 //Return recipient name from forename and surname
454 return implode(' ', [$this->forename
, $this->surname
]);
458 * Returns a string representation of the user
462 public function __toString(): string {
463 return $this->civility
.' '.$this->forename
.' '.$this->surname
;