Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

src/Chamilo/PageBundle/Entity/User.php (2 issues)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\PageBundle\Entity;
5
6
use Chamilo\CoreBundle\Entity\ExtraFieldValues;
7
use Chamilo\CoreBundle\Entity\UsergroupRelUser;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\ORM\Event\LifecycleEventArgs;
10
use Doctrine\ORM\Mapping as ORM;
11
//use Sonata\UserBundle\Entity\BaseUser as BaseUser;
12
use FOS\UserBundle\Model\GroupInterface;
13
use FOS\UserBundle\Model\UserInterface;
14
use Sonata\UserBundle\Model\User as BaseUser;
15
//use Symfony\Component\Security\Core\User\UserInterface;
16
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
17
use Symfony\Component\HttpFoundation\File\File;
18
use Symfony\Component\Validator\Constraints as Assert;
19
use Symfony\Component\Validator\Mapping\ClassMetadata;
20
21
class User extends BaseUser
22
{
23
    const COURSE_MANAGER = 1;
24
    const TEACHER = 1;
25
    const SESSION_ADMIN = 3;
26
    const DRH = 4;
27
    const STUDENT = 5;
28
    const ANONYMOUS = 6;
29
30
    /**
31
     * @var int
32
     *
33
     * @ORM\Column(name="id", type="integer")
34
     * @ORM\Id
35
     * @ORM\GeneratedValue(strategy="AUTO")
36
     */
37
    protected $id;
38
39
    /**
40
     * @var int
41
     *
42
     * @ORM\Column(name="user_id", type="integer", nullable=true)
43
     */
44
    protected $userId;
45
46
    /**
47
     * @var string
48
     *
49
     * @ORM\Column(name="username", type="string", length=100, nullable=false, unique=true)
50
     */
51
    //protected $username;
52
53
    /**
54
     * @var string
55
     *
56
     * * @ORM\Column(name="username_canonical", type="string", length=100, nullable=false, unique=true)
57
     */
58
    //protected $usernameCanonical;
59
60
    /**
61
     * @var string
62
     *
63
     * @ORM\Column(name="email", type="string", length=100, nullable=false, unique=false)
64
     */
65
    //protected $email;
66
67
    /**
68
     * @var bool
69
     * @ORM\Column(name="locked", type="boolean")
70
     */
71
    //protected $locked;
72
73
    /**
74
     * @var bool
75
     * @ORM\Column(name="enabled", type="boolean")
76
     */
77
    //protected $enabled;
78
79
    /**
80
     * @var bool
81
     * @ORM\Column(name="expired", type="boolean")
82
     */
83
    //protected $expired;
84
85
    /**
86
     * @var bool
87
     * @ORM\Column(name="credentials_expired", type="boolean")
88
     */
89
    //protected $credentialsExpired;
90
91
    /**
92
     * @var \DateTime
93
     * @ORM\Column(name="credentials_expire_at", type="datetime", nullable=true, unique=false)
94
     */
95
    //protected $credentialsExpireAt;
96
97
    /**
98
     * @var \DateTime
99
     * @ORM\Column(name="expires_at", type="datetime", nullable=true, unique=false)
100
     */
101
    //protected $expiresAt;
102
103
    /**
104
     * @var string
105
     *
106
     * @ORM\Column(name="lastname", type="string", length=60, nullable=true, unique=false)
107
     */
108
    protected $lastname;
109
110
    /**
111
     * @var string
112
     *
113
     * @ORM\Column(name="firstname", type="string", length=60, nullable=true, unique=false)
114
     */
115
    protected $firstname;
116
117
    /**
118
     * @var string
119
     *
120
     * @ORM\Column(name="phone", type="string", length=30, nullable=true, unique=false)
121
     */
122
    protected $phone;
123
124
    /**
125
     * Vich\UploadableField(mapping="user_image", fileNameProperty="picture_uri").
126
     *
127
     * note This is not a mapped field of entity metadata, just a simple property.
128
     *
129
     * @var File
130
     */
131
    protected $imageFile;
132
133
    /**
134
     * @ORM\Column(type="string", length=255)
135
     */
136
    //protected $salt;
137
138
    /**
139
     * @var \DateTime
140
     *
141
     * @ORM\Column(name="last_login", type="datetime", nullable=true, unique=false)
142
     */
143
    //protected $lastLogin;
144
145
    /**
146
     * Random string sent to the user email address in order to verify it.
147
     *
148
     * @var string
149
     * @ORM\Column(name="confirmation_token", type="string", length=255, nullable=true)
150
     */
151
    //protected $confirmationToken;
152
153
    /**
154
     * @var \DateTime
155
     *
156
     * @ORM\Column(name="password_requested_at", type="datetime", nullable=true, unique=false)
157
     */
158
    //protected $passwordRequestedAt;
159
160
    /**
161
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\CourseRelUser", mappedBy="user")
162
     */
163
    protected $courses;
164
165
    /**
166
     * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CItemProperty", mappedBy="user")
167
     */
168
    //protected $items;
169
170
    /**
171
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\UsergroupRelUser", mappedBy="user")
172
     */
173
    protected $classes;
174
175
    /**
176
     * ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CDropboxPost", mappedBy="user").
177
     */
178
    protected $dropBoxReceivedFiles;
179
180
    /**
181
     * ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CDropboxFile", mappedBy="userSent").
182
     */
183
    protected $dropBoxSentFiles;
184
185
    /**
186
     * @ORM\Column(type="array")
187
     */
188
    //protected $roles;
189
190
    /**
191
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\JuryMembers", mappedBy="user")
192
     */
193
    //protected $jurySubscriptions;
194
195
    /**
196
     * @ORM\ManyToMany(targetEntity="Chamilo\UserBundle\Entity\Group")
197
     * @ORM\JoinTable(name="fos_user_user_group",
198
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
199
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
200
     * )
201
     */
202
    protected $groups;
203
204
    //private $isActive;
205
206
    /**
207
     * ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\CurriculumItemRelUser", mappedBy="user").
208
     */
209
    protected $curriculumItems;
210
211
    /*
212
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\AccessUrlRelUser", mappedBy="user")
213
     *
214
     */
215
    protected $portals;
216
217
    /**
218
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Session", mappedBy="generalCoach")
219
     */
220
    protected $sessionAsGeneralCoach;
221
222
    /**
223
     * @var ArrayCollection
224
     *                      ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\UserFieldValues", mappedBy="user", orphanRemoval=true, cascade={"persist"})
225
     */
226
    protected $extraFields;
227
228
    /**
229
     * ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", mappedBy="creator").
230
     */
231
    protected $resourceNodes;
232
233
    /**
234
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\SessionRelCourseRelUser", mappedBy="user", cascade={"persist"})
235
     */
236
    protected $sessionCourseSubscriptions;
237
238
    /**
239
     * @var string
240
     *
241
     * @ORM\Column(name="password", type="string", length=255, nullable=false, unique=false)
242
     */
243
    //protected $password;
244
245
    /**
246
     * @var string
247
     *
248
     * @ORM\Column(name="auth_source", type="string", length=50, nullable=true, unique=false)
249
     */
250
    private $authSource;
251
252
    /**
253
     * @var int
254
     *
255
     * @ORM\Column(name="status", type="integer", nullable=false)
256
     */
257
    private $status;
258
259
    /**
260
     * @var string
261
     *
262
     * @ORM\Column(name="official_code", type="string", length=40, nullable=true, unique=false)
263
     */
264
    private $officialCode;
265
266
    /**
267
     * @var string
268
     * @ORM\Column(name="picture_uri", type="string", length=250, nullable=true, unique=false)
269
     */
270
    private $pictureUri;
271
272
    /**
273
     * ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"all"} ).
274
     *
275
     * @ORM\JoinColumn(name="picture_uri", referencedColumnName="id")
276
     */
277
    //protected $pictureUri;
278
279
    /**
280
     * @var int
281
     *
282
     * @ORM\Column(name="creator_id", type="integer", nullable=true, unique=false)
283
     */
284
    private $creatorId;
285
286
    /**
287
     * @var string
288
     *
289
     * @ORM\Column(name="competences", type="text", nullable=true, unique=false)
290
     */
291
    private $competences;
292
293
    /**
294
     * @var string
295
     *
296
     * @ORM\Column(name="diplomas", type="text", nullable=true, unique=false)
297
     */
298
    private $diplomas;
299
300
    /**
301
     * @var string
302
     *
303
     * @ORM\Column(name="openarea", type="text", nullable=true, unique=false)
304
     */
305
    private $openarea;
306
307
    /**
308
     * @var string
309
     *
310
     * @ORM\Column(name="teach", type="text", nullable=true, unique=false)
311
     */
312
    private $teach;
313
314
    /**
315
     * @var string
316
     *
317
     * @ORM\Column(name="productions", type="string", length=250, nullable=true, unique=false)
318
     */
319
    private $productions;
320
321
    /**
322
     * @var string
323
     *
324
     * @ORM\Column(name="language", type="string", length=40, nullable=true, unique=false)
325
     */
326
    private $language;
327
328
    /**
329
     * @var \DateTime
330
     *
331
     * @ORM\Column(name="registration_date", type="datetime", nullable=false, unique=false)
332
     */
333
    private $registrationDate;
334
335
    /**
336
     * @var \DateTime
337
     *
338
     * @ORM\Column(name="expiration_date", type="datetime", nullable=true, unique=false)
339
     */
340
    private $expirationDate;
341
342
    /**
343
     * @var int
344
     *
345
     * @ORM\Column(name="active", type="boolean", nullable=false, unique=false)
346
     */
347
    private $active;
348
349
    /**
350
     * @var string
351
     *
352
     * @ORM\Column(name="openid", type="string", length=255, nullable=true, unique=false)
353
     */
354
    private $openid;
355
356
    /**
357
     * @var string
358
     *
359
     * @ORM\Column(name="theme", type="string", length=255, nullable=true, unique=false)
360
     */
361
    private $theme;
362
363
    /**
364
     * @var int
365
     *
366
     * @ORM\Column(name="hr_dept_id", type="smallint", nullable=true, unique=false)
367
     */
368
    private $hrDeptId;
369
370
    /**
371
     * Constructor.
372
     */
373
    public function __construct()
374
    {
375
        $this->status = self::STUDENT;
376
377
        $this->salt = sha1(uniqid(null, true));
378
        $this->isActive = true;
379
        $this->active = 1;
380
        $this->registrationDate = new \DateTime();
381
        $this->authSource = 'platform';
382
        $this->courses = new ArrayCollection();
383
        $this->items = new ArrayCollection();
384
        $this->classes = new ArrayCollection();
385
        $this->curriculumItems = new ArrayCollection();
386
        $this->portals = new ArrayCollection();
387
        $this->dropBoxSentFiles = new ArrayCollection();
388
        $this->dropBoxReceivedFiles = new ArrayCollection();
389
        //$this->extraFields = new ArrayCollection();
390
        //$this->userId = 0;
391
        //$this->createdAt = new \DateTime();
392
        //$this->updatedAt = new \DateTime();
393
394
        $this->enabled = false;
395
        $this->locked = false;
396
        $this->expired = false;
397
        $this->roles = [];
398
        $this->credentialsExpired = false;
399
    }
400
401
    /**
402
     * @return string
403
     */
404
    public function __toString()
405
    {
406
        return $this->getUsername();
407
    }
408
409
    /**
410
     * Updates the id with the user_id.
411
     *
412
     *  @ORM\PostPersist()
413
     */
414
    public function postPersist(LifecycleEventArgs $args)
415
    {
416
        //parent::postPersist();
417
        // Updates the user_id field
418
        $user = $args->getEntity();
419
        $this->setUserId($user->getId());
420
        /*$em = $args->getEntityManager();
421
        $em->persist($user);
422
        $em->flush();*/
423
    }
424
425
    /**
426
     * @param int $userId
427
     */
428
    public function setId($userId)
429
    {
430
        $this->id = $userId;
431
    }
432
433
    /**
434
     * @param int $userId
435
     */
436
    public function setUserId($userId)
437
    {
438
        if (!empty($userId)) {
439
            $this->userId = $userId;
440
        }
441
    }
442
443
    /**
444
     * @return int
445
     */
446
    public function getId()
447
    {
448
        return $this->id;
449
    }
450
451
    /**
452
     * @return string
453
     */
454
    public function getEncoderName()
455
    {
456
        return "legacy_encoder";
457
    }
458
459
    /**
460
     * @return ArrayCollection
461
     */
462
    public function getDropBoxSentFiles()
463
    {
464
        return $this->dropBoxSentFiles;
465
    }
466
467
    /**
468
     * @return ArrayCollection
469
     */
470
    public function getDropBoxReceivedFiles()
471
    {
472
        return $this->dropBoxReceivedFiles;
473
    }
474
475
    /**
476
     * @return ArrayCollection
477
     */
478
    public function getCourses()
479
    {
480
        return $this->courses;
481
    }
482
483
    /**
484
     * @return array
485
     */
486
    public static function getPasswordConstraints()
487
    {
488
        return
489
            [
490
                new Assert\Length(['min' => 5]),
491
                // Alpha numeric + "_" or "-"
492
                new Assert\Regex(
493
                    [
494
                        'pattern' => '/^[a-z\-_0-9]+$/i',
495
                        'htmlPattern' => '/^[a-z\-_0-9]+$/i', ]
496
                ),
497
                // Min 3 letters - not needed
498
                /*new Assert\Regex(array(
499
                    'pattern' => '/[a-z]{3}/i',
500
                    'htmlPattern' => '/[a-z]{3}/i')
501
                ),*/
502
                // Min 2 numbers
503
                new Assert\Regex(
504
                    [
505
                        'pattern' => '/[0-9]{2}/',
506
                        'htmlPattern' => '/[0-9]{2}/', ]
507
                ),
508
            ]
509
            ;
510
    }
511
512
    public static function loadValidatorMetadata(ClassMetadata $metadata)
513
    {
514
        //$metadata->addPropertyConstraint('firstname', new Assert\NotBlank());
515
        //$metadata->addPropertyConstraint('lastname', new Assert\NotBlank());
516
        //$metadata->addPropertyConstraint('email', new Assert\Email());
517
        /*
518
        $metadata->addPropertyConstraint('password',
519
            new Assert\Collection(self::getPasswordConstraints())
520
        );*/
521
522
        /*$metadata->addConstraint(new UniqueEntity(array(
523
            'fields'  => 'username',
524
            'message' => 'This value is already used.',
525
        )));*/
526
527
        /*$metadata->addPropertyConstraint(
528
            'username',
529
            new Assert\Length(array(
530
                'min'        => 2,
531
                'max'        => 50,
532
                'minMessage' => 'This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more.',
533
                'maxMessage' => 'This value is too long. It should have {{ limit }} character or less.|This value is too long. It should have {{ limit }} characters or less.',
534
            ))
535
        );*/
536
    }
537
538
    /**
539
     * {@inheritdoc}
540
     */
541
    public function isEqualTo(UserInterface $user)
542
    {
543
        if (!$user instanceof User) {
544
            return false;
545
        }
546
547
        /*if ($this->password !== $user->getPassword()) {
548
            return false;
549
        }*/
550
551
        /*if ($this->getSalt() !== $user->getSalt()) {
552
            return false;
553
        }*/
554
555
        /*if ($this->username !== $user->getUsername()) {
556
            return false;
557
        }*/
558
559
        return true;
560
    }
561
562
    /**
563
     * @return ArrayCollection
564
     */
565
    public function getPortals()
566
    {
567
        return $this->portals;
568
    }
569
570
    /**
571
     * @param $portal
572
     */
573
    public function setPortal($portal)
574
    {
575
        $this->portals->add($portal);
576
    }
577
578
    /**
579
     * @return ArrayCollection
580
     */
581
    public function getCurriculumItems()
582
    {
583
        return $this->curriculumItems;
584
    }
585
586
    /**
587
     * @param $items
588
     */
589
    public function setCurriculumItems($items)
590
    {
591
        $this->curriculumItems = $items;
592
    }
593
594
    /**
595
     * @return bool
596
     */
597
    public function getIsActive()
598
    {
599
        return $this->active == 1;
600
    }
601
602
    /**
603
     * @return bool
604
     */
605
    public function isActive()
606
    {
607
        return $this->getIsActive();
608
    }
609
610
    /**
611
     * {@inheritdoc}
612
     */
613
    public function isEnabled()
614
    {
615
        return $this->enabled;
616
    }
617
618
    /**
619
     * @return ArrayCollection
620
     */
621
    /*public function getRolesObj()
622
    {
623
        return $this->roles;
624
    }*/
625
626
    /**
627
     * Set salt.
628
     *
629
     * @param string $salt
630
     *
631
     * @return User
632
     */
633
    public function setSalt($salt)
634
    {
635
        $this->salt = $salt;
636
637
        return $this;
638
    }
639
640
    /**
641
     * Get salt.
642
     *
643
     * @return string
644
     */
645
    public function getSalt()
646
    {
647
        return $this->salt;
648
    }
649
650
    /**
651
     * @return ArrayCollection
652
     */
653
    public function getClasses()
654
    {
655
        return $this->classes;
656
    }
657
658
    public function getLps()
659
    {
660
        //return $this->lps;
661
        /*$criteria = Criteria::create()
662
            ->where(Criteria::expr()->eq("id", "666"))
663
            //->orderBy(array("username" => "ASC"))
664
            //->setFirstResult(0)
665
            //->setMaxResults(20)
666
        ;
667
        $lps = $this->lps->matching($criteria);*/
668
        /*return $this->lps->filter(
669
            function($entry) use ($idsToFilter) {
670
                return $entry->getId() == 1;
671
        });*/
672
    }
673
674
    /**
675
     * @todo don't use api_get_person_name
676
     *
677
     * @return string
678
     */
679
    public function getCompleteName()
680
    {
681
        return api_get_person_name($this->firstname, $this->lastname);
682
    }
683
684
    /**
685
     * Returns the list of classes for the user.
686
     *
687
     * @return string
688
     */
689
    public function getCompleteNameWithClasses()
690
    {
691
        $classSubscription = $this->getClasses();
692
        $classList = [];
693
        /** @var UsergroupRelUser $subscription */
694
        foreach ($classSubscription as $subscription) {
695
            $class = $subscription->getUsergroup();
696
            $classList[] = $class->getName();
697
        }
698
        $classString = !empty($classList) ? ' ['.implode(', ', $classList).']' : null;
699
700
        return $this->getCompleteName().$classString;
701
    }
702
703
    /**
704
     * Get userId.
705
     *
706
     * @return int
707
     */
708
    public function getUserId()
709
    {
710
        return $this->userId;
711
    }
712
713
    /**
714
     * Set lastname.
715
     *
716
     * @param string $lastname
717
     *
718
     * @return User
719
     */
720
    public function setLastname($lastname)
721
    {
722
        $this->lastname = $lastname;
723
724
        return $this;
725
    }
726
727
    /**
728
     * Set firstname.
729
     *
730
     * @param string $firstname
731
     *
732
     * @return User
733
     */
734
    public function setFirstname($firstname)
735
    {
736
        $this->firstname = $firstname;
737
738
        return $this;
739
    }
740
741
    /**
742
     * Set password.
743
     *
744
     * @param string $password
745
     *
746
     * @return User
747
     */
748
    public function setPassword($password)
749
    {
750
        $this->password = $password;
751
752
        return $this;
753
    }
754
755
    /**
756
     * Get password.
757
     *
758
     * @return string
759
     */
760
    public function getPassword()
761
    {
762
        return $this->password;
763
    }
764
765
    /**
766
     * Set authSource.
767
     *
768
     * @param string $authSource
769
     *
770
     * @return User
771
     */
772
    public function setAuthSource($authSource)
773
    {
774
        $this->authSource = $authSource;
775
776
        return $this;
777
    }
778
779
    /**
780
     * Get authSource.
781
     *
782
     * @return string
783
     */
784
    public function getAuthSource()
785
    {
786
        return $this->authSource;
787
    }
788
789
    /**
790
     * Set email.
791
     *
792
     * @param string $email
793
     *
794
     * @return User
795
     */
796
    public function setEmail($email)
797
    {
798
        $this->email = $email;
799
800
        return $this;
801
    }
802
803
    /**
804
     * Get email.
805
     *
806
     * @return string
807
     */
808
    public function getEmail()
809
    {
810
        return $this->email;
811
    }
812
813
    /**
814
     * Set status.
815
     *
816
     * @param int $status
817
     *
818
     * @return User
819
     */
820
    public function setStatus($status)
821
    {
822
        $this->status = $status;
823
824
        return $this;
825
    }
826
827
    /**
828
     * Get status.
829
     *
830
     * @return bool
831
     */
832
    public function getStatus()
833
    {
834
        return $this->status;
835
    }
836
837
    /**
838
     * Set officialCode.
839
     *
840
     * @param string $officialCode
841
     *
842
     * @return User
843
     */
844
    public function setOfficialCode($officialCode)
845
    {
846
        $this->officialCode = $officialCode;
847
848
        return $this;
849
    }
850
851
    /**
852
     * Get officialCode.
853
     *
854
     * @return string
855
     */
856
    public function getOfficialCode()
857
    {
858
        return $this->officialCode;
859
    }
860
861
    /**
862
     * Set phone.
863
     *
864
     * @param string $phone
865
     *
866
     * @return User
867
     */
868
    public function setPhone($phone)
869
    {
870
        $this->phone = $phone;
871
872
        return $this;
873
    }
874
875
    /**
876
     * Get phone.
877
     *
878
     * @return string
879
     */
880
    public function getPhone()
881
    {
882
        return $this->phone;
883
    }
884
885
    /**
886
     * Set pictureUri.
887
     *
888
     * @param string $pictureUri
889
     *
890
     * @return User
891
     */
892
    public function setPictureUri($pictureUri)
893
    {
894
        $this->pictureUri = $pictureUri;
895
896
        return $this;
897
    }
898
899
    /**
900
     * Get pictureUri.
901
     *
902
     * @return Media
903
     */
904
    public function getPictureUri()
905
    {
906
        return $this->pictureUri;
907
    }
908
909
    /**
910
     * Set creatorId.
911
     *
912
     * @param int $creatorId
913
     *
914
     * @return User
915
     */
916
    public function setCreatorId($creatorId)
917
    {
918
        $this->creatorId = $creatorId;
919
920
        return $this;
921
    }
922
923
    /**
924
     * Get creatorId.
925
     *
926
     * @return int
927
     */
928
    public function getCreatorId()
929
    {
930
        return $this->creatorId;
931
    }
932
933
    /**
934
     * Set competences.
935
     *
936
     * @param string $competences
937
     *
938
     * @return User
939
     */
940
    public function setCompetences($competences)
941
    {
942
        $this->competences = $competences;
943
944
        return $this;
945
    }
946
947
    /**
948
     * Get competences.
949
     *
950
     * @return string
951
     */
952
    public function getCompetences()
953
    {
954
        return $this->competences;
955
    }
956
957
    /**
958
     * Set diplomas.
959
     *
960
     * @param string $diplomas
961
     *
962
     * @return User
963
     */
964
    public function setDiplomas($diplomas)
965
    {
966
        $this->diplomas = $diplomas;
967
968
        return $this;
969
    }
970
971
    /**
972
     * Get diplomas.
973
     *
974
     * @return string
975
     */
976
    public function getDiplomas()
977
    {
978
        return $this->diplomas;
979
    }
980
981
    /**
982
     * Set openarea.
983
     *
984
     * @param string $openarea
985
     *
986
     * @return User
987
     */
988
    public function setOpenarea($openarea)
989
    {
990
        $this->openarea = $openarea;
991
992
        return $this;
993
    }
994
995
    /**
996
     * Get openarea.
997
     *
998
     * @return string
999
     */
1000
    public function getOpenarea()
1001
    {
1002
        return $this->openarea;
1003
    }
1004
1005
    /**
1006
     * Set teach.
1007
     *
1008
     * @param string $teach
1009
     *
1010
     * @return User
1011
     */
1012
    public function setTeach($teach)
1013
    {
1014
        $this->teach = $teach;
1015
1016
        return $this;
1017
    }
1018
1019
    /**
1020
     * Get teach.
1021
     *
1022
     * @return string
1023
     */
1024
    public function getTeach()
1025
    {
1026
        return $this->teach;
1027
    }
1028
1029
    /**
1030
     * Set productions.
1031
     *
1032
     * @param string $productions
1033
     *
1034
     * @return User
1035
     */
1036
    public function setProductions($productions)
1037
    {
1038
        $this->productions = $productions;
1039
1040
        return $this;
1041
    }
1042
1043
    /**
1044
     * Get productions.
1045
     *
1046
     * @return string
1047
     */
1048
    public function getProductions()
1049
    {
1050
        return $this->productions;
1051
    }
1052
1053
    /**
1054
     * Set language.
1055
     *
1056
     * @param string $language
1057
     *
1058
     * @return User
1059
     */
1060
    public function setLanguage($language)
1061
    {
1062
        $this->language = $language;
1063
1064
        return $this;
1065
    }
1066
1067
    /**
1068
     * Get language.
1069
     *
1070
     * @return string
1071
     */
1072
    public function getLanguage()
1073
    {
1074
        return $this->language;
1075
    }
1076
1077
    /**
1078
     * Set registrationDate.
1079
     *
1080
     * @param \DateTime $registrationDate
1081
     *
1082
     * @return User
1083
     */
1084
    public function setRegistrationDate($registrationDate)
1085
    {
1086
        $this->registrationDate = $registrationDate;
1087
1088
        return $this;
1089
    }
1090
1091
    /**
1092
     * Get registrationDate.
1093
     *
1094
     * @return \DateTime
1095
     */
1096
    public function getRegistrationDate()
1097
    {
1098
        return $this->registrationDate;
1099
    }
1100
1101
    /**
1102
     * Set expirationDate.
1103
     *
1104
     * @param \DateTime $expirationDate
1105
     *
1106
     * @return User
1107
     */
1108
    public function setExpirationDate($expirationDate)
1109
    {
1110
        $this->expirationDate = $expirationDate;
1111
1112
        return $this;
1113
    }
1114
1115
    /**
1116
     * Get expirationDate.
1117
     *
1118
     * @return \DateTime
1119
     */
1120
    public function getExpirationDate()
1121
    {
1122
        return $this->expirationDate;
1123
    }
1124
1125
    /**
1126
     * Set active.
1127
     *
1128
     * @param bool $active
1129
     *
1130
     * @return User
1131
     */
1132
    public function setActive($active)
1133
    {
1134
        $this->active = $active;
0 ignored issues
show
Documentation Bug introduced by
The property $active was declared of type integer, but $active is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1135
1136
        return $this;
1137
    }
1138
1139
    /**
1140
     * Get active.
1141
     *
1142
     * @return bool
1143
     */
1144
    public function getActive()
1145
    {
1146
        return $this->active;
1147
    }
1148
1149
    /**
1150
     * Set openid.
1151
     *
1152
     * @param string $openid
1153
     *
1154
     * @return User
1155
     */
1156
    public function setOpenid($openid)
1157
    {
1158
        $this->openid = $openid;
1159
1160
        return $this;
1161
    }
1162
1163
    /**
1164
     * Get openid.
1165
     *
1166
     * @return string
1167
     */
1168
    public function getOpenid()
1169
    {
1170
        return $this->openid;
1171
    }
1172
1173
    /**
1174
     * Set theme.
1175
     *
1176
     * @param string $theme
1177
     *
1178
     * @return User
1179
     */
1180
    public function setTheme($theme)
1181
    {
1182
        $this->theme = $theme;
1183
1184
        return $this;
1185
    }
1186
1187
    /**
1188
     * Get theme.
1189
     *
1190
     * @return string
1191
     */
1192
    public function getTheme()
1193
    {
1194
        return $this->theme;
1195
    }
1196
1197
    /**
1198
     * Set hrDeptId.
1199
     *
1200
     * @param int $hrDeptId
1201
     *
1202
     * @return User
1203
     */
1204
    public function setHrDeptId($hrDeptId)
1205
    {
1206
        $this->hrDeptId = $hrDeptId;
1207
1208
        return $this;
1209
    }
1210
1211
    /**
1212
     * Get hrDeptId.
1213
     *
1214
     * @return int
1215
     */
1216
    public function getHrDeptId()
1217
    {
1218
        return $this->hrDeptId;
1219
    }
1220
1221
    /**
1222
     * @return Media
1223
     */
1224
    public function getAvatar()
1225
    {
1226
        return $this->getPictureUri();
1227
    }
1228
1229
    /**
1230
     * @return \DateTime
1231
     */
1232
    public function getMemberSince()
1233
    {
1234
        return $this->registrationDate;
1235
    }
1236
1237
    /**
1238
     * @return bool
1239
     */
1240
    public function isOnline()
1241
    {
1242
        return false;
1243
    }
1244
1245
    /**
1246
     * @return int
1247
     */
1248
    public function getIdentifier()
1249
    {
1250
        return $this->getId();
1251
    }
1252
1253
    /**
1254
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
1255
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
1256
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
1257
     * must be able to accept an instance of 'File' as the bundle will inject one here
1258
     * during Doctrine hydration.
1259
     *
1260
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
1261
     */
1262
    public function setImageFile(File $image)
1263
    {
1264
        $this->imageFile = $image;
1265
1266
        if ($image) {
0 ignored issues
show
$image is of type Symfony\Component\HttpFoundation\File\File, thus it always evaluated to true.
Loading history...
1267
            // It is required that at least one field changes if you are using doctrine
1268
            // otherwise the event listeners won't be called and the file is lost
1269
            $this->updatedAt = new \DateTime('now');
1270
        }
1271
    }
1272
1273
    /**
1274
     * @return File
1275
     */
1276
    public function getImageFile()
1277
    {
1278
        return $this->imageFile;
1279
    }
1280
1281
    /**
1282
     * @param string $imageName
1283
     */
1284
    public function setImageName($imageName)
1285
    {
1286
        $this->imageName = $imageName;
1287
    }
1288
1289
    /**
1290
     * @return string
1291
     */
1292
    public function getImageName()
1293
    {
1294
        return $this->imageName;
1295
    }
1296
1297
    /**
1298
     * @return string
1299
     */
1300
    public function getSlug()
1301
    {
1302
        return $this->getUsername();
1303
    }
1304
1305
    /**
1306
     * @param $slug
1307
     *
1308
     * @return User
1309
     */
1310
    public function setSlug($slug)
1311
    {
1312
        return $this->setUsername($slug);
1313
    }
1314
1315
    /**
1316
     * Set lastLogin.
1317
     *
1318
     * @return User
1319
     */
1320
    public function setLastLogin(\DateTime $lastLogin)
1321
    {
1322
        $this->lastLogin = $lastLogin;
1323
1324
        return $this;
1325
    }
1326
1327
    /**
1328
     * Get lastLogin.
1329
     *
1330
     * @return \DateTime
1331
     */
1332
    public function getLastLogin()
1333
    {
1334
        return $this->lastLogin;
1335
    }
1336
1337
    /**
1338
     * {@inheritdoc}
1339
     */
1340
    public function getExtraFields()
1341
    {
1342
        return $this->extraFields;
1343
    }
1344
1345
    /**
1346
     * {@inheritdoc}
1347
     */
1348
    public function setExtraFields($extraFields)
1349
    {
1350
        $this->extraFields = new ArrayCollection();
1351
        foreach ($extraFields as $extraField) {
1352
            $this->addExtraFields($extraField);
1353
        }
1354
1355
        return $this;
1356
    }
1357
1358
    /**
1359
     * {@inheritdoc}
1360
     */
1361
    /*public function addExtraFields(ExtraFieldValues $extraFieldValue)
1362
    {
1363
        $extraFieldValue->setUser($this);
1364
        $this->extraFields[] = $extraFieldValue;
1365
1366
        return $this;
1367
    }*/
1368
1369
    /**
1370
     * {@inheritdoc}
1371
     */
1372
    public function addExtraFields(ExtraFieldValues $extraFieldValue)
1373
    {
1374
        //if (!$this->hasExtraField($attribute)) {
1375
        $extraFieldValue->setUser($this);
1376
        $this->extraFields[] = $extraFieldValue;
1377
        //}
1378
1379
        return $this;
1380
    }
1381
1382
    /**
1383
     * {@inheritdoc}
1384
     */
1385
    public function removeExtraField(ExtraFieldValues $attribute)
1386
    {
1387
        //if ($this->hasExtraField($attribute)) {
1388
        //$this->extraFields->removeElement($attribute);
1389
        //$attribute->setUser($this);
1390
        //}
1391
1392
        return $this;
1393
    }
1394
1395
    /**
1396
     * {@inheritdoc}
1397
     */
1398
    /*public function hasExtraField($attribute)
1399
    {
1400
        if (!$this->extraFields) {
1401
            return false;
1402
        }
1403
        return $this->extraFields->contains($attribute);
1404
    }*/
1405
1406
    /**
1407
     * {@inheritdoc}
1408
     */
1409
    public function hasExtraFieldByName($attributeName)
1410
    {
1411
        foreach ($this->extraFields as $attribute) {
1412
            if ($attribute->getName() === $attributeName) {
1413
                return true;
1414
            }
1415
        }
1416
1417
        return false;
1418
    }
1419
1420
    /**
1421
     * {@inheritdoc}
1422
     */
1423
    public function getExtraFieldByName($attributeName)
1424
    {
1425
        foreach ($this->extraFields as $attribute) {
1426
            if ($attribute->getName() === $attributeName) {
1427
                return $attribute;
1428
            }
1429
        }
1430
1431
        return null;
1432
    }
1433
1434
    /**
1435
     * Get sessionCourseSubscription.
1436
     *
1437
     * @return ArrayCollection
1438
     */
1439
    public function getSessionCourseSubscriptions()
1440
    {
1441
        return $this->sessionCourseSubscriptions;
1442
    }
1443
1444
    /**
1445
     * @return string
1446
     */
1447
    public function getConfirmationToken()
1448
    {
1449
        return $this->confirmationToken;
1450
    }
1451
1452
    /**
1453
     * @param string $confirmationToken
1454
     *
1455
     * @return User
1456
     */
1457
    public function setConfirmationToken($confirmationToken)
1458
    {
1459
        $this->confirmationToken = $confirmationToken;
1460
1461
        return $this;
1462
    }
1463
1464
    /**
1465
     * @return \DateTime
1466
     */
1467
    public function getPasswordRequestedAt()
1468
    {
1469
        return $this->passwordRequestedAt;
1470
    }
1471
1472
    /**
1473
     * @param int $ttl
1474
     *
1475
     * @return bool
1476
     */
1477
    public function isPasswordRequestNonExpired($ttl)
1478
    {
1479
        return $this->getPasswordRequestedAt() instanceof \DateTime &&
1480
        $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
1481
    }
1482
1483
    public function getUsername()
1484
    {
1485
        return $this->username;
1486
    }
1487
1488
    /**
1489
     * Returns the creation date.
1490
     *
1491
     * @return \DateTime|null
1492
     */
1493
    public function getCreatedAt()
1494
    {
1495
        return $this->createdAt;
1496
    }
1497
1498
    /**
1499
     * Sets the last update date.
1500
     *
1501
     * @return User
1502
     */
1503
    public function setUpdatedAt(\DateTime $updatedAt = null)
1504
    {
1505
        $this->updatedAt = $updatedAt;
1506
1507
        return $this;
1508
    }
1509
1510
    /**
1511
     * Returns the last update date.
1512
     *
1513
     * @return \DateTime|null
1514
     */
1515
    public function getUpdatedAt()
1516
    {
1517
        return $this->updatedAt;
1518
    }
1519
1520
    /**
1521
     * Returns the expiration date.
1522
     *
1523
     * @return \DateTime|null
1524
     */
1525
    public function getExpiresAt()
1526
    {
1527
        return $this->expiresAt;
1528
    }
1529
1530
    /**
1531
     * Returns the credentials expiration date.
1532
     *
1533
     * @return \DateTime
1534
     */
1535
    public function getCredentialsExpireAt()
1536
    {
1537
        return $this->credentialsExpireAt;
1538
    }
1539
1540
    /**
1541
     * Sets the credentials expiration date.
1542
     *
1543
     * @return User
1544
     */
1545
    public function setCredentialsExpireAt(\DateTime $date = null)
1546
    {
1547
        $this->credentialsExpireAt = $date;
1548
1549
        return $this;
1550
    }
1551
1552
    /**
1553
     * Sets the user groups.
1554
     *
1555
     * @param array $groups
1556
     *
1557
     * @return User
1558
     */
1559
    public function setGroups($groups)
1560
    {
1561
        foreach ($groups as $group) {
1562
            $this->addGroup($group);
1563
        }
1564
1565
        return $this;
1566
    }
1567
1568
    /**
1569
     * Sets the two-step verification code.
1570
     *
1571
     * @param string $twoStepVerificationCode
1572
     *
1573
     * @return User
1574
     */
1575
    public function setTwoStepVerificationCode($twoStepVerificationCode)
1576
    {
1577
        $this->twoStepVerificationCode = $twoStepVerificationCode;
1578
1579
        return $this;
1580
    }
1581
1582
    /**
1583
     * Returns the two-step verification code.
1584
     *
1585
     * @return string
1586
     */
1587
    public function getTwoStepVerificationCode()
1588
    {
1589
        return $this->twoStepVerificationCode;
1590
    }
1591
1592
    /**
1593
     * @param string $biography
1594
     *
1595
     * @return User
1596
     */
1597
    public function setBiography($biography)
1598
    {
1599
        $this->biography = $biography;
1600
1601
        return $this;
1602
    }
1603
1604
    /**
1605
     * @return string
1606
     */
1607
    public function getBiography()
1608
    {
1609
        return $this->biography;
1610
    }
1611
1612
    /**
1613
     * @param \DateTime $dateOfBirth
1614
     *
1615
     * @return User
1616
     */
1617
    public function setDateOfBirth($dateOfBirth)
1618
    {
1619
        $this->dateOfBirth = $dateOfBirth;
1620
1621
        return $this;
1622
    }
1623
1624
    /**
1625
     * @return \DateTime
1626
     */
1627
    public function getDateOfBirth()
1628
    {
1629
        return $this->dateOfBirth;
1630
    }
1631
1632
    /**
1633
     * @param string $facebookData
1634
     *
1635
     * @return User
1636
     */
1637
    public function setFacebookData($facebookData)
1638
    {
1639
        $this->facebookData = $facebookData;
1640
1641
        return $this;
1642
    }
1643
1644
    /**
1645
     * @return string
1646
     */
1647
    public function getFacebookData()
1648
    {
1649
        return $this->facebookData;
1650
    }
1651
1652
    /**
1653
     * @param string $facebookName
1654
     *
1655
     * @return User
1656
     */
1657
    public function setFacebookName($facebookName)
1658
    {
1659
        $this->facebookName = $facebookName;
1660
1661
        return $this;
1662
    }
1663
1664
    /**
1665
     * @return string
1666
     */
1667
    public function getFacebookName()
1668
    {
1669
        return $this->facebookName;
1670
    }
1671
1672
    /**
1673
     * @param string $facebookUid
1674
     *
1675
     * @return User
1676
     */
1677
    public function setFacebookUid($facebookUid)
1678
    {
1679
        $this->facebookUid = $facebookUid;
1680
1681
        return $this;
1682
    }
1683
1684
    /**
1685
     * @return string
1686
     */
1687
    public function getFacebookUid()
1688
    {
1689
        return $this->facebookUid;
1690
    }
1691
1692
    /**
1693
     * @return string
1694
     */
1695
    public function getFirstname()
1696
    {
1697
        return $this->firstname;
1698
    }
1699
1700
    /**
1701
     * @param string $gender
1702
     *
1703
     * @return User
1704
     */
1705
    public function setGender($gender)
1706
    {
1707
        $this->gender = $gender;
1708
1709
        return $this;
1710
    }
1711
1712
    /**
1713
     * @return string
1714
     */
1715
    public function getGender()
1716
    {
1717
        return $this->gender;
1718
    }
1719
1720
    /**
1721
     * @param string $gplusData
1722
     *
1723
     * @return User
1724
     */
1725
    public function setGplusData($gplusData)
1726
    {
1727
        $this->gplusData = $gplusData;
1728
1729
        return $this;
1730
    }
1731
1732
    /**
1733
     * @return string
1734
     */
1735
    public function getGplusData()
1736
    {
1737
        return $this->gplusData;
1738
    }
1739
1740
    /**
1741
     * @param string $gplusName
1742
     *
1743
     * @return User
1744
     */
1745
    public function setGplusName($gplusName)
1746
    {
1747
        $this->gplusName = $gplusName;
1748
1749
        return $this;
1750
    }
1751
1752
    /**
1753
     * @return string
1754
     */
1755
    public function getGplusName()
1756
    {
1757
        return $this->gplusName;
1758
    }
1759
1760
    /**
1761
     * @param string $gplusUid
1762
     *
1763
     * @return User
1764
     */
1765
    public function setGplusUid($gplusUid)
1766
    {
1767
        $this->gplusUid = $gplusUid;
1768
1769
        return $this;
1770
    }
1771
1772
    /**
1773
     * @return string
1774
     */
1775
    public function getGplusUid()
1776
    {
1777
        return $this->gplusUid;
1778
    }
1779
1780
    /**
1781
     * @return string
1782
     */
1783
    public function getLastname()
1784
    {
1785
        return $this->lastname;
1786
    }
1787
1788
    /**
1789
     * @param string $locale
1790
     *
1791
     * @return User
1792
     */
1793
    public function setLocale($locale)
1794
    {
1795
        $this->locale = $locale;
1796
1797
        return $this;
1798
    }
1799
1800
    /**
1801
     * @return string
1802
     */
1803
    public function getLocale()
1804
    {
1805
        return $this->locale;
1806
    }
1807
1808
    /**
1809
     * @param string $timezone
1810
     *
1811
     * @return User
1812
     */
1813
    public function setTimezone($timezone)
1814
    {
1815
        $this->timezone = $timezone;
1816
1817
        return $this;
1818
    }
1819
1820
    /**
1821
     * @return string
1822
     */
1823
    public function getTimezone()
1824
    {
1825
        return $this->timezone;
1826
    }
1827
1828
    /**
1829
     * @param string $twitterData
1830
     *
1831
     * @return User
1832
     */
1833
    public function setTwitterData($twitterData)
1834
    {
1835
        $this->twitterData = $twitterData;
1836
1837
        return $this;
1838
    }
1839
1840
    /**
1841
     * @return string
1842
     */
1843
    public function getTwitterData()
1844
    {
1845
        return $this->twitterData;
1846
    }
1847
1848
    /**
1849
     * @param string $twitterName
1850
     *
1851
     * @return User
1852
     */
1853
    public function setTwitterName($twitterName)
1854
    {
1855
        $this->twitterName = $twitterName;
1856
1857
        return $this;
1858
    }
1859
1860
    /**
1861
     * @return string
1862
     */
1863
    public function getTwitterName()
1864
    {
1865
        return $this->twitterName;
1866
    }
1867
1868
    /**
1869
     * @param string $twitterUid
1870
     *
1871
     * @return User
1872
     */
1873
    public function setTwitterUid($twitterUid)
1874
    {
1875
        $this->twitterUid = $twitterUid;
1876
1877
        return $this;
1878
    }
1879
1880
    /**
1881
     * @return string
1882
     */
1883
    public function getTwitterUid()
1884
    {
1885
        return $this->twitterUid;
1886
    }
1887
1888
    /**
1889
     * @param string $website
1890
     *
1891
     * @return User
1892
     */
1893
    public function setWebsite($website)
1894
    {
1895
        $this->website = $website;
1896
1897
        return $this;
1898
    }
1899
1900
    /**
1901
     * @return string
1902
     */
1903
    public function getWebsite()
1904
    {
1905
        return $this->website;
1906
    }
1907
1908
    /**
1909
     * @param string $token
1910
     *
1911
     * @return User
1912
     */
1913
    public function setToken($token)
1914
    {
1915
        $this->token = $token;
1916
1917
        return $this;
1918
    }
1919
1920
    /**
1921
     * @return string
1922
     */
1923
    public function getToken()
1924
    {
1925
        return $this->token;
1926
    }
1927
1928
    /**
1929
     * @return string
1930
     */
1931
    public function getFullname()
1932
    {
1933
        return sprintf('%s %s', $this->getFirstname(), $this->getLastname());
1934
    }
1935
1936
    /**
1937
     * @return array
1938
     */
1939
    public function getRealRoles()
1940
    {
1941
        return $this->roles;
1942
    }
1943
1944
    /**
1945
     * @return User
1946
     */
1947
    public function setRealRoles(array $roles)
1948
    {
1949
        $this->setRoles($roles);
1950
1951
        return $this;
1952
    }
1953
1954
    /**
1955
     * Removes sensitive data from the user.
1956
     */
1957
    public function eraseCredentials()
1958
    {
1959
        $this->plainPassword = null;
1960
    }
1961
1962
    public function getUsernameCanonical()
1963
    {
1964
        return $this->usernameCanonical;
1965
    }
1966
1967
    public function getEmailCanonical()
1968
    {
1969
        return $this->emailCanonical;
1970
    }
1971
1972
    public function getPlainPassword()
1973
    {
1974
        return $this->plainPassword;
1975
    }
1976
1977
    /**
1978
     * Returns the user roles.
1979
     *
1980
     * @return array The roles
1981
     */
1982
    public function getRoles()
1983
    {
1984
        $roles = $this->roles;
1985
1986
        foreach ($this->getGroups() as $group) {
1987
            $roles = array_merge($roles, $group->getRoles());
1988
        }
1989
1990
        // we need to make sure to have at least one role
1991
        $roles[] = static::ROLE_DEFAULT;
1992
1993
        return array_unique($roles);
1994
    }
1995
1996
    /**
1997
     * Never use this to check if this user has access to anything!
1998
     *
1999
     * Use the SecurityContext, or an implementation of AccessDecisionManager
2000
     * instead, e.g.
2001
     *
2002
     *         $securityContext->isGranted('ROLE_USER');
2003
     *
2004
     * @param string $role
2005
     *
2006
     * @return bool
2007
     */
2008
    public function hasRole($role)
2009
    {
2010
        return in_array(strtoupper($role), $this->getRoles(), true);
2011
    }
2012
2013
    public function isAccountNonExpired()
2014
    {
2015
        if (true === $this->expired) {
2016
            return false;
2017
        }
2018
2019
        if (null !== $this->expiresAt && $this->expiresAt->getTimestamp() < time()) {
2020
            return false;
2021
        }
2022
2023
        return true;
2024
    }
2025
2026
    public function isAccountNonLocked()
2027
    {
2028
        return !$this->locked;
2029
    }
2030
2031
    public function isCredentialsNonExpired()
2032
    {
2033
        if (true === $this->credentialsExpired) {
2034
            return false;
2035
        }
2036
2037
        if (null !== $this->credentialsExpireAt && $this->credentialsExpireAt->getTimestamp() < time()) {
2038
            return false;
2039
        }
2040
2041
        return true;
2042
    }
2043
2044
    public function isCredentialsExpired()
2045
    {
2046
        return !$this->isCredentialsNonExpired();
2047
    }
2048
2049
    public function isExpired()
2050
    {
2051
        return !$this->isAccountNonExpired();
2052
    }
2053
2054
    public function isLocked()
2055
    {
2056
        return !$this->isAccountNonLocked();
2057
    }
2058
2059
    public function isSuperAdmin()
2060
    {
2061
        return $this->hasRole(static::ROLE_SUPER_ADMIN);
2062
    }
2063
2064
    public function isUser(UserInterface $user = null)
2065
    {
2066
        return null !== $user && $this->getId() === $user->getId();
2067
    }
2068
2069
    public function removeRole($role)
2070
    {
2071
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
2072
            unset($this->roles[$key]);
2073
            $this->roles = array_values($this->roles);
2074
        }
2075
2076
        return $this;
2077
    }
