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\ArrayCollection
;
15 use Doctrine\ORM\Event\PreUpdateEventArgs
;
16 use Symfony\Component\Security\Core\User\UserInterface
;
18 use Rapsys\UserBundle\Entity\Civility
;
19 use Rapsys\UserBundle\Entity\Group
;
24 class User
implements UserInterface
, \Serializable
{
76 * @var ArrayCollection
83 * @param string $mail The user mail
85 public function __construct(string $mail) {
87 if (!empty($this->mail
= $mail)) {
88 //Extract names from mail
89 $names = explode(' ', ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $mail))))));
90 $this->forename
= $names[0];
91 $this->surname
= $names[1]??$names[0];
96 $this->password
= $mail;
97 $this->active
= false;
98 $this->disabled
= false;
99 $this->created
= new \
DateTime('now');
100 $this->updated
= new \
DateTime('now');
103 $this->groups
= new ArrayCollection();
111 public function getId(): int {
118 * @param string $mail
122 public function setMail(string $mail): User
{
124 if (!empty($this->mail
= $mail)) {
125 //Without forename and surname
126 if (empty($this->forename
) && empty($this->surname
)) {
127 //Extract names from mail
128 $names = explode(' ', ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $mail))))));
130 $this->forename
= $names[0];
132 $this->surname
= $names[1]??$names[0];
144 public function getMail(): string {
151 * @param string $forename
155 public function setForename(string $forename): User
{
156 $this->forename
= $forename;
166 public function getForename(): string {
167 return $this->forename
;
173 * @param string $surname
177 public function setSurname(string $surname): User
{
178 $this->surname
= $surname;
188 public function getSurname(): string {
189 return $this->surname
;
195 * @param string $password
199 public function setPassword(string $password): User
{
200 $this->password
= $password;
212 public function getPassword(): ?string {
213 return $this->password
;
219 * @param bool $active
223 public function setActive(bool $active): User
{
224 $this->active
= $active;
234 public function getActive(): bool {
235 return $this->active
;
241 * @param bool $disabled
245 public function setDisabled(bool $disabled): User
{
246 $this->disabled
= $disabled;
256 public function getDisabled(): bool {
257 return $this->disabled
;
263 * @param \DateTime $created
267 public function setCreated(\DateTime
$created): User
{
268 $this->created
= $created;
278 public function getCreated(): \DateTime
{
279 return $this->created
;
285 * @param \DateTime $updated
289 public function setUpdated(\DateTime
$updated): User
{
290 $this->updated
= $updated;
300 public function getUpdated(): \DateTime
{
301 return $this->updated
;
307 public function setCivility(Civility
$civility): User
{
308 $this->civility
= $civility;
316 public function getCivility(): ?Civility
{
317 return $this->civility
;
323 * @param Group $group
327 public function addGroup(Group
$group) {
328 $this->groups
[] = $group;
336 * @param Group $group
338 public function removeGroup(Group
$group) {
339 $this->groups
->removeElement($group);
345 * @return ArrayCollection
347 public function getGroups(): ArrayCollection
{
348 return $this->groups
;
354 public function getRoles(): array {
355 //Get the unique roles list by id
356 return array_unique(array_reduce(
357 //Cast groups as array
358 $this->groups
->toArray(),
359 //Reduce to an array of id => group tuples
360 function ($array, $group) {
361 $array[$group->getId()] = $group->getRole();
364 //Init with empty array
365 //XXX: on registration, add each group present in rapsys_user.default.group array to user
366 //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450
374 public function getRole(): ?string {
376 $roles = $this->getRoles();
378 //With roles array empty
384 //Return the role with max id
385 //XXX: should be rewriten if it change in your configuration
386 return $roles[array_reduce(
388 function($cur, $id) {
389 if ($cur === null || $id > $cur) {
401 public function getSalt(): ?string {
402 //No salt required with bcrypt
409 public function getUsername(): string {
416 public function eraseCredentials(): void {}
421 public function serialize(): string {
438 public function unserialize($serialized) {
449 ) = unserialize($serialized);
453 * Check if account is activated
455 * It was from deprecated AdvancedUserInterface, see if it's used anymore
457 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
459 public function isActivated(): bool {
460 return $this->active
;
464 * Check if account is disabled
466 * It was from deprecated AdvancedUserInterface, see if it's used anymore
468 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
470 public function isDisabled(): bool {
471 return $this->disabled
;
477 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
478 //Check that we have an user instance
479 if (($user = $eventArgs->getEntity()) instanceof User
) {
481 $user->setUpdated(new \
DateTime('now'));
486 * Returns a string representation of the user
490 public function __toString(): string {
491 return $this->civility
.' '.$this->forename
.' '.$this->surname
;