|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Entity\Staff; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
6
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
7
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
8
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @ORM\Table(name="app_users") |
|
12
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\Staff\User2Repository") |
|
13
|
|
|
* @UniqueEntity( |
|
14
|
|
|
* fields={"email"}, |
|
15
|
|
|
* message="L'email que vous avez indiqué existe déjà !!" |
|
16
|
|
|
* ) |
|
17
|
|
|
*/ |
|
18
|
|
|
class User2 implements UserInterface |
|
19
|
|
|
{ |
|
20
|
|
|
const ROLE_DEFAULT = 'ROLE_USER'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @ORM\Id() |
|
24
|
|
|
* @ORM\GeneratedValue() |
|
25
|
|
|
* @ORM\Column(type="integer") |
|
26
|
|
|
*/ |
|
27
|
|
|
private $id; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @ORM\Column(type="string", length=255) |
|
31
|
|
|
* @Assert\Email() |
|
32
|
|
|
*/ |
|
33
|
|
|
private $email; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @ORM\Column(type="string", length=255) |
|
37
|
|
|
*/ |
|
38
|
|
|
private $username; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @ORM\Column(type="string", length=255) |
|
42
|
|
|
* @Assert\Length(min="8", minMessage="Votre mot de passe doit faire minimum 8 caractères") |
|
43
|
|
|
*/ |
|
44
|
|
|
private $password; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @Assert\EqualTo(propertyPath="password", message="Vous n'avez pas typé le même mot de passe !!") |
|
48
|
|
|
*/ |
|
49
|
|
|
public $confirmPassword; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @ORM\Column(name="is_active", type="boolean") |
|
53
|
|
|
*/ |
|
54
|
|
|
private $isActive; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @ORM\Column(name="roles", type="array") |
|
58
|
|
|
*/ |
|
59
|
|
|
private $roles = []; |
|
60
|
|
|
|
|
61
|
|
|
public function getId(): ?int |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->id; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getEmail(): ?string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->email; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function setEmail(string $email): self |
|
72
|
|
|
{ |
|
73
|
|
|
$this->email = $email; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getUsername(): ?string |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->username; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function setUsername(string $username): self |
|
84
|
|
|
{ |
|
85
|
|
|
$this->username = $username; |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getPassword(): ?string |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->password; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function setPassword(string $password): self |
|
96
|
|
|
{ |
|
97
|
|
|
$this->password = $password; |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function getSalt() |
|
103
|
|
|
{ |
|
104
|
|
|
// you *may* need a real salt depending on your encoder |
|
105
|
|
|
// see section on salt below |
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function getRoles(): array |
|
110
|
|
|
{ |
|
111
|
|
|
if (empty($this->roles)) { |
|
112
|
|
|
// guarantee every user at least has ROLE_USER |
|
113
|
|
|
$roles[] = 'ROLE_USER'; |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
$roles = $this->roles; |
|
116
|
|
|
|
|
117
|
|
|
return array_unique($roles); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function eraseCredentials() |
|
121
|
|
|
{ |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get the value of isActive |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getIsActive() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->isActive; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set the value of isActive |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setIsActive($isActive) :self |
|
136
|
|
|
{ |
|
137
|
|
|
$this->isActive = $isActive; |
|
138
|
|
|
|
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Sets the roles of the user. |
|
144
|
|
|
* |
|
145
|
|
|
* This overwrites any previous roles. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $roles |
|
148
|
|
|
*/ |
|
149
|
|
|
public function setRoles(array $roles = array()) :self |
|
150
|
|
|
{ |
|
151
|
|
|
$this->roles = array(); |
|
152
|
|
|
foreach ($roles as $role) { |
|
153
|
|
|
$this->addRoles($role); |
|
154
|
|
|
} |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Adds a role to the user. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $role |
|
162
|
|
|
* |
|
163
|
|
|
* @return static |
|
164
|
|
|
*/ |
|
165
|
|
|
public function addRoles($role) |
|
166
|
|
|
{ |
|
167
|
|
|
$role = strtoupper($role); |
|
168
|
|
|
if ($role === static::ROLE_DEFAULT) { |
|
169
|
|
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (!in_array($role, $this->roles, true)) { |
|
173
|
|
|
$this->roles[] = $role; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.