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 $this->password
= $mail;
96 $this->active
= false;
97 $this->disabled
= false;
98 $this->created
= new \
DateTime('now');
99 $this->updated
= new \
DateTime('now');
102 $this->groups
= new ArrayCollection();
110 public function getId(): int {
117 * @param string $mail
121 public function setMail(string $mail): User
{
123 if (!empty($this->mail
= $mail)) {
125 if (empty($this->password
)) {
126 //Set mail as password
127 $this->password
= $mail;
139 public function getMail(): string {
146 * @param string $forename
150 public function setForename(string $forename): User
{
151 $this->forename
= $forename;
161 public function getForename(): string {
162 return $this->forename
;
168 * @param string $surname
172 public function setSurname(string $surname): User
{
173 $this->surname
= $surname;
183 public function getSurname(): string {
184 return $this->surname
;
190 * @param string $password
194 public function setPassword(string $password): User
{
195 $this->password
= $password;
207 public function getPassword(): ?string {
208 return $this->password
;
214 * @param bool $active
218 public function setActive(bool $active): User
{
219 $this->active
= $active;
229 public function getActive(): bool {
230 return $this->active
;
236 * @param bool $disabled
240 public function setDisabled(bool $disabled): User
{
241 $this->disabled
= $disabled;
251 public function getDisabled(): bool {
252 return $this->disabled
;
258 * @param \DateTime $created
262 public function setCreated(\DateTime
$created): User
{
263 $this->created
= $created;
273 public function getCreated(): \DateTime
{
274 return $this->created
;
280 * @param \DateTime $updated
284 public function setUpdated(\DateTime
$updated): User
{
285 $this->updated
= $updated;
295 public function getUpdated(): \DateTime
{
296 return $this->updated
;
302 public function setCivility(Civility
$civility): User
{
303 $this->civility
= $civility;
311 public function getCivility(): ?Civility
{
312 return $this->civility
;
318 * @param Group $group
322 public function addGroup(Group
$group) {
323 $this->groups
[] = $group;
331 * @param Group $group
333 public function removeGroup(Group
$group) {
334 $this->groups
->removeElement($group);
340 * @return ArrayCollection
342 public function getGroups(): ArrayCollection
{
343 return $this->groups
;
349 public function getRoles(): array {
350 //Get the unique roles list by id
351 return array_unique(array_reduce(
352 //Cast groups as array
353 $this->groups
->toArray(),
354 //Reduce to an array of id => group tuples
355 function ($array, $group) {
356 $array[$group->getId()] = $group->getRole();
359 //Init with empty array
360 //XXX: on registration, add each group present in rapsys_user.default.group array to user
361 //XXX: see vendor/rapsys/userbundle/Controller/DefaultController.php +450
369 public function getRole(): ?string {
371 $roles = $this->getRoles();
373 //With roles array empty
379 //Return the role with max id
380 //XXX: should be rewriten if it change in your configuration
381 return $roles[array_reduce(
383 function($cur, $id) {
384 if ($cur === null || $id > $cur) {
396 public function getSalt(): ?string {
397 //No salt required with bcrypt
404 public function getUsername(): string {
411 public function getUserIdentifier(): string {
418 public function eraseCredentials(): void {}
423 public function serialize(): string {
440 public function unserialize($serialized) {
451 ) = unserialize($serialized);
455 * Check if account is activated
457 * It was from deprecated AdvancedUserInterface, see if it's used anymore
459 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
461 public function isActivated(): bool {
462 return $this->active
;
466 * Check if account is disabled
468 * It was from deprecated AdvancedUserInterface, see if it's used anymore
470 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
472 public function isDisabled(): bool {
473 return $this->disabled
;
479 public function preUpdate(PreUpdateEventArgs
$eventArgs) {
480 //Check that we have an user instance
481 if (($user = $eventArgs->getEntity()) instanceof User
) {
483 $user->setUpdated(new \
DateTime('now'));
488 * Returns a recipient name of the user
492 public function getRecipientName(): string {
493 //Without forename and surname
494 if (empty($this->forename
) && empty($this->surname
)) {
495 //Return recipient name from mail
496 return ucwords(trim(preg_replace('/[^a-zA-Z]+/', ' ', current(explode('@', $this->mail
)))));
499 //Return recipient name from forename and surname
500 return implode(' ', [$this->forename
, $this->surname
]);
504 * Returns a string representation of the user
508 public function __toString(): string {
509 return $this->civility
.' '.$this->forename
.' '.$this->surname
;