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
{
86 * @var ArrayCollection
93 * @param string $mail The user mail
95 public function __construct(string $mail) {
97 $this->active
= false;
98 $this->disabled
= false;
99 $this->groups
= new ArrayCollection();
107 public function getId(): int {
114 * @param string $mail
118 public function setMail(string $mail): User
{
129 public function getMail(): ?string {
136 * @param string $pseudonym
140 public function setPseudonym(string $pseudonym): User
{
141 $this->pseudonym
= $pseudonym;
151 public function getPseudonym(): ?string {
152 return $this->pseudonym
;
158 * @param string $forename
162 public function setForename(string $forename): User
{
163 $this->forename
= $forename;
173 public function getForename(): ?string {
174 return $this->forename
;
180 * @param string $surname
184 public function setSurname(string $surname): User
{
185 $this->surname
= $surname;
195 public function getSurname(): ?string {
196 return $this->surname
;
202 * @param string $password
206 public function setPassword(string $password): User
{
207 $this->password
= $password;
219 public function getPassword(): ?string {
220 return $this->password
;
226 * @param string $slug
230 public function setSlug(?string $slug): User
{
241 public function getSlug(): ?string {
248 * @param bool $active
252 public function setActive(bool $active): User
{
253 $this->active
= $active;
263 public function getActive(): bool {
264 return $this->active
;
270 * @param bool $disabled
274 public function setDisabled(bool $disabled): User
{
275 $this->disabled
= $disabled;
285 public function getDisabled(): bool {
286 return $this->disabled
;
292 * @param \DateTime $created
296 public function setCreated(\DateTime
$created): User
{
297 $this->created
= $created;
307 public function getCreated(): \DateTime
{
308 return $this->created
;
314 * @param \DateTime $updated
318 public function setUpdated(\DateTime
$updated): User
{
319 $this->updated
= $updated;
329 public function getUpdated(): \DateTime
{
330 return $this->updated
;
336 public function setCivility(Civility
$civility): User
{
337 $this->civility
= $civility;
345 public function getCivility(): ?Civility
{
346 return $this->civility
;
352 * @param Group $group
356 public function addGroup(Group
$group) {
357 $this->groups
[] = $group;
365 * @param Group $group
367 public function removeGroup(Group
$group) {
368 $this->groups
->removeElement($group);
374 * @return ArrayCollection
376 public function getGroups(): ArrayCollection
{
377 return $this->groups
;
383 public function getRoles(): array {
384 //Get the unique roles list by id
385 return array_unique(array_reduce(
386 //Cast groups as array
387 $this->groups
->toArray(),
388 //Reduce to an array of id => group tuples
389 function ($array, $group) {
390 $array[$group->getId()] = $group->getRole();
393 //Init with empty array
394 //XXX: on registration, add each group present in rapsys_user.default.group array to user
395 //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450
403 public function getRole(): ?string {
405 $roles = $this->getRoles();
407 //With roles array empty
413 //Return the role with max id
414 //XXX: should be rewriten if it change in your configuration
415 return $roles[array_reduce(
417 function($cur, $id) {
418 if ($cur === null || $id > $cur) {
430 public function getSalt(): ?string {
431 //No salt required with bcrypt
438 public function getUsername(): string {
445 public function eraseCredentials(): void {}
447 public function serialize(): string {
459 public function unserialize($serialized) {
468 ) = unserialize($serialized);
472 * Check if account is activated
474 * It was from deprecated AdvancedUserInterface, see if it's used anymore
476 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
478 public function isActivated(): bool {
479 return $this->active
;
483 * Check if account is disabled
485 * It was from deprecated AdvancedUserInterface, see if it's used anymore
487 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
489 public function isDisabled(): bool {
490 return $this->disabled
;
496 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
497 //Check that we have an user instance
498 if (($user = $eventArgs->getEntity()) instanceof User
) {
500 $user->setUpdated(new \
DateTime('now'));
505 * Returns a string representation of the user
509 public function __toString(): string {
510 return $this->civility
.' '.$this->forename
.' '.$this->surname
;