2078
2079
    public function setUsername($username)
2080
    {
2081
        $this->username = $username;
2082
2083
        return $this;
2084
    }
2085
2086
    public function setUsernameCanonical($usernameCanonical)
2087
    {
2088
        $this->usernameCanonical = $usernameCanonical;
2089
2090
        return $this;
2091
    }
2092
2093
    /**
2094
     * @param bool $boolean
2095
     *
2096
     * @return User
2097
     */
2098
    public function setCredentialsExpired($boolean)
2099
    {
2100
        $this->credentialsExpired = $boolean;
2101
2102
        return $this;
2103
    }
2104
2105
    public function setEmailCanonical($emailCanonical)
2106
    {
2107
        $this->emailCanonical = $emailCanonical;
2108
2109
        return $this;
2110
    }
2111
2112
    public function setEnabled($boolean)
2113
    {
2114
        $this->enabled = (bool) $boolean;
2115
2116
        return $this;
2117
    }
2118
2119
    /**
2120
     * Sets this user to expired.
2121
     *
2122
     * @param bool $boolean
2123
     *
2124
     * @return User
2125
     */
2126
    public function setExpired($boolean)
2127
    {
2128
        $this->expired = (bool) $boolean;
2129
2130
        return $this;
2131
    }
2132
2133
    /**
2134
     * @param \DateTime $date
2135
     *
2136
     * @return User
2137
     */
