Skip to content

Commit ae06b6c

Browse files
committed
Security: add example code which Maker Bundle generated
See #11265
1 parent b9b32de commit ae06b6c

File tree

1 file changed

+186
-1
lines changed

1 file changed

+186
-1
lines changed

security.rst

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,176 @@ to add more fields. Also, make sure to make and run a migration for the new enti
8484
$ php bin/console make:migration
8585
$ php bin/console doctrine:migrations:migrate
8686
87+
The Maker Bundler generated the following: 1) the User entity and 2) the User Repository
88+
89+
**Step 1.** The User entity::
90+
91+
// src/Entity/User.php
92+
namespace App\Entity;
93+
94+
use Doctrine\ORM\Mapping as ORM;
95+
use Symfony\Component\Security\Core\User\UserInterface;
96+
97+
/**
98+
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
99+
*/
100+
class User implements UserInterface
101+
{
102+
/**
103+
* @ORM\Id()
104+
* @ORM\GeneratedValue()
105+
* @ORM\Column(type="integer")
106+
*/
107+
private $id;
108+
109+
/**
110+
* @ORM\Column(type="string", length=180, unique=true)
111+
*/
112+
private $email;
113+
114+
/**
115+
* @ORM\Column(type="json")
116+
*/
117+
private $roles = [];
118+
119+
/**
120+
* @var string The hashed password
121+
* @ORM\Column(type="string")
122+
*/
123+
private $password;
124+
125+
public function getId(): ?int
126+
{
127+
return $this->id;
128+
}
129+
130+
public function getEmail(): ?string
131+
{
132+
return $this->email;
133+
}
134+
135+
public function setEmail(string $email): self
136+
{
137+
$this->email = $email;
138+
139+
return $this;
140+
}
141+
142+
/**
143+
* A visual identifier that represents this user.
144+
*
145+
* @see UserInterface
146+
*/
147+
public function getUsername(): string
148+
{
149+
return (string) $this->email;
150+
}
151+
152+
/**
153+
* @see UserInterface
154+
*/
155+
public function getRoles(): array
156+
{
157+
$roles = $this->roles;
158+
// guarantee every user at least has ROLE_USER
159+
$roles[] = 'ROLE_USER';
160+
161+
return array_unique($roles);
162+
}
163+
164+
public function setRoles(array $roles): self
165+
{
166+
$this->roles = $roles;
167+
168+
return $this;
169+
}
170+
171+
/**
172+
* @see UserInterface
173+
*/
174+
public function getPassword(): string
175+
{
176+
return (string) $this->password;
177+
}
178+
179+
public function setPassword(string $password): self
180+
{
181+
$this->password = $password;
182+
183+
return $this;
184+
}
185+
186+
/**
187+
* @see UserInterface
188+
*/
189+
public function getSalt()
190+
{
191+
// not needed when using the "bcrypt" algorithm in security.yaml
192+
}
193+
194+
/**
195+
* @see UserInterface
196+
*/
197+
public function eraseCredentials()
198+
{
199+
// If you store any temporary, sensitive data on the user, clear it here
200+
// $this->plainPassword = null;
201+
}
202+
}
203+
204+
205+
**Step 2.** The User Repository::
206+
207+
// src/Repository/UserRepository.php
208+
namespace App\Repository;
209+
210+
use App\Entity\User;
211+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
212+
use Doctrine\Common\Persistence\ManagerRegistry;
213+
214+
/**
215+
* @method User|null find($id, $lockMode = null, $lockVersion = null)
216+
* @method User|null findOneBy(array $criteria, array $orderBy = null)
217+
* @method User[] findAll()
218+
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
219+
*/
220+
class UserRepository extends ServiceEntityRepository
221+
{
222+
public function __construct(ManagerRegistry $registry)
223+
{
224+
parent::__construct($registry, User::class);
225+
}
226+
227+
// /**
228+
// * @return User[] Returns an array of User objects
229+
// */
230+
/*
231+
public function findByExampleField($value)
232+
{
233+
return $this->createQueryBuilder('u')
234+
->andWhere('u.exampleField = :val')
235+
->setParameter('val', $value)
236+
->orderBy('u.id', 'ASC')
237+
->setMaxResults(10)
238+
->getQuery()
239+
->getResult()
240+
;
241+
}
242+
*/
243+
244+
/*
245+
public function findOneBySomeField($value): ?User
246+
{
247+
return $this->createQueryBuilder('u')
248+
->andWhere('u.exampleField = :val')
249+
->setParameter('val', $value)
250+
->getQuery()
251+
->getOneOrNullResult()
252+
;
253+
}
254+
*/
255+
}
256+
87257
.. _security-user-providers:
88258
.. _where-do-users-come-from-user-providers:
89259

@@ -96,7 +266,22 @@ optional features, like :doc:`remember me </security/remember_me>` and
96266
:doc:`impersonation </security/impersonating_user>`.
97267

98268
Fortunately, the ``make:user`` command already configured one for you in your
99-
``security.yaml`` file under the ``providers`` key.
269+
``security.yaml`` file under the ``providers`` key:
270+
271+
.. configuration-block::
272+
273+
.. code-block:: yaml
274+
275+
# config/packages/security.yaml
276+
security:
277+
# ...
278+
279+
providers:
280+
# used to reload user from session & other features (e.g. switch_user)
281+
app_user_provider:
282+
entity:
283+
class: App\Entity\User
284+
property: email
100285
101286
If your ``User`` class is an entity, you don't need to do anything else. But if
102287
your class is *not* an entity, then ``make:user`` will also have generated a

0 commit comments

Comments
 (0)