Skip to content

Commit c41f88c

Browse files
ThomasLandauerwouterj
authored andcommitted
Added missing security.yaml config
1 parent d4bb765 commit c41f88c

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
@@ -49,6 +49,63 @@ Don't forget to generate and execute the migration:
4949
$ php bin/console make:migration
5050
$ php bin/console doctrine:migrations:migrate
5151
52+
Next, configure your "user provider" to use this new ``apiToken`` property:
53+
54+
.. configuration-block::
55+
56+
.. code-block:: yaml
57+
58+
# config/packages/security.yaml
59+
security:
60+
# ...
61+
62+
providers:
63+
your_db_provider:
64+
entity:
65+
class: App\Entity\User
66+
property: apiToken
67+
68+
# ...
69+
70+
.. code-block:: xml
71+
72+
<!-- config/packages/security.xml -->
73+
<?xml version="1.0" encoding="UTF-8"?>
74+
<srv:container xmlns="http://symfony.com/schema/dic/security"
75+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
76+
xmlns:srv="http://symfony.com/schema/dic/services"
77+
xsi:schemaLocation="http://symfony.com/schema/dic/services
78+
https://symfony.com/schema/dic/services/services-1.0.xsd">
79+
80+
<config>
81+
<!-- ... -->
82+
83+
<provider name="your_db_provider">
84+
<entity class="App\Entity\User" property="apiToken"/>
85+
</provider>
86+
87+
<!-- ... -->
88+
</config>
89+
</srv:container>
90+
91+
.. code-block:: php
92+
93+
// config/packages/security.php
94+
$container->loadFromExtension('security', [
95+
// ...
96+
97+
'providers' => [
98+
'your_db_provider' => [
99+
'entity' => [
100+
'class' => 'App\Entity\User',
101+
'property' => 'apiToken',
102+
],
103+
],
104+
],
105+
106+
// ...
107+
]);
108+
52109
Step 2) Create the Authenticator Class
53110
--------------------------------------
54111

@@ -108,10 +165,10 @@ This requires you to implement several methods::
108165
return null;
109166
}
110167

111-
// if a User is returned, checkCredentials() is called
112-
return $this->em->getRepository(User::class)
113-
->findOneBy(['apiToken' => $credentials])
114-
;
168+
// The "username" in this case is the apiToken, see the key `property`
169+
// of `your_db_provider` in `security.yaml`.
170+
// If this returns a user, checkCredentials() is called next:
171+
return $userProvider->loadUserByUsername($apiToken);
115172
}
116173

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

0 commit comments

Comments
 (0)