2138
    public function setExpiresAt(\DateTime $date = null)
2139
    {
2140
        $this->expiresAt = $date;
2141
2142
        return $this;
2143
    }
2144
2145
    public function setSuperAdmin($boolean)
2146
    {
2147
        if (true === $boolean) {
2148
            $this->addRole(static::ROLE_SUPER_ADMIN);
2149
        } else {
2150
            $this->removeRole(static::ROLE_SUPER_ADMIN);
2151
        }
2152
2153
        return $this;
2154
    }
2155
2156
    public function setPlainPassword($password)
2157
    {
2158
        $this->plainPassword = $password;
2159
2160
        return $this;
2161
    }
2162
2163
    public function setLocked($boolean)
2164
    {
2165
        $this->locked = $boolean;
2166
2167
        return $this;
2168
    }
2169
2170
    public function setPasswordRequestedAt(\DateTime $date = null)
2171
    {
2172
        $this->passwordRequestedAt = $date;
2173
2174
        return $this;
2175
    }
2176
2177
    public function setRoles(array $roles)
2178
    {
2179
        $this->roles = [];
2180
2181
        foreach ($roles as $role) {
2182
            $this->addRole($role);
2183
        }
2184
2185
        return $this;
2186
    }
2187
2188
    /**
2189
     * Gets the groups granted to the user.
2190
     *
2191
     * @return Collection
2192
     */
