User::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2017: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Entity;
22
23
use AppBundle\Entity\Traits\UserBlameableTrait;
24
use Doctrine\Common\Collections\Collection;
25
use Doctrine\ORM\Mapping as ORM;
26
use Gedmo\Mapping\Annotation as Gedmo;
27
use Gedmo\Timestampable\Traits\TimestampableEntity;
28
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
29
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
30
use Symfony\Component\Validator\Constraints as Assert;
31
use Symfony\Component\Validator\Context\ExecutionContextInterface;
32
33
/**
34
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
35
 * @UniqueEntity("loginUsername")
36
 * @UniqueEntity("emailAddress")
37
 */
38
class User implements AdvancedUserInterface
39
{
40
    use TimestampableEntity;
41
    use UserBlameableTrait;
42
43
    const GENDER_NEUTRAL = 0;
44
    const GENDER_MALE = 1;
45
    const GENDER_FEMALE = 2;
46
47
    /**
48
     * @ORM\Id
49
     * @ORM\GeneratedValue(strategy="AUTO")
50
     * @ORM\Column(type="integer")
51
     * @var int
52
     */
53
    private $id;
54
55
    /**
56
     * @ORM\Column(type="string", unique=true, nullable=true)
57
     * @Assert\Regex(pattern="/[@ ]{1,}/", match=false, message="login_username.invalid_chars", htmlPattern=false)
58
     * @var string
59
     */
60
    private $loginUsername;
61
62
    /**
63
     * @ORM\Column(type="string", unique=true, nullable=true)
64
     * @var string
65
     */
66
    private $password;
67
68
    /**
69
     * @var \DateTime
70
     * @Gedmo\Timestampable(on="change", field="password")
71
     * @ORM\Column(type="datetime", nullable=true)
72
     */
73
    protected $passwordChangedAt;
74
75
    /**
76
     * @ORM\Column(type="string")
77
     * @Assert\NotBlank
78
     * @var string
79
     */
80
    private $firstName;
81
82
    /**
83
     * @ORM\Column(type="string")
84
     * @Assert\NotBlank
85
     * @var string
86
     */
87
    private $lastName;
88
89
    /**
90
     * @ORM\Column(type="boolean")
91
     * @var bool
92
     */
93
    private $enabled;
94
95
    /**
96
     * @ORM\Column(type="boolean")
97
     * @var bool
98
     */
99
    private $globalAdministrator;
100
101
    /**
102
     * @ORM\Column(type="string", unique=true, nullable=true)
103
     * @Assert\Email
104
     * @var string
105
     */
106
    private $emailAddress;
107
108
    /**
109
     * @ORM\Column(type="string", nullable=true)
110
     * @var string
111
     */
112
    private $internalCode;
113
114
    /**
115
     * @ORM\Column(type="integer")
116
     * @var int
117
     */
118
    private $gender;
119
120
    /**
121
     * @ORM\Column(type="string", nullable=true)
122
     * @var string
123
     */
124
    private $token;
125
126
    /**
127
     * @ORM\Column(type="string", nullable=true)
128
     * @var string
129
     */
130
    private $tokenType;
131
132
    /**
133
     * @ORM\Column(type="datetime", nullable=true)
134
     * @var \DateTime
135
     */
136
    private $tokenExpiration;
137
138
    /**
139
     * @ORM\Column(type="datetime", nullable=true)
140
     * @var \DateTime
141
     */
142
    private $lastAccess;
143
144
    /**
145
     * @ORM\Column(type="datetime", nullable=true)
146
     * @var \DateTime
147
     */
148
    private $blockedUntil;
149
150
    /**
151
     * @ORM\OneToMany(targetEntity="Membership", mappedBy="user")
152
     * @var Collection
153
     */
154
    private $memberships;
155
156
    /**
157
     * @ORM\ManyToMany(targetEntity="Organization", mappedBy="administrators")
158
     * @var Collection
159
     */
160
    private $managedOrganizations;
161
162
    /**
163
     * @ORM\ManyToOne(targetEntity="Organization")
164
     * @ORM\JoinColumn(nullable=true)
165
     * @var Organization|null
166
     */
167
    protected $defaultOrganization;
168
169
    /**
170
     * @ORM\OneToMany(targetEntity="Role", mappedBy="user")
171
     * @var Collection
172
     */
173
    private $assignedRoles;
174
175
    /**
176
     * @ORM\Column(type="boolean")
177
     * @var bool
178
     */
179
    private $externalCheck;
180
181
    /**
182
     * @ORM\Column(type="boolean")
183
     * @var bool
184
     */
185
    protected $allowExternalCheck;
186
187
    /**
188
     * Convertir usuario en cadena
189
     *
190
     * @return string
191
     */
192
    public function __toString()
193
    {
194
        return $this->getFirstName().' '.$this->getLastName();
195
    }
196
197
    /**
198
     * Convertir usuario en cadena
199
     *
200
     * @return string
201
     */
202
    public function getFullName()
203
    {
204
        return (string) $this.' ('.$this->getUsernameAndEmailAddress().')';
205
    }
206
207
    /**
208
     * Constructor
209
     */
210
    public function __construct()
211
    {
212
        $this->memberships = new \Doctrine\Common\Collections\ArrayCollection();
213
        $this->managedOrganizations = new \Doctrine\Common\Collections\ArrayCollection();
214
        $this->assignedRoles = new \Doctrine\Common\Collections\ArrayCollection();
215
        $this->externalCheck = false;
216
        $this->allowExternalCheck = false;
217
    }
218
219
    /**
220
     * @return string
221
     */
222
    public function getUsernameAndEmailAddress()
223
    {
224
        return $this->loginUsername.(($this->loginUsername && $this->emailAddress) ? ' - ' : '').$this->emailAddress;
225
    }
226
    /**
227
     * Get id
228
     *
229
     * @return integer
230
     */
231
    public function getId()
232
    {
233
        return $this->id;
234
    }
235
236
    /**
237
     * Set userName
238
     *
239
     * @param string $loginUsername
240
     *
241
     * @return User
242
     */
243
    public function setLoginUsername($loginUsername)
244
    {
245
        $this->loginUsername = $loginUsername;
246
247
        return $this;
248
    }
249
250
    /**
251
     * Get userName
252
     *
253
     * @return string
254
     */
255
    public function getLoginUsername()
256
    {
257
        return $this->loginUsername;
258
    }
259
260
    /**
261
     * Set password
262
     *
263
     * @param string $password
264
     *
265
     * @return User
266
     */
267
    public function setPassword($password)
268
    {
269
        $this->password = $password;
270
271
        return $this;
272
    }
273
274
    /**
275
     * Get password
276
     *
277
     * @return string
278
     */
279
    public function getPassword()
280
    {
281
        return $this->password;
282
    }
283
284
    /**
285
     * Set firstName
286
     *
287
     * @param string $firstName
288
     *
289
     * @return User
290
     */
291
    public function setFirstName($firstName)
292
    {
293
        $this->firstName = $firstName;
294
295
        return $this;
296
    }
297
298
    /**
299
     * Get firstName
300
     *
301
     * @return string
302
     */
303
    public function getFirstName()
304
    {
305
        return $this->firstName;
306
    }
307
308
    /**
309
     * Set lastName
310
     *
311
     * @param string $lastName
312
     *
313
     * @return User
314
     */
315
    public function setLastName($lastName)
316
    {
317
        $this->lastName = $lastName;
318
319
        return $this;
320
    }
321
322
    /**
323
     * Get lastName
324
     *
325
     * @return string
326
     */
327
    public function getLastName()
328
    {
329
        return $this->lastName;
330
    }
331
332
    /**
333
     * Set enabled
334
     *
335
     * @param boolean $enabled
336
     *
337
     * @return User
338
     */
339
    public function setEnabled($enabled)
340
    {
341
        $this->enabled = $enabled;
342
343
        return $this;
344
    }
345
346
347
    /**
348
     * Set globalAdmin
349
     *
350
     * @param boolean $globalAdministrator
351
     *
352
     * @return User
353
     */
354
    public function setGlobalAdministrator($globalAdministrator)
355
    {
356
        $this->globalAdministrator = $globalAdministrator;
357
358
        return $this;
359
    }
360
361
    /**
362
     * Get globalAdmin
363
     *
364
     * @return boolean
365
     */
366
    public function isGlobalAdministrator()
367
    {
368
        return $this->globalAdministrator;
369
    }
370
371
    /**
372
     * Get internal code
373
     *
374
     * @return string
375
     */
376
    public function getInternalCode()
377
    {
378
        return $this->internalCode;
379
    }
380
381
    /**
382
     * Set internal code
383
     *
384
     * @param string $internalCode
385
     * @return User
386
     */
387
    public function setInternalCode($internalCode)
388
    {
389
        $this->internalCode = $internalCode;
390
        return $this;
391
    }
392
393
    /**
394
     * Set emailAddress
395
     *
396
     * @param string $emailAddress
397
     *
398
     * @return User
399
     */
400
    public function setEmailAddress($emailAddress)
401
    {
402
        $this->emailAddress = $emailAddress;
403
404
        return $this;
405
    }
406
407
    /**
408
     * Get emailAddress
409
     *
410
     * @return string
411
     */
412
    public function getEmailAddress()
413
    {
414
        return $this->emailAddress;
415
    }
416
417
    /**
418
     * Set gender
419
     *
420
     * @param integer $gender
421
     *
422
     * @return User
423
     */
424
    public function setGender($gender)
425
    {
426
        $this->gender = $gender;
427
428
        return $this;
429
    }
430
431
    /**
432
     * Get gender
433
     *
434
     * @return integer
435
     */
436
    public function getGender()
437
    {
438
        return $this->gender;
439
    }
440
441
    /**
442
     * Set token
443
     *
444
     * @param string $token
445
     *
446
     * @return User
447
     */
448
    public function setToken($token)
449
    {
450
        $this->token = $token;
451
452
        return $this;
453
    }
454
455
    /**
456
     * Get token
457
     *
458
     * @return string
459
     */
460
    public function getToken()
461
    {
462
        return $this->token;
463
    }
464
465
    /**
466
     * Set tokenType
467
     *
468
     * @param string $tokenType
469
     *
470
     * @return User
471
     */
472
    public function setTokenType($tokenType)
473
    {
474
        $this->tokenType = $tokenType;
475
476
        return $this;
477
    }
478
479
    /**
480
     * Get tokenType
481
     *
482
     * @return string
483
     */
484
    public function getTokenType()
485
    {
486
        return $this->tokenType;
487
    }
488
489
    /**
490
     * Set tokenExpiration
491
     *
492
     * @param \DateTime $tokenExpiration
493
     *
494
     * @return User
495
     */
496
    public function setTokenExpiration($tokenExpiration)
497
    {
498
        $this->tokenExpiration = $tokenExpiration;
499
500
        return $this;
501
    }
502
503
    /**
504
     * Get tokenExpiration
505
     *
506
     * @return \DateTime
507
     */
508
    public function getTokenExpiration()
509
    {
510
        return $this->tokenExpiration;
511
    }
512
513
    /**
514
     * Set lastAccess
515
     *
516
     * @param \DateTime $lastAccess
517
     *
518
     * @return User
519
     */
520
    public function setLastAccess($lastAccess)
521
    {
522
        $this->lastAccess = $lastAccess;
523
524
        return $this;
525
    }
526
527
    /**
528
     * Get lastAccess
529
     *
530
     * @return \DateTime
531
     */
532
    public function getLastAccess()
533
    {
534
        return $this->lastAccess;
535
    }
536
537
    /**
538
     * Set blockedUntil
539
     *
540
     * @param \DateTime $blockedUntil
541
     *
542
     * @return User
543
     */
544
    public function setBlockedUntil($blockedUntil)
545
    {
546
        $this->blockedUntil = $blockedUntil;
547
548
        return $this;
549
    }
550
551
    /**
552
     * Get blockedUntil
553
     *
554
     * @return \DateTime
555
     */
556
    public function getBlockedUntil()
557
    {
558
        return $this->blockedUntil;
559
    }
560
561
    /**
562
     * Add membership
563
     *
564
     * @param Membership $membership
565
     *
566
     * @return User
567
     */
568
    public function addMembership(Membership $membership)
569
    {
570
        $this->memberships[] = $membership;
571
572
        return $this;
573
    }
574
575
    /**
576
     * Remove membership
577
     *
578
     * @param Membership $membership
579
     */
580
    public function removeMembership(Membership $membership)
581
    {
582
        $this->memberships->removeElement($membership);
583
    }
584
585
    /**
586
     * Get memberships
587
     *
588
     * @return Collection
589
     */
590
    public function getMemberships()
591
    {
592
        return $this->memberships;
593
    }
594
595
    /**
596
     * Set defaultOrganization
597
     *
598
     * @param Organization $defaultOrganization
599
     *
600
     * @return User
601
     */
602
    public function setDefaultOrganization(Organization $defaultOrganization = null)
603
    {
604
        $this->defaultOrganization = $defaultOrganization;
605
606
        return $this;
607
    }
608
609
    /**
610
     * Get defaultOrganization
611
     *
612
     * @return Organization|null
613
     */
614
    public function getDefaultOrganization()
615
    {
616
        return $this->defaultOrganization;
617
    }
618
619
    /**
620
     * @Assert\Callback
621
     */
622
    public function validate(ExecutionContextInterface $context)
623
    {
624
        // comprobar si se ha especificado al menos el nombre de usuario o el correo electrónico
625
        if (!$this->getLoginUsername() && !$this->getEmailAddress()) {
626
            $context->buildViolation('user.id.not_found')
627
                ->atPath('userName')
628
                ->addViolation();
629
            $context->buildViolation('user.id.not_found')
630
                ->atPath('emailAddress')
631
                ->addViolation();
632
        }
633
    }
634
635
    /**
636
     * Returns the username used to authenticate the user.
637
     *
638
     * @return string The username
639
     */
640
    public function getUsername()
641
    {
642
        return $this->getLoginUsername() ?: $this->getEmailAddress();
643
    }
644
645
    /**
646
     * Checks whether the user's account has expired.
647
     *
648
     * Internally, if this method returns false, the authentication system
649
     * will throw an AccountExpiredException and prevent login.
650
     *
651
     * @return bool true if the user's account is non expired, false otherwise
652
     *
653
     * @see AccountExpiredException
654
     */
655
    public function isAccountNonExpired()
656
    {
657
        return true;
658
    }
659
660
    /**
661
     * Checks whether the user is locked.
662
     *
663
     * Internally, if this method returns false, the authentication system
664
     * will throw a LockedException and prevent login.
665
     *
666
     * @return bool true if the user is not locked, false otherwise
667
     *
668
     * @see LockedException
669
     */
670
    public function isAccountNonLocked()
671
    {
672
        return $this->getBlockedUntil() ? ($this->getBlockedUntil() <= new \DateTime()) : true;
673
    }
674
675
    /**
676
     * Checks whether the user's credentials (password) has expired.
677
     *
678
     * Internally, if this method returns false, the authentication system
679
     * will throw a CredentialsExpiredException and prevent login.
680
     *
681
     * @return bool true if the user's credentials are non expired, false otherwise
682
     *
683
     * @see CredentialsExpiredException
684
     */
685
    public function isCredentialsNonExpired()
686
    {
687
        return true;
688
    }
689
690
    /**
691
     * Checks whether the user is enabled.
692
     *
693
     * Internally, if this method returns false, the authentication system
694
     * will throw a DisabledException and prevent login.
695
     *
696
     * @return bool true if the user is enabled, false otherwise
697
     *
698
     * @see DisabledException
699
     */
700
    public function isEnabled()
701
    {
702
        return $this->enabled;
703
    }
704
705
    /**
706
     * Returns the roles granted to the user.
707
     *
708
     * <code>
709
     * public function getRoles()
710
     * {
711
     *     return array('ROLE_USER');
712
     * }
713
     * </code>
714
     *
715
     * Alternatively, the roles might be stored on a ``roles`` property,
716
     * and populated in any number of different ways when the user object
717
     * is created.
718
     *
719
     * @return string[] The user roles
720
     */
721
    public function getRoles()
722
    {
723
        $roles = ['ROLE_USER'];
724
        if ($this->isGlobalAdministrator()) {
725
            $roles[] = 'ROLE_ADMIN';
726
        }
727
        return $roles;
728
    }
729
730
    /**
731
     * Returns the salt that was originally used to encode the password.
732
     *
733
     * This can return null if the password was not encoded using a salt.
734
     *
735
     * @return string|null The salt
736
     */
737
    public function getSalt()
738
    {
739
        return null;
740
    }
741
742
    /**
743
     * Removes sensitive data from the user.
744
     *
745
     * This is important if, at any given point, sensitive information like
746
     * the plain-text password is stored on this object.
747
     */
748
    public function eraseCredentials()
749
    {
750
    }
751
752
    /** @see \Serializable::serialize() */
753
    public function serialize()
754
    {
755
        return serialize(array(
756
            $this->id,
757
            $this->loginUsername,
758
            $this->emailAddress,
759
            $this->password,
760
            $this->enabled
761
        ));
762
    }
763
764
    /** @see \Serializable::unserialize() */
765
    public function unserialize($serialized)
766
    {
767
        list (
768
            $this->id,
769
            $this->loginUsername,
770
            $this->emailAddress,
771
            $this->password,
772
            $this->enabled
773
            ) = unserialize($serialized);
774
    }
775
776
    /**
777
     * Add managedOrganization
778
     *
779
     * @param Organization $managedOrganization
780
     *
781
     * @return User
782
     */
783
    public function addManagedOrganization(Organization $managedOrganization)
784
    {
785
        $this->managedOrganizations[] = $managedOrganization;
786
787
        return $this;
788
    }
789
790
    /**
791
     * Remove managedOrganization
792
     *
793
     * @param Organization $managedOrganization
794
     */
795
    public function removeManagedOrganization(Organization $managedOrganization)
796
    {
797
        $this->managedOrganizations->removeElement($managedOrganization);
798
    }
799
800
    /**
801
     * Get managedOrganizations
802
     *
803
     * @return Collection
804
     */
805
    public function getManagedOrganizations()
806
    {
807
        return $this->managedOrganizations;
808
    }
809
810
    /**
811
     * Add assigned role
812
     *
813
     * @param Role $assignedRole
814
     *
815
     * @return User
816
     */
817
    public function addAssignedRole(Role $assignedRole)
818
    {
819
        if (!$this->assignedRoles->contains($assignedRole)) {
820
            $this->assignedRoles[] = $assignedRole;
821
        }
822
823
        return $this;
824
    }
825
826
    /**
827
     * Remove assigned role
828
     *
829
     * @param Element $assignedRole
830
     */
831
    public function removeRole(Role $assignedRole)
832
    {
833
        if ($this->assignedRoles->contains($assignedRole)) {
834
            $this->assignedRoles->removeElement($assignedRole);
835
        }
836
    }
837
838
    /**
839
     * Get assigned roles
840
     *
841
     * @return Collection
842
     */
843
    public function getAssignedRoles()
844
    {
845
        return $this->assignedRoles;
846
    }
847
848
    /**
849
     * Set externalCheck
850
     *
851
     * @param boolean $externalCheck
852
     *
853
     * @return User
854
     */
855
    public function setExternalCheck($externalCheck)
856
    {
857
        $this->externalCheck = $this->allowExternalCheck && $externalCheck;
858
859
        return $this;
860
    }
861
862
    /**
863
     * Get externalCheck
864
     *
865
     * @return boolean
866
     */
867
    public function getExternalCheck()
868
    {
869
        return $this->externalCheck;
870
    }
871
872
    /**
873
     * Set allowExternalCheck
874
     *
875
     * @param boolean $allowExternalCheck
876
     *
877
     * @return User
878
     */
879
    public function setAllowExternalCheck($allowExternalCheck)
880
    {
881
        $this->allowExternalCheck = $allowExternalCheck;
882
883
        if (!$allowExternalCheck) {
884
            $this->externalCheck = false;
885
        }
886
887
        return $this;
888
    }
889
890
    /**
891
     * Get allowExternalCheck
892
     *
893
     * @return boolean
894
     */
895
    public function getAllowExternalCheck()
896
    {
897
        return $this->allowExternalCheck;
898
    }
899
}
900