@@ -87,6 +87,177 @@ to add more fields. Also, make sure to make and run a migration for the new enti
87
87
.. _security-user-providers :
88
88
.. _where-do-users-come-from-user-providers :
89
89
90
+ This generates the following: 1) the User entity and 2) the User Repository
91
+
92
+ **Step 1. ** The User entity::
93
+
94
+ // src/Entity/User.php
95
+ namespace App\Entity;
96
+
97
+ use Doctrine\ORM\Mapping as ORM;
98
+ use Symfony\Component\Security\Core\User\UserInterface;
99
+
100
+ /**
101
+ * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
102
+ */
103
+ class User implements UserInterface
104
+ {
105
+ /**
106
+ * @ORM\Id()
107
+ * @ORM\GeneratedValue()
108
+ * @ORM\Column(type="integer")
109
+ */
110
+ private $id;
111
+
112
+ /**
113
+ * @ORM\Column(type="string", length=180, unique=true)
114
+ */
115
+ private $email;
116
+
117
+ /**
118
+ * @ORM\Column(type="json")
119
+ */
120
+ private $roles = [];
121
+
122
+ /**
123
+ * @var string The hashed password
124
+ * @ORM\Column(type="string")
125
+ */
126
+ private $password;
127
+
128
+ public function getId(): ?int
129
+ {
130
+ return $this->id;
131
+ }
132
+
133
+ public function getEmail(): ?string
134
+ {
135
+ return $this->email;
136
+ }
137
+
138
+ public function setEmail(string $email): self
139
+ {
140
+ $this->email = $email;
141
+
142
+ return $this;
143
+ }
144
+
145
+ /**
146
+ * A visual identifier that represents this user.
147
+ *
148
+ * @see UserInterface
149
+ */
150
+ public function getUsername(): string
151
+ {
152
+ return (string) $this->email;
153
+ }
154
+
155
+ /**
156
+ * @see UserInterface
157
+ */
158
+ public function getRoles(): array
159
+ {
160
+ $roles = $this->roles;
161
+ // guarantee every user at least has ROLE_USER
162
+ $roles[] = 'ROLE_USER';
163
+
164
+ return array_unique($roles);
165
+ }
166
+
167
+ public function setRoles(array $roles): self
168
+ {
169
+ $this->roles = $roles;
170
+
171
+ return $this;
172
+ }
173
+
174
+ /**
175
+ * @see UserInterface
176
+ */
177
+ public function getPassword(): string
178
+ {
179
+ return (string) $this->password;
180
+ }
181
+
182
+ public function setPassword(string $password): self
183
+ {
184
+ $this->password = $password;
185
+
186
+ return $this;
187
+ }
188
+
189
+ /**
190
+ * @see UserInterface
191
+ */
192
+ public function getSalt()
193
+ {
194
+ // not needed when using the "bcrypt" algorithm in security.yaml
195
+ }
196
+
197
+ /**
198
+ * @see UserInterface
199
+ */
200
+ public function eraseCredentials()
201
+ {
202
+ // If you store any temporary, sensitive data on the user, clear it here
203
+ // $this->plainPassword = null;
204
+ }
205
+ }
206
+
207
+
208
+ **Step 2. ** The User Repository::
209
+
210
+ // src/Repository/UserRepository.php
211
+ namespace App\Repository;
212
+
213
+ use App\Entity\User;
214
+ use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
215
+ use Doctrine\Common\Persistence\ManagerRegistry;
216
+
217
+ /**
218
+ * @method User|null find($id, $lockMode = null, $lockVersion = null)
219
+ * @method User|null findOneBy(array $criteria, array $orderBy = null)
220
+ * @method User[] findAll()
221
+ * @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
222
+ */
223
+ class UserRepository extends ServiceEntityRepository
224
+ {
225
+ public function __construct(ManagerRegistry $registry)
226
+ {
227
+ parent::__construct($registry, User::class);
228
+ }
229
+
230
+ // /**
231
+ // * @return User[] Returns an array of User objects
232
+ // */
233
+ /*
234
+ public function findByExampleField($value)
235
+ {
236
+ return $this->createQueryBuilder('u')
237
+ ->andWhere('u.exampleField = :val')
238
+ ->setParameter('val', $value)
239
+ ->orderBy('u.id', 'ASC')
240
+ ->setMaxResults(10)
241
+ ->getQuery()
242
+ ->getResult()
243
+ ;
244
+ }
245
+ */
246
+
247
+ /*
248
+ public function findOneBySomeField($value): ?User
249
+ {
250
+ return $this->createQueryBuilder('u')
251
+ ->andWhere('u.exampleField = :val')
252
+ ->setParameter('val', $value)
253
+ ->getQuery()
254
+ ->getOneOrNullResult()
255
+ ;
256
+ }
257
+ */
258
+ }
259
+
260
+
90
261
2b) The "User Provider"
91
262
-----------------------
92
263
@@ -96,7 +267,22 @@ optional features, like :doc:`remember me </security/remember_me>` and
96
267
:doc: `impersonation </security/impersonating_user >`.
97
268
98
269
Fortunately, the ``make:user `` command already configured one for you in your
99
- ``security.yaml `` file under the ``providers `` key.
270
+ ``security.yaml `` file under the ``providers `` key:
271
+
272
+ .. configuration-block ::
273
+
274
+ .. code-block :: yaml
275
+
276
+ # config/packages/security.yaml
277
+ security :
278
+ # ...
279
+
280
+ providers :
281
+ # used to reload user from session & other features (e.g. switch_user)
282
+ app_user_provider :
283
+ entity :
284
+ class : App\Entity\User
285
+ property : email
100
286
101
287
If your ``User `` class is an entity, you don't need to do anything else. But if
102
288
your class is *not * an entity, then ``make:user `` will also have generated a
0 commit comments