3 // src/Rapsys/UserBundle/Entity/User.php
4 namespace Rapsys\UserBundle\Entity
;
6 use Rapsys\UserBundle\Entity\Group
;
7 use Symfony\Component\Security\Core\User\UserInterface
;
8 use Doctrine\Common\Collections\ArrayCollection
;
9 use Rapsys\UserBundle\Entity\Civility
;
11 class User
implements UserInterface
, \Serializable
{
63 * @var \Rapsys\UserBundle\Entity\Civility
68 * @var \Doctrine\Common\Collections\ArrayCollection
75 public function __construct() {
76 $this->active
= false;
77 $this->disabled
= false;
78 $this->groups
= new ArrayCollection();
86 public function getId(): int {
97 public function setMail(string $mail) {
108 public function getMail(): ?string {
115 * @param string $pseudonym
119 public function setPseudonym(string $pseudonym) {
120 $this->pseudonym
= $pseudonym;
130 public function getPseudonym(): ?string {
131 return $this->pseudonym
;
137 * @param string $forename
141 public function setForename(string $forename) {
142 $this->forename
= $forename;
152 public function getForename(): ?string {
153 return $this->forename
;
159 * @param string $surname
163 public function setSurname(string $surname) {
164 $this->surname
= $surname;
174 public function getSurname(): ?string {
175 return $this->surname
;
181 * @param string $password
185 public function setPassword(string $password) {
186 $this->password
= $password;
198 public function getPassword(): ?string {
199 return $this->password
;
205 * @param bool $active
209 public function setActive(bool $active) {
210 $this->active
= $active;
220 public function getActive(): bool {
221 return $this->active
;
227 * @param bool $disabled
231 public function setDisabled(bool $disabled) {
232 $this->disabled
= $disabled;
242 public function getDisabled(): bool {
243 return $this->disabled
;
249 * @param \DateTime $created
253 public function setCreated(\DateTime
$created) {
254 $this->created
= $created;
264 public function getCreated(): \DateTime
{
265 return $this->created
;
271 * @param \DateTime $updated
275 public function setUpdated(\DateTime
$updated) {
276 $this->updated
= $updated;
286 public function getUpdated(): \DateTime
{
287 return $this->updated
;
293 public function setCivility(Civility
$civility) {
294 $this->civility
= $civility;
302 public function getCivility(): ?Civility
{
303 return $this->civility
;
309 * @param \Rapsys\UserBundle\Entity\Group $group
313 public function addGroup(Group
$group) {
314 $this->groups
[] = $group;
322 * @param \Rapsys\UserBundle\Entity\Group $group
324 public function removeGroup(Group
$group) {
325 $this->groups
->removeElement($group);
331 * @return \Doctrine\Common\Collections\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 {}
404 public function serialize(): string {
416 public function unserialize($serialized) {
425 ) = unserialize($serialized);
429 * Check if account is activated
431 * It was from deprecated AdvancedUserInterface, see if it's used anymore
433 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
435 public function isActivated(): bool {
436 return $this->active
;
440 * Check if account is disabled
442 * It was from deprecated AdvancedUserInterface, see if it's used anymore
444 * @see vendor/symfony/security-core/User/AdvancedUserInterface.php
446 public function isDisabled(): bool {
447 return $this->disabled
;
453 public function preUpdate(\Doctrine\ORM\Event\PreUpdateEventArgs
$eventArgs) {
454 //Check that we have an user instance
455 if (($user = $eventArgs->getEntity()) instanceof User
) {
457 $user->setUpdated(new \
DateTime('now'));
462 * Returns a string representation of the user
466 public function __toString(): string {
467 return $this->civility
.' '.$this->forename
.' '.$this->surname
;