Skip to content

Commit c6ab137

Browse files
Gary PEGEOTGaryPEGEOT
authored andcommitted
Add access_control rule for form login auth
1 parent 5387922 commit c6ab137

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

src/Maker/MakeAuthenticator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
226226
$entryPoint,
227227
$input->getArgument('authenticator-class'),
228228
$input->hasArgument('logout-setup') ? $input->getArgument('logout-setup') : false,
229-
$this->useSecurity52
229+
$this->useSecurity52,
230+
self::AUTH_TYPE_FORM_LOGIN === $input->getArgument('authenticator-type')
230231
);
231232
$generator->dumpFile($path, $newYaml);
232233
$securityYamlUpdated = true;

src/Security/SecurityConfigUpdater.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function updateForUserClass(string $yamlSource, UserClassConfiguration $u
5959
return $contents;
6060
}
6161

62-
public function updateForAuthenticator(string $yamlSource, string $firewallName, $chosenEntryPoint, string $authenticatorClass, bool $logoutSetup, bool $useSecurity52): string
62+
public function updateForAuthenticator(string $yamlSource, string $firewallName, $chosenEntryPoint, string $authenticatorClass, bool $logoutSetup, bool $useSecurity52, bool $addAccessControlRule = false): string
6363
{
6464
$this->manipulator = new YamlSourceManipulator($yamlSource);
6565

@@ -134,6 +134,20 @@ public function updateForAuthenticator(string $yamlSource, string $firewallName,
134134

135135
$newData['security']['firewalls'][$firewallName] = $firewall;
136136

137+
$accessControlRules = $newData['security']['access_control'] ?? [];
138+
139+
foreach ($accessControlRules as $rule) {
140+
if ('^/login$' === $rule['path']) {
141+
$addAccessControlRule = false;
142+
break;
143+
}
144+
}
145+
146+
if ($addAccessControlRule) {
147+
array_unshift($accessControlRules, ['path' => '^/login$', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY']);
148+
$newData['security']['access_control'] = $accessControlRules;
149+
}
150+
137151
$this->manipulator->setData($newData);
138152

139153
return $this->manipulator->getContents();

tests/Security/SecurityConfigUpdaterTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ public function getUserClassTests()
100100
/**
101101
* @dataProvider getAuthenticatorTests
102102
*/
103-
public function testUpdateForAuthenticator(string $firewallName, $entryPoint, string $expectedSourceFilename, string $startingSourceFilename, bool $logoutSetup, bool $useSecurity51)
103+
public function testUpdateForAuthenticator(string $firewallName, $entryPoint, string $expectedSourceFilename, string $startingSourceFilename, bool $logoutSetup, bool $useSecurity51, bool $addAccessControl = false)
104104
{
105105
$this->createLogger();
106106

107107
$updater = new SecurityConfigUpdater($this->ysmLogger);
108108
$source = file_get_contents(__DIR__.'/yaml_fixtures/source/'.$startingSourceFilename);
109-
$actualSource = $updater->updateForAuthenticator($source, $firewallName, $entryPoint, 'App\\Security\\AppCustomAuthenticator', $logoutSetup, $useSecurity51);
109+
$actualSource = $updater->updateForAuthenticator($source, $firewallName, $entryPoint, 'App\\Security\\AppCustomAuthenticator', $logoutSetup, $useSecurity51, $addAccessControl);
110110
$expectedSource = file_get_contents(__DIR__.'/yaml_fixtures/expected_authenticator/'.$expectedSourceFilename);
111111

112112
$this->assertSame($expectedSource, $actualSource);
@@ -185,6 +185,26 @@ public function getAuthenticatorTests()
185185
false,
186186
true,
187187
];
188+
189+
yield 'simple_security_with_access_control' => [
190+
'main',
191+
null,
192+
'simple_security_with_access_control.yaml',
193+
'simple_security_with_access_control.yaml',
194+
false,
195+
false,
196+
true
197+
];
198+
199+
yield 'simple_security_without_access_control' => [
200+
'main',
201+
null,
202+
'simple_security_with_added_access_control.yaml',
203+
'simple_security.yaml',
204+
false,
205+
false,
206+
true
207+
];
188208
}
189209

190210
private function createLogger(): void
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
security:
2+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
3+
providers:
4+
in_memory: { memory: ~ }
5+
6+
firewalls:
7+
dev: ~
8+
main:
9+
anonymous: true
10+
guard:
11+
authenticators:
12+
- App\Security\AppCustomAuthenticator
13+
14+
# Easy way to control access for large sections of your site
15+
# Note: Only the *first* access control that matches will be used
16+
access_control:
17+
- { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
security:
2+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
3+
providers:
4+
in_memory: { memory: ~ }
5+
6+
firewalls:
7+
dev: ~
8+
main:
9+
anonymous: true
10+
guard:
11+
authenticators:
12+
- App\Security\AppCustomAuthenticator
13+
access_control:
14+
-
15+
path: ^/login$
16+
roles: IS_AUTHENTICATED_ANONYMOUSLY
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
security:
2+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
3+
providers:
4+
in_memory: { memory: ~ }
5+
6+
firewalls:
7+
dev: ~
8+
main: ~
9+
10+
# Easy way to control access for large sections of your site
11+
# Note: Only the *first* access control that matches will be used
12+
access_control:
13+
- { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }

0 commit comments

Comments
 (0)