|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace App\Users\Entity; |
|
5
|
|
|
|
|
6
|
|
|
use App\Users\Entity\UserProfile; |
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
9
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
10
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
|
11
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
12
|
|
|
use JMS\Serializer\Annotation\ExclusionPolicy; |
|
13
|
|
|
use JMS\Serializer\Annotation\Expose; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @ORM\Entity(repositoryClass="App\Users\Repository\UserRepository") |
|
17
|
|
|
* @ORM\Table(name="users") |
|
18
|
|
|
* @UniqueEntity(fields="email", message="Email already taken") |
|
19
|
|
|
* @UniqueEntity(fields="username", message="Username already taken") |
|
20
|
|
|
* @ExclusionPolicy("all") |
|
21
|
|
|
*/ |
|
22
|
|
|
class User implements UserInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @ORM\Id() |
|
26
|
|
|
* @ORM\GeneratedValue() |
|
27
|
|
|
* @ORM\Column(type="integer") |
|
28
|
|
|
* @Expose |
|
29
|
|
|
*/ |
|
30
|
|
|
private $id; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var $profile UserProfile |
|
34
|
|
|
* @ORM\OneToOne(targetEntity="App\Users\Entity\UserProfile", cascade={"persist", "remove"}) |
|
35
|
|
|
* @Expose |
|
36
|
|
|
*/ |
|
37
|
|
|
private $profile; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
|
41
|
|
|
*/ |
|
42
|
|
|
private $email; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @ORM\Column(type="integer", length=1, options={"default": 0}) |
|
46
|
|
|
*/ |
|
47
|
|
|
private $isEmailConfirmed = 0; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
|
51
|
|
|
* @Expose |
|
52
|
|
|
*/ |
|
53
|
|
|
private $username; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @ORM\Embedded(class="App\Users\Entity\UserRoles", columnPrefix=false) |
|
57
|
|
|
*/ |
|
58
|
|
|
private $roles; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private $plainPassword; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @ORM\Column(type="string", length=64) |
|
67
|
|
|
*/ |
|
68
|
|
|
private $password; |
|
69
|
|
|
|
|
70
|
30 |
|
public function __construct(string $email, string $username, string $password) |
|
71
|
|
|
{ |
|
72
|
30 |
|
$this->roles = new UserRoles(); |
|
73
|
30 |
|
$this->profile = new UserProfile($this); |
|
74
|
|
|
|
|
75
|
30 |
|
$this->email = $email; |
|
76
|
30 |
|
$this->username = $username; |
|
77
|
30 |
|
$this->setPlainPassword($password); |
|
78
|
30 |
|
} |
|
79
|
|
|
|
|
80
|
9 |
|
public function getProfile(): UserProfile |
|
81
|
|
|
{ |
|
82
|
9 |
|
return $this->profile; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
public function getId(): ?int |
|
86
|
|
|
{ |
|
87
|
4 |
|
return $this->id; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
public function getEmail(): string |
|
91
|
|
|
{ |
|
92
|
4 |
|
return $this->email; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
30 |
|
public function setPlainPassword(string $plainPassword): self |
|
96
|
|
|
{ |
|
97
|
30 |
|
if (!empty($plainPassword)) { |
|
98
|
30 |
|
$this->plainPassword = $plainPassword; |
|
99
|
|
|
// Change some mapped values so preUpdate will get called. |
|
100
|
30 |
|
$this->password = ''; // just blank it out |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
30 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
6 |
|
public function getPlainPassword(): string |
|
107
|
|
|
{ |
|
108
|
6 |
|
return (string)$this->plainPassword; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
8 |
|
public function setPassword($password, UserPasswordEncoderInterface $passwordEncoder): self |
|
112
|
|
|
{ |
|
113
|
8 |
|
$this->plainPassword = $password; |
|
114
|
8 |
|
$this->password = $passwordEncoder->encodePassword($this, $password); |
|
115
|
|
|
|
|
116
|
8 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
6 |
|
public function isPasswordValid($password, UserPasswordEncoderInterface $passwordEncoder): bool |
|
120
|
|
|
{ |
|
121
|
6 |
|
return $passwordEncoder->isPasswordValid($this, $password); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
4 |
|
public function getPassword(): ?string |
|
125
|
|
|
{ |
|
126
|
4 |
|
return $this->password; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
9 |
|
public function getSalt(): ?string |
|
130
|
|
|
{ |
|
131
|
9 |
|
return null; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
4 |
|
public function getUsername(): ?string |
|
135
|
|
|
{ |
|
136
|
4 |
|
return $this->username; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
9 |
|
public function eraseCredentials(): self |
|
140
|
|
|
{ |
|
141
|
9 |
|
$this->plainPassword = null; |
|
142
|
|
|
|
|
143
|
9 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
1 |
|
public function confirmEmail(): self |
|
147
|
|
|
{ |
|
148
|
1 |
|
$this->isEmailConfirmed = 1; |
|
149
|
|
|
|
|
150
|
1 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function changeEmail($email): self |
|
154
|
|
|
{ |
|
155
|
|
|
$this->email = $email; |
|
156
|
|
|
$this->isEmailConfirmed = 0; |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
7 |
|
public function getRoles() |
|
165
|
|
|
{ |
|
166
|
7 |
|
return $this->getRolesObject()->getRoles(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
10 |
|
public function getRolesObject(): UserRoles |
|
170
|
|
|
{ |
|
171
|
10 |
|
return $this->roles; |
|
172
|
|
|
} |
|
173
|
|
|
} |