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
;
18 use Symfony\Component\Security\Core\User\UserInterface
;
19 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
;
21 use Rapsys\UserBundle\Entity\Civility
;
22 use Rapsys\UserBundle\Entity\Group
;
27 class User
implements UserInterface
, PasswordAuthenticatedUserInterface
{
31 protected ?int $id = null;
36 protected \DateTime
$created;
41 protected \DateTime
$updated;
46 protected Collection
$groups;
51 * @param string $mail The user mail
52 * @param string $password The user password
53 * @param ?Civility $civility The user civility
54 * @param ?string $forename The user forename
55 * @param ?string $surname The user surname
56 * @param bool $active The user active
57 * @param bool $enable The user enable
59 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) {
61 $this->created
= new \
DateTime('now');
62 $this->updated
= new \
DateTime('now');
65 $this->groups
= new ArrayCollection();
73 public function getId(): ?int {
83 public function setMail(string $mail): User
{
95 public function getMail(): string {
102 * @param ?string $forename
106 public function setForename(?string $forename): User
{
107 $this->forename
= $forename;
117 public function getForename(): ?string {
118 return $this->forename
;
124 * @param ?string $surname
128 public function setSurname(?string $surname): User
{
129 $this->surname
= $surname;
139 public function getSurname(): ?string {
140 return $this->surname
;
146 * @param string $password
150 public function setPassword(string $password): User
{
152 $this->password
= $password;
164 public function getPassword(): string {
165 return $this->password
;
171 * @param bool $active
175 public function setActive(bool $active): User
{
176 $this->active
= $active;
186 public function getActive(): bool {
187 return $this->active
;
193 * @param bool $enable
197 public function setEnable(bool $enable): User
{
198 $this->enable
= $enable;
208 public function getEnable(): bool {
209 return $this->enable
;
215 * @param \DateTime $created
219 public function setCreated(\DateTime
$created): User
{
220 $this->created
= $created;
230 public function getCreated(): \DateTime
{
231 return $this->created
;
237 * @param \DateTime $updated
241 public function setUpdated(\DateTime
$updated): User
{
242 $this->updated
= $updated;
252 public function getUpdated(): \DateTime
{
253 return $this->updated
;
259 public function setCivility(?Civility
$civility = null): User
{
260 $this->civility
= $civility;
268 public function getCivility(): ?Civility
{
269 return $this->civility
;
275 * @param Group $group
279 public function addGroup(Group
$group): User
{
280 $this->groups
[] = $group;
288 * @param Group $group
290 * @return Doctrine\Common\Collections\Collection
292 public function removeGroup(Group
$group): Collection
{
293 return $this->groups
->removeElement($group);
299 * @return Doctrine\Common\Collections\Collection
301 public function getGroups(): Collection
{
302 return $this->groups
;
308 public function getRoles(): array {
309 //Get the unique roles list by id
310 return array_unique(array_reduce(
311 //Cast groups as array
312 $this->groups
->toArray(),
313 //Reduce to an array of id => group tuples
314 function ($array, $group) {
315 $array[$group->getId()] = $group->getRole();
318 //Init with empty array
319 //XXX: on registration, add each group present in rapsysuser.default.group array to user
320 //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450
328 public function getRole(): ?string {
330 $roles = $this->getRoles();
332 //With roles array empty
338 //Return the role with max id
339 //XXX: should be rewriten if it change in your configuration
340 return $roles[array_reduce(
342 function($cur, $id) {
343 if ($cur === null || $id > $cur) {
355 public function getSalt(): ?string {
356 //No salt required with bcrypt
363 public function getUsername(): string {
370 public function getUserIdentifier(): string {
377 public function eraseCredentials(): void {}
382 public function __serialize(): array {
399 public function __unserialize(array $data): void {
414 * Check if account is activated
416 * @see vendor/rapsys/userbundle/Checker/UserChecker.php
418 public function isActivated(): bool {
419 return $this->active
;
423 * Check if account is enabled
425 * @see vendor/symfony/security-core/User/InMemoryUserChecker.php
427 public function isEnabled(): bool {
428 return $this->enable
;
434 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
435 //Check that we have an user instance
436 if (($user = $eventArgs->getObject()) instanceof User
) {
438 $user->setUpdated(new \
DateTime('now'));
443 * Returns a recipient name of the user
447 public function getRecipientName(): string {
448 //Without forename and surname
449 if (empty($this->forename
) && empty($this->surname
)) {
450 //Return recipient name from mail
451 return ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $this->mail
)))));
454 //Return recipient name from forename and surname
455 return implode(' ', [$this->forename
, $this->surname
]);
459 * Returns a string representation of the user
463 public function __toString(): string {
464 return $this->civility
.' '.$this->forename
.' '.$this->surname
;