2193
    public function getGroups()
2194
    {
2195
        return $this->groups ?: $this->groups = new ArrayCollection();
2196
    }
2197
2198
    public function getGroupNames()
2199
    {
2200
        $names = [];
2201
        foreach ($this->getGroups() as $group) {
2202
            $names[] = $group->getName();
2203
        }
2204
2205
        return $names;
2206
    }
2207
2208
    public function hasGroup($name)
2209
    {
2210
        return in_array($name, $this->getGroupNames());
2211
    }
2212
2213
    public function addGroup(GroupInterface $group)
2214
    {
2215
        if (!$this->getGroups()->contains($group)) {
2216
            $this->getGroups()->add($group);
2217
        }
2218
2219
        return $this;
2220
    }
2221
2222
    public function removeGroup(GroupInterface $group)
2223
    {
2224
        if ($this->getGroups()->contains($group)) {
2225
            $this->getGroups()->removeElement($group);
2226
        }
2227
2228
        return $this;
2229
    }
2230
2231
    public function addRole($role)
2232
    {
2233
        $role = strtoupper($role);
2234
        if ($role === static::ROLE_DEFAULT) {
2235
            return $this;
2236
        }
2237
2238
        if (!in_array($role, $this->roles, true)) {
2239
            $this->roles[] = $role;
2240
        }
2241
2242
        return $this;
2243
    }
