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