Skip to content

Commit 33a06fd

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: Added missing security.yaml config
2 parents d50cd86 + 9953a11 commit 33a06fd

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

security/guard_authentication.rst

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,63 @@ Don't forget to generate and run the migration:
5454
$ php bin/console make:migration
5555
$ php bin/console doctrine:migrations:migrate
5656
57+
Next, configure your "user provider" to use this new ``apiToken`` property:
58+
59+
.. configuration-block::
60+
61+
.. code-block:: yaml
62+
63+
# config/packages/security.yaml
64+
security:
65+
# ...
66+
67+
providers:
68+
your_db_provider:
69+
entity:
70+
class: App\Entity\User
71+
property: apiToken
72+
73+
# ...
74+
75+
.. code-block:: xml
76+
77+
<!-- config/packages/security.xml -->
78+
<?xml version="1.0" encoding="UTF-8"?>
79+
<srv:container xmlns="http://symfony.com/schema/dic/security"
80+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
81+
xmlns:srv="http://symfony.com/schema/dic/services"
82+
xsi:schemaLocation="http://symfony.com/schema/dic/services
83+
https://symfony.com/schema/dic/services/services-1.0.xsd">
84+
85+
<config>
86+
<!-- ... -->
87+
88+
<provider name="your_db_provider">
89+
<entity class="App\Entity\User" property="apiToken"/>
90+
</provider>
91+
92+
<!-- ... -->
93+
</config>
94+
</srv:container>
95+
96+
.. code-block:: php
97+
98+
// config/packages/security.php
99+
$container->loadFromExtension('security', [
100+
// ...
101+
102+
'providers' => [
103+
'your_db_provider' => [
104+
'entity' => [
105+
'class' => 'App\Entity\User',
106+
'property' => 'apiToken',
107+
],
108+
],
109+
],
110+
111+
// ...
112+
]);
113+
57114
Step 2) Create the Authenticator Class
58115
--------------------------------------
59116

@@ -113,10 +170,10 @@ This requires you to implement several methods::
113170
return null;
114171
}
115172

116-
// if a User is returned, checkCredentials() is called
117-
return $this->em->getRepository(User::class)
118-
->findOneBy(['apiToken' => $credentials])
119-
;
173+
// The "username" in this case is the apiToken, see the key `property`
174+
// of `your_db_provider` in `security.yaml`.
175+
// If this returns a user, checkCredentials() is called next:
176+
return $userProvider->loadUserByUsername($apiToken);
120177
}
121178

122179
public function checkCredentials($credentials, UserInterface $user)

0 commit comments

Comments
 (0)