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