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) {
86 //Extract names from mail
87 $names = explode(' ', ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $mail))))));
91 $this->forename
= $names[0];
92 $this->surname
= $names[1]??$names[0];
93 $this->password
= $mail;
94 $this->active
= false;
95 $this->disabled
= false;
96 $this->created
= new \
DateTime('now');
97 $this->updated
= new \
DateTime('now');
100 $this->groups
= new ArrayCollection();
108 public function getId(): int {
115 * @param string $mail
119 public function setMail(string $mail): User
{
130 public function getMail(): string {
137 * @param string $forename
141 public function setForename(string $forename): User
{
142 $this->forename
= $forename;
152 public function getForename(): string {
153 return $this->forename
;
159 * @param string $surname
163 public function setSurname(string $surname): User
{
164 $this->surname
= $surname;
174 public function getSurname(): string {
175 return $this->surname
;
181 * @param string $password
185 public function setPassword(string $password): User
{
186 $this->password
= $password;
198 public function getPassword(): ?string {
199 return $this->password
;
205 * @param bool $active
209 public function setActive(bool $active): User
{
210 $this->active
= $active;
220 public function getActive(): bool {
221 return $this->active
;
227 * @param bool $disabled
231 public function setDisabled(bool $disabled): User
{
232 $this->disabled
= $disabled;
242 public function getDisabled(): bool {
243 return $this->disabled
;
249 * @param \DateTime $created
253 public function setCreated(\DateTime
$created): User
{
254 $this->created
= $created;
264 public function getCreated(): \DateTime
{
265 return $this->created
;
271 * @param \DateTime $updated
275 public function setUpdated(\DateTime
$updated): User
{
276 $this->updated
= $updated;
286 public function getUpdated(): \DateTime
{
287 return $this->updated
;
293 public function setCivility(Civility
$civility): User
{
294 $this->civility
= $civility;
302 public function getCivility(): ?Civility
{
303 return $this->civility
;
309 * @param Group $group
313 public function addGroup(Group
$group) {
314 $this->groups
[] = $group;
322 * @param Group $group
324 public function removeGroup(Group
$group) {
325 $this->groups
->removeElement($group);
331 * @return ArrayCollection
333 public function getGroups(): ArrayCollection
{
334 return $this->groups
;
340 public function getRoles(): array {
341 //Get the unique roles list by id
342 return array_unique(array_reduce(
343 //Cast groups as array
344 $this->groups
->toArray(),
345 //Reduce to an array of id => group tuples
346 function ($array, $group) {
347 $array[$group->getId()] = $group->getRole();
350 //Init with empty array
351 //XXX: on registration, add each group present in rapsys_user.default.group array to user
352 //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450
360 public function getRole(): ?string {
362 $roles = $this->getRoles();
364 //With roles array empty
370 //Return the role with max id
371 //XXX: should be rewriten if it change in your configuration
372 return $roles[array_reduce(
374 function($cur, $id) {
375 if ($cur === null || $id > $cur) {
387 public function getSalt(): ?string {
388 //No salt required with bcrypt
395 public function getUsername(): string {
402 public function eraseCredentials(): void {}
407 public function serialize(): string {
424 public function unserialize($serialized) {
435 ) = unserialize($serialized);
439 * Check if account is activated
441 * It was from deprecated AdvancedUserInterface, see if it's used anymore
443 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
445 public function isActivated(): bool {
446 return $this->active
;
450 * Check if account is disabled
452 * It was from deprecated AdvancedUserInterface, see if it's used anymore
454 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
456 public function isDisabled(): bool {
457 return $this->disabled
;
463 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
464 //Check that we have an user instance
465 if (($user = $eventArgs->getEntity()) instanceof User
) {
467 $user->setUpdated(new \
DateTime('now'));
472 * Returns a string representation of the user
476 public function __toString(): string {
477 return $this->civility
.' '.$this->forename
.' '.$this->surname
;