Skip to content

Commit b06ebe5

Browse files
authored
Merge pull request #546 from nikophil/fix-security-config-updater
Fix SecurityConfigUpdater
2 parents 8b57384 + 8cb64d4 commit b06ebe5

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

src/Security/SecurityConfigUpdater.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,7 @@ private function normalizeSecurityYamlFile()
103103

104104
private function updateProviders(UserClassConfiguration $userConfig, string $userClass)
105105
{
106-
if ($this->isSingleInMemoryProviderConfigured()) {
107-
// empty the providers if the generic "in_memory" is the only one
108-
$newData = $this->manipulator->getData();
109-
$newData['security']['providers'] = [];
110-
$this->manipulator->setData($newData);
111-
}
106+
$this->removeMemoryProviderIfIsSingleConfigured();
112107

113108
$newData = $this->manipulator->getData();
114109
$newData['security']['providers']['__'] = $this->manipulator->createCommentLine(
@@ -149,6 +144,27 @@ private function updateEncoders(UserClassConfiguration $userConfig, string $user
149144
$this->manipulator->setData($newData);
150145
}
151146

147+
private function removeMemoryProviderIfIsSingleConfigured()
148+
{
149+
if (!$this->isSingleInMemoryProviderConfigured()) {
150+
return;
151+
}
152+
153+
$newData = $this->manipulator->getData();
154+
155+
$memoryProviderName = array_keys($newData['security']['providers'])[0];
156+
157+
$newData['security']['providers'] = [];
158+
159+
foreach ($newData['security']['firewalls'] as &$firewall) {
160+
if (($firewall['provider'] ?? null) === $memoryProviderName) {
161+
$firewall['provider'] = 'app_user_provider';
162+
}
163+
}
164+
165+
$this->manipulator->setData($newData);
166+
}
167+
152168
private function isSingleInMemoryProviderConfigured(): bool
153169
{
154170
if (!isset($this->manipulator->getData()['security']['providers'])) {

tests/Security/SecurityConfigUpdaterTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public function getUserClassTests()
7272
'empty_source_model_email_with_password.yaml',
7373
'empty_security.yaml',
7474
];
75+
76+
yield 'simple_security_with_single_memory_provider_configured' => [
77+
new UserClassConfiguration(true, 'email', true),
78+
'simple_security_with_single_memory_provider_configured.yaml',
79+
'simple_security_with_single_memory_provider_configured.yaml',
80+
];
7581
}
7682

7783
/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
security:
2+
encoders:
3+
App\Entity\User:
4+
algorithm: {BCRYPT_OR_AUTO}
5+
6+
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
7+
providers:
8+
# used to reload user from session & other features (e.g. switch_user)
9+
app_user_provider:
10+
entity:
11+
class: App\Entity\User
12+
property: email
13+
14+
firewalls:
15+
dev: ~
16+
main:
17+
anonymous: true
18+
provider: app_user_provider
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
provider: in_memory

0 commit comments

Comments
 (0)