Skip to content

Commit 2fb5b40

Browse files
committed
add authenticator in security.yaml the simplest way
1 parent 70bbe65 commit 2fb5b40

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

src/Maker/MakeAuthenticator.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1515
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16+
use Symfony\Bundle\MakerBundle\FileManager;
1617
use Symfony\Bundle\MakerBundle\Generator;
1718
use Symfony\Bundle\MakerBundle\InputConfiguration;
19+
use Symfony\Bundle\MakerBundle\Security\SecurityConfigUpdater;
20+
use Symfony\Bundle\MakerBundle\Util\YamlManipulationFailedException;
21+
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
1822
use Symfony\Component\Console\Command\Command;
1923
use Symfony\Component\Console\Input\InputArgument;
2024
use Symfony\Bundle\SecurityBundle\SecurityBundle;
@@ -25,6 +29,16 @@
2529
*/
2630
final class MakeAuthenticator extends AbstractMaker
2731
{
32+
private $fileManager;
33+
34+
private $configUpdater;
35+
36+
public function __construct(FileManager $fileManager, SecurityConfigUpdater $configUpdater)
37+
{
38+
$this->fileManager = $fileManager;
39+
$this->configUpdater = $configUpdater;
40+
}
41+
2842
public static function getCommandName(): string
2943
{
3044
return 'make:auth';
@@ -51,6 +65,19 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
5165
'authenticator/Empty.tpl.php',
5266
[]
5367
);
68+
69+
$path = 'config/packages/security.yaml';
70+
if ($this->fileManager->fileExists($path)) {
71+
try {
72+
$newYaml = $this->configUpdater->updateForAuthenticator(
73+
$this->fileManager->getFileContents($path),
74+
$classNameDetails->getFullName()
75+
);
76+
$generator->dumpFile($path, $newYaml);
77+
} catch (YamlManipulationFailedException $e) {
78+
}
79+
}
80+
5481
$generator->writeChanges();
5582

5683
$this->writeSuccessMessage($io);

src/Resources/config/makers.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<defaults public="false" />
99

1010
<service id="maker.maker.make_authenticator" class="Symfony\Bundle\MakerBundle\Maker\MakeAuthenticator">
11+
<argument type="service" id="maker.file_manager" />
12+
<argument type="service" id="maker.security_config_updater" />
1113
<tag name="maker.command" />
1214
</service>
1315

src/Security/SecurityConfigUpdater.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ public function updateForUserClass(string $yamlSource, UserClassConfiguration $u
3434
{
3535
$this->manipulator = new YamlSourceManipulator($yamlSource);
3636

37-
// normalize the top level, just in case
38-
if (!isset($this->manipulator->getData()['security'])) {
39-
$newData = $this->manipulator->getData();
40-
$newData['security'] = [];
41-
$this->manipulator->setData($newData);
42-
}
37+
$this->normalizeSecurityYamlFile();
4338

4439
$this->updateProviders($userConfig, $userClass);
4540

@@ -53,6 +48,57 @@ public function updateForUserClass(string $yamlSource, UserClassConfiguration $u
5348
return $contents;
5449
}
5550

51+
public function updateForAuthenticator(string $yamlSource, string $authenticatorFQCN)
52+
{
53+
$this->manipulator = new YamlSourceManipulator($yamlSource);
54+
55+
$this->normalizeSecurityYamlFile();
56+
57+
$newData = $this->manipulator->getData();
58+
59+
if (!isset($newData['security']['firewalls'])) {
60+
$newData['security']['firewalls'] = [];
61+
}
62+
63+
$firewalls = array_filter(
64+
$newData['security']['firewalls'],
65+
function ($item) {
66+
return !isset($item['security']) || true === $item['security'];
67+
}
68+
);
69+
70+
if (!$firewalls) {
71+
$firewalls['main'] = ['anonymous' => true];
72+
}
73+
74+
$firewall = $firewalls['main'];
75+
if (!isset($firewall['guard'])) {
76+
$firewall['guard'] = [];
77+
}
78+
79+
if (!isset($firewall['guard']['authenticators'])) {
80+
$firewall['guard']['authenticators'] = [];
81+
}
82+
83+
$firewall['guard']['authenticators'][] = $authenticatorFQCN;
84+
85+
$newData['security']['firewalls']['main'] = $firewall;
86+
$this->manipulator->setData($newData);
87+
88+
$contents = $this->manipulator->getContents();
89+
90+
return $contents;
91+
}
92+
93+
private function normalizeSecurityYamlFile()
94+
{
95+
if (!isset($this->manipulator->getData()['security'])) {
96+
$newData = $this->manipulator->getData();
97+
$newData['security'] = [];
98+
$this->manipulator->setData($newData);
99+
}
100+
}
101+
56102
private function updateProviders(UserClassConfiguration $userConfig, string $userClass)
57103
{
58104
if ($this->isSingleInMemoryProviderConfigured()) {

0 commit comments

Comments
 (0)