User.php 16.9 KB
Newer Older
Julien Jorry committed
1 2 3 4 5
<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
Julien Jorry committed
7 8 9 10 11 12
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
13 14 15
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use FOS\UserBundle\Model\UserInterface;
16
use FOS\UserBundle\Model\GroupInterface;
Julien Jorry committed
17 18

/**
19 20 21 22 23 24 25 26 27 28 29 30
 * ApiResource(
 *     attributes={"security"="is_granted('ROLE_SONATA_USER_GERER_VIEW')"},
 *     collectionOperations={
 *         "get"={"security"="is_granted('ROLE_SONATA_USER_GERER_LIST')"},
 *         "post"={"security"="is_granted('ROLE_SONATA_USER_GERER_EDIT')"}
 *     },
 *     itemOperations={
 *         "get"={"security"="is_granted('ROLE_SONATA_USER_GERER_VIEW')"},
 *         "put"={"security"="is_granted('ROLE_SONATA_USER_GERER_EDIT')"},
 *     },
 *     normalizationContext={"groups"={"read"}},
 *     denormalizationContext={"groups"={"write"}}
31
 * )
Julien Jorry committed
32
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
Julien Jorry committed
33 34
 * @ORM\Table(name="user")
 * @UniqueEntity(
35
 *     fields={"email"},
36
 *     errorPath="email",
37
 *     message="Cet email est déjà utilisé !"
Julien Jorry committed
38 39 40 41 42 43 44 45
 * )
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
46
     * @Groups({"user:read"})
Julien Jorry committed
47 48 49
     */
    protected $id;

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    /**
     * @Groups({"user"})
     */
    protected $email;

    /**
     * @Groups({"user:write"})
     */
    protected $plainPassword;

    /**
     * @Groups({"user"})
     */
    protected $username;

Julien Jorry committed
65 66 67 68
    /**
     * @var null|string
     *
     * @ORM\Column(name="etat", type="string", length=10, nullable=true)
69
     * @Groups({"user"})
Julien Jorry committed
70 71 72 73 74
     */
    protected $etat;

    /**
     * @ORM\Column(type="string", length=15, nullable=true)
75
     * @Groups({"user"})
Julien Jorry committed
76 77 78
     */
    protected $mobile;

79 80 81 82 83 84 85 86 87 88 89 90 91 92
    /**
     * @var ArrayCollection|Document[]
     *
     * @ORM\OneToMany(targetEntity="Document", mappedBy="user", cascade={"persist"})
     */
    private $documents;

    /**
     * @var ArrayCollection|Faq[]
     *
     * @ORM\OneToMany(targetEntity="Faq", mappedBy="user", cascade={"persist"})
     */
    private $faqs;

93 94 95 96 97 98 99
    /**
     * @var ArrayCollection|Import[]
     *
     * @ORM\OneToMany(targetEntity="Import", mappedBy="user", cascade={"persist"})
     */
    private $imports;

100
    /**
101
     * @ORM\OneToMany(targetEntity="Flux", mappedBy="operateur", cascade={"persist"})
102
     * @ORM\OrderBy({"createdAt" = "DESC"})
103 104 105 106 107 108 109 110 111
     */
    protected $flux;

    /**
     * @ORM\OneToMany(targetEntity="EmailToken", mappedBy="user", cascade={"persist", "remove"})
     * @ORM\OrderBy({"expiredAt" = "DESC"})
     */
    private $emailTokens;

112
    /**
Julien Jorry committed
113 114 115 116 117 118 119 120 121 122 123
     * @ORM\OneToOne(targetEntity="Adherent", inversedBy="user", cascade={"all"}, fetch="EXTRA_LAZY")
     * @ORM\JoinColumn(name="adherent_id", referencedColumnName="id", nullable=true)
     */
    protected $adherent;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Prestataire", mappedBy="users", cascade={"persist"})
     * @ORM\JoinTable(name="user_prestataire",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="prestataire_id", referencedColumnName="id")}
     * )
124
     */
Julien Jorry committed
125
    protected $prestataires;
126 127

    /**
128
     * @ORM\ManyToMany(targetEntity="App\Entity\Groupe", mappedBy="gestionnaires", cascade={"persist"}, fetch="EAGER")
Julien Jorry committed
129 130 131 132
     * @ORM\JoinTable(name="user_groupe",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="groupe_id", referencedColumnName="id")}
     * )
133
     */
Julien Jorry committed
134 135 136
    private $groupesgeres;

    /**
137
     * @ORM\ManyToMany(targetEntity="App\Entity\Comptoir", mappedBy="gestionnaires", cascade={"persist"}, fetch="EAGER")
Julien Jorry committed
138 139 140 141 142 143
     * @ORM\JoinTable(name="user_comptoir",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="comptoir_id", referencedColumnName="id")}
     * )
     */
    private $comptoirsgeres;