2244
2245
    /**
2246
     * Serializes the user.
2247
     *
2248
     * The serialized data have to contain the fields used by the equals method and the username.
2249
     *
2250
     * @return string
2251
     */
2252
    public function serialize()
2253
    {
2254
        return serialize([
2255
            $this->password,
2256
            $this->salt,
2257
            $this->usernameCanonical,
2258
            $this->username,
2259
            $this->expired,
2260
            $this->locked,
2261
            $this->credentialsExpired,
2262
            $this->enabled,
2263
            $this->id,
2264
        ]);
2265
    }
2266
2267
    /**
2268
     * Unserializes the user.
2269
     *
2270
     * @param string $serialized
2271
     */
2272
    public function unserialize($serialized)
2273
    {
2274
        $data = \UnserializeApi::unserialize('not_allowed_classes', $serialized);
2275
        // add a few extra elements in the array to ensure that we have enough keys when unserializing
2276
        // older data which does not include all properties.
2277
        $data = array_merge($data, array_fill(0, 2, null));
2278
2279
        list(
2280
            $this->password,
2281
            $this->salt,
2282
            $this->usernameCanonical,
2283
            $this->username,
2284
            $this->expired,
2285
            $this->locked,
2286
            $this->credentialsExpired,
2287
            $this->enabled,
2288
            $this->id
2289
            ) = $data;
2290
    }
2291
}
2292