144

145 146
    /**
     * @ORM\Column(name="apiKey", type="string", length=255, nullable=true)
147
     * @Groups({"user:read"})
148 149 150
     */
    private $apiKey;

151 152 153 154 155 156
    /**
     * @var ArrayCollection|News[]
     * @ORM\OneToMany(targetEntity="App\Entity\News", mappedBy="user", cascade={"persist"})
     */
    private $news;

157 158 159 160 161 162
    /**
     * @var ArrayCollection|Page[]
     * @ORM\OneToMany(targetEntity="App\Entity\Page", mappedBy="user", cascade={"persist"})
     */
    private $pages;

163
    /**
164
     * @ORM\ManyToMany(targetEntity="App\Entity\Usergroup", cascade={"persist"}, fetch="EXTRA_LAZY")
165 166 167 168
     * @ORM\JoinTable(name="user_usergroup",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
169
     * @Groups({"user"})
170 171
     */
    protected $groups;
172 173 174 175 176 177 178 179 180 181
    
    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Usergroup", fetch="EAGER")
     * @ORM\JoinTable(name="user_possiblegroup",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     * @Groups({"user"})
     */
    protected $possiblegroups;
182

183 184 185 186
    /**
     * Alerte email à chaque transaction concernant l'utilisateur
     * @var bool
     *
Julien Jorry committed
187
     * @ORM\Column(name="alertemailflux", type="boolean", options={"default" : true})
188
     * @Groups({"user"})
189
     */
Julien Jorry committed
190
    protected $alertemailflux = true;
191

Julien Jorry committed
192 193 194
    public function __construct()
    {
        parent::__construct();
Julien Jorry committed
195 196 197
        $this->imports = new ArrayCollection();
        $this->faqs = new ArrayCollection();
        $this->documents = new ArrayCollection();
198 199
        $this->flux = new ArrayCollection();
        $this->emailTokens = new ArrayCollection();
Julien Jorry committed
200 201 202
        $this->prestataires = new ArrayCollection();
        $this->groupesgeres = new ArrayCollection();
        $this->comptoirsgeres = new ArrayCollection();
203 204
        $this->faqs = new ArrayCollection();
        $this->news = new ArrayCollection();
Julien Jorry committed
205
        $this->pages = new ArrayCollection();
206
        $this->possiblegroups = new ArrayCollection();
207
        $this->alertemailflux = true;
208
        $this->createApiKey();
Julien Jorry committed
209
        $this->createEmailToken();
Julien Jorry committed
210 211
    }

212 213 214 215 216 217 218 219 220
    /**
    * Get apiKey
    * @return string
    */
    public function getApiKey()
    {
        return $this->apiKey;
    }

221 222 223 224 225
    public function isUser(?UserInterface $user = null): bool
    {
        return $user instanceof self && $user->id === $this->id;
    }

226 227 228 229 230 231 232
    public function createApiKey()
    {
        $bytes = random_bytes(64);
        $this->apiKey = rtrim(strtr(base64_encode($bytes), '+/', '-_'), '=');
    }


Julien Jorry committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    public function getCommonName(): ?string
    {
        return $this->getFirstname()." ".$this->getLastname();
    }

    /**
     * @return null|string
     */
    public function getEtat(): ?string
    {
        return $this->etat;
    }

    /**
     * @param null|string $etat
     * @return Prestataire
     */
Julien Jorry committed
250
    public function setEtat(?string $etat): self
Julien Jorry committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    {
        $this->etat = $etat;
        return $this;
    }

    /**
     * Get mobile number
     * @return [type] [description]
     */
    public function getMobile(): ?string
    {
        return $this->mobile;
    }

    /**
     * Set mobile number
     * @param string $mobile [description]
     */
    public function setMobile(?string $mobile): self
    {
        $this->mobile = $mobile;

        return $this;
    }

    public function isGranted($role)
    {
        return in_array($role, $this->getRoles());
    }

    public function isAdmin()
    {
        if ($this->isSuperAdmin()) {
            return true;
        }
        $isAdmin = false;
        foreach ($this->getRoles() as $role) {
            if ((is_object($role) && $role->getRole() == 'ROLE_ADMIN') || $role == 'ROLE_ADMIN') {
                $isAdmin = true;
                break;
            }
        }
        return $isAdmin;
    }

    public function isSuperAdmin()
    {
        return $this->getSuperAdmin();
    }

    public function getSuperAdmin()
    {
        $isSuperAdmin = false;
        foreach ($this->getRoles() as $role) {
            if ((is_object($role) && $role->getRole() == 'ROLE_SUPER_ADMIN') || $role == 'ROLE_SUPER_ADMIN') {
                $isSuperAdmin = true;
                break;
            }
        }
        return $isSuperAdmin;
    }
Julien Jorry committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

    /**
    * Get adherent
    * @return
    */
    public function getAdherent()
    {
        return $this->adherent;
    }

    /**
    * Set adherent
    * @return $this
    */
    public function setAdherent($adherent): self
    {
        $this->adherent = $adherent;
        return $this;
    }

    /**
Julien Jorry committed
333
    * Get prestataires
Julien Jorry committed
334 335
    * @return
    */
Julien Jorry committed
336
    public function getPrestataires()
Julien Jorry committed
337
    {
Julien Jorry committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        return $this->prestataires;
    }

    /**
     * @param Prestataire $prestataire
     * @return $this
     */
    public function addPrestataire(Prestataire $prestataire): self
    {
        if (!$this->prestataires->contains($prestataire)) {
            $this->prestataires[] = $prestataire;
        }
        return $this;
    }

    /**
     * @param Prestataire $prestataire
     * @return $this
     */
357
    public function removePrestataire(Prestataire $prestataire): self
Julien Jorry committed
358 359 360 361 362
    {
        if ($this->prestataires->contains($prestataire)) {
            $this->prestataires->removeElement($prestataire);
        }
        return $this;
Julien Jorry committed
363 364 365 366 367 368
    }

    /**
    * Set prestataire
    * @return $this
    */
Julien Jorry committed
369
    public function setPrestataires($prestataires): self
Julien Jorry committed
370
    {
Julien Jorry committed
371
        $this->prestataires = $prestataires;
Julien Jorry committed
372 373 374
        return $this;
    }

375 376 377 378 379 380 381 382 383 384 385 386
    /**
     * @return Flux[]|ArrayCollection
     */
    public function getFlux()
    {
        return $this->flux;
    }

    /**
     * @param Flux $flux
     * @return $this
     */
Julien Jorry committed
387
    public function addFlux(Flux $flux): self
388 389 390 391 392 393 394 395 396 397 398 399
    {
        if (!$this->flux->contains($flux)) {
            $this->flux[] = $flux;
            $flux->setUser($this);
        }
        return $this;
    }

    /**
     * @param Flux $flux
     * @return $this
     */
Julien Jorry committed
400
    public function removeFlux(Flux $flux): self
401
    {
402
        throw new \LogicException('User::removeFlux : Ce code ne devrait jamais être atteint !');
403 404 405 406 407 408
        // if ($this->flux->contains($flux)) {
        //     $this->flux->removeElement($flux);
        // }
        return $this;
    }

409
    /**
Julien Jorry committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
     * @return Groupesgere[]|ArrayCollection
     */
    public function getGroupesgeres()
    {
        return $this->groupesgeres;
    }

    /**
     * @param Groupesgere[]|ArrayCollection
     * @return this
     */
    public function setGroupesgeres($groupesgeres): self
    {
        $this->groupesgeres = $groupesgeres;
        return $this;
    }

    /**
     * @param Groupesgere $groupesgere
     * @return $this
     */
    public function addGroupesgere(Groupe $groupesgere): self
    {
        if (!$this->groupesgeres->contains($groupesgere)) {
            $this->groupesgeres[] = $groupesgere;
            $groupesgere->addGestionnaire($this);
        }
        return $this;
    }

    /**
     * @param Groupesgere $groupesgere
442 443
     * @return $this
     */
444
    public function removeGroupesgere(Groupe $groupesgere): self
445
    {
Julien Jorry committed
446 447 448 449
        if ($this->groupesgeres->contains($groupesgere)) {
            $this->groupesgeres->removeElement($groupesgere);
            $groupesgere->removeGestionnaire($this);
        }
450 451 452
        return $this;
    }

Julien Jorry committed
453

454
    /**
Julien Jorry committed
455
     * @return Comptoirsgere[]|ArrayCollection
456
     */
Julien Jorry committed
457
    public function getComptoirsgeres()
458
    {
Julien Jorry committed
459
        return $this->comptoirsgeres;
460 461 462
    }

    /**
Julien Jorry committed
463 464 465 466 467 468 469 470 471 472 473
     * @param Comptoirsgere[]|ArrayCollection
     * @return this
     */
    public function setComptoirsgeres($comptoirsgeres): self
    {
        $this->comptoirsgeres = $comptoirsgeres;
        return $this;
    }

    /**
     * @param Comptoirsgere $comptoirsgere
474 475
     * @return $this
     */
Julien Jorry committed
476
    public function addComptoirsgere(Comptoir $comptoirsgere): self
477
    {
Julien Jorry committed
478 479 480 481
        if (!$this->comptoirsgeres->contains($comptoirsgere)) {
            $this->comptoirsgeres[] = $comptoirsgere;
            $comptoirsgere->addGestionnaire($this);
        }
482 483 484 485
        return $this;
    }

    /**
Julien Jorry committed
486 487
     * @param Comptoirsgere $comptoirsgere
     * @return $this
488
     */
489
    public function removeComptoirsgere(Comptoir $comptoirsgere): self
490
    {
Julien Jorry committed
491 492 493 494 495
        if ($this->comptoirsgeres->contains($comptoirsgere)) {
            $this->comptoirsgeres->removeElement($comptoirsgere);
            $comptoirsgere->removeGestionnaire($this);
        }
        return $this;
496 497
    }

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    public function createEmailToken()
    {
        $token = new EmailToken();
        $token->setUser($this);
        $this->emailTokens->add($token);

        return $token;
    }

    public function getFirstValidEmailToken()
    {
        foreach ($this->getEmailTokens() as $emailToken) {
            if ($emailToken->isValid()) {
                return $emailToken;
            }
        }

        return null;
    }

    public function getEmailTokens()
    {
        return $this->emailTokens;
    }

    public function getEmailToken($token)
    {
        foreach ($this->emailTokens as $emailToken) {
            if ($emailToken->getToken()==$token) {
                return $emailToken;
            }
        }
    }
531

532 533 534 535 536 537 538 539
    public function __toString()
    {
        if (empty($this->getFullname())) {
            return $this->getEmail();
        }
        return $this->getFullname();
    }

540 541 542 543 544 545 546
    public function getName()
    {
        if (empty($this->getFullname())) {
            return $this->__toString();
        }
        return $this->getFullname();
    }
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

    /**
     * @return Collection|News[]
     */
    public function getNews(): Collection
    {
        return $this->news;
    }

    public function addNews(News $news): self
    {
        if (!$this->news->contains($news)) {
            $this->news[] = $news;
            $news->setUser($this);
        }

        return $this;
    }

    public function removeNews(News $news): self
    {
        if ($this->news->contains($news)) {
            $this->news->removeElement($news);
            // set the owning side to null (unless already changed)
            if ($news->getUser() === $this) {
                $news->setUser(null);
            }
        }

        return $this;
    }
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

    /**
     * @return Collection|Page[]
     */
    public function getPage(): Collection
    {
        return $this->page;
    }

    public function addPage(Page $page): self
    {
        if (!$this->page->contains($page)) {
            $this->page[] = $page;
            $page->setUser($this);
        }

        return $this;
    }

    public function removePage(Page $page): self
    {
        if ($this->page->contains($page)) {
            $this->page->removeElement($page);
            // set the owning side to null (unless already changed)
            if ($page->getUser() === $this) {
                $page->setUser(null);
            }
        }

        return $this;
    }
609

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    /**
    * Get possiblegroups
    * @return
    */
    public function getPossiblegroups()
    {
        return $this->possiblegroups;
    }
    
    /**
    * Set possiblegroups
    * @return $this
    */
    public function setPossiblegroups($possiblegroups)
    {
        $this->possiblegroups = $possiblegroups;
        return $this;
    }
    
    /**
     * addPossibleGroup
     * @param GroupInterface $possiblegroups [description]
     */
    public function addPossibleGroup(GroupInterface $possiblegroups)
    {
        if (!$this->getPossiblegroups()->contains($possiblegroups)) {
            $this->getPossiblegroups()->add($possiblegroups);
        }

        return $this;
    }

    /**
     * removePossibleGroup
     * @param  GroupInterface $possiblegroups [description]
     * @return [type]                [description]
     */
    public function removePossibleGroup(GroupInterface $possiblegroups)
    {
        if ($this->getPossiblegroups()->contains($possiblegroups)) {
            $this->getPossiblegroups()->removeElement($possiblegroups);
        }

        return $this;
    }

656 657 658 659 660 661 662 663 664 665 666 667 668
    /**
    * Get alertemailflux
    * @return
    */
    public function getAlertemailflux()
    {
        return $this->alertemailflux;
    }
    
    /**
    * Set alertemailflux
    * @return $this
    */
Julien Jorry committed
669
    public function setAlertemailflux($alertemailflux): self
670 671 672 673
    {
        $this->alertemailflux = $alertemailflux;
        return $this;
    }
674 675 676 677 678 679 680 681 682 683 684 685 686 687

    /**
     * Quand on appelle setGroups sur le user, on réinitialise ses groupes avant ! Pour pouvoir se connecter sur un seul groupe, les groupes possibles sont dans possiblegroups !
     * {@inheritdoc}
     */
    public function setGroups($groups)
    {
        $this->groups = new ArrayCollection();
        foreach ($groups as $group) {
            $this->addGroup($group);
        }

        return $this;
    }
Julien Jorry committed
688
}