Skip to content

Commit b4ed9e7

Browse files
committed
set authenticator on security data
1 parent ab12c72 commit b4ed9e7

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/Maker/Security/MakeCustomAuthenticator.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313

1414
use Symfony\Bundle\MakerBundle\ConsoleStyle;
1515
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16+
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
17+
use Symfony\Bundle\MakerBundle\FileManager;
1618
use Symfony\Bundle\MakerBundle\Generator;
1719
use Symfony\Bundle\MakerBundle\InputConfiguration;
1820
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
21+
use Symfony\Bundle\MakerBundle\Maker\Common\InstallDependencyTrait;
1922
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
2023
use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator;
24+
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
2125
use Symfony\Bundle\MakerBundle\Validator;
2226
use Symfony\Component\Console\Command\Command;
2327
use Symfony\Component\Console\Input\InputInterface;
@@ -39,9 +43,14 @@
3943
*/
4044
final class MakeCustomAuthenticator extends AbstractMaker
4145
{
46+
use InstallDependencyTrait;
47+
48+
private const SECURITY_CONFIG_PATH = 'config/packages/security.yaml';
49+
4250
private ClassNameDetails $authenticatorClassName;
4351

4452
public function __construct(
53+
private FileManager $fileManager,
4554
private Generator $generator,
4655
) {
4756
}
@@ -65,6 +74,16 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
6574

6675
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
6776
{
77+
$this->installDependencyIfNeeded(
78+
io: $io,
79+
expectedClassToExist: AbstractAuthenticator::class,
80+
composerPackage: 'symfony/security-bundle'
81+
);
82+
83+
if (!$this->fileManager->fileExists(self::SECURITY_CONFIG_PATH)) {
84+
throw new RuntimeCommandException(sprintf('The file "%s" does not exist. PHP & XML configuration formats are currently not supported.', self::SECURITY_CONFIG_PATH));
85+
}
86+
6887
$name = $io->ask(
6988
question: 'What is the class name of the authenticator (e.g. <fg=yellow>CustomAuthenticator</>)',
7089
validator: static function (mixed $answer) {
@@ -81,6 +100,17 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
81100

82101
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
83102
{
103+
// Configure security to use custom authenticator
104+
$securityConfig = ($ysm = new YamlSourceManipulator(
105+
$this->fileManager->getFileContents(self::SECURITY_CONFIG_PATH)
106+
))->getData();
107+
108+
$securityConfig['security']['firewalls']['main']['custom_authenticators'] = [$this->authenticatorClassName->getFullName()];
109+
110+
$ysm->setData($securityConfig);
111+
$generator->dumpFile(self::SECURITY_CONFIG_PATH, $ysm->getContents());
112+
113+
// Generate the new authenticator
84114
$useStatements = new UseStatementGenerator([
85115
Request::class,
86116
Response::class,
@@ -110,6 +140,5 @@ className: $this->authenticatorClassName->getFullName(),
110140

111141
public function configureDependencies(DependencyBuilder $dependencies): void
112142
{
113-
// TODO: Implement configureDependencies() method.
114143
}
115144
}

src/Resources/config/makers.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
</service>
160160

161161
<service id="maker.maker.make_custom_authenticator" class="Symfony\Bundle\MakerBundle\Maker\Security\MakeCustomAuthenticator">
162+
<argument type="service" id="maker.file_manager" />
162163
<argument type="service" id="maker.generator" />
163164
<tag name="maker.command" />
164165
</service>

tests/Maker/Security/MakeCustomAuthenticatorTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ public function getTestDetails(): \Generator
3838

3939
$this->assertFileEquals($fixturePath.'/FixtureAuthenticator.php', $runner->getPath('src/Security/FixtureAuthenticator.php'));
4040

41-
// $securityConfig = $runner->readYaml('config/packages/security.yaml');
42-
//
43-
// $this->assertSame('app_login', $securityConfig['security']['firewalls']['main']['form_login']['login_path']);
44-
// $this->assertSame('app_login', $securityConfig['security']['firewalls']['main']['form_login']['check_path']);
45-
// $this->assertTrue($securityConfig['security']['firewalls']['main']['form_login']['enable_csrf']);
46-
// $this->assertSame('app_logout', $securityConfig['security']['firewalls']['main']['logout']['path']);
41+
$securityConfig = $runner->readYaml('config/packages/security.yaml');
42+
43+
self::assertArrayHasKey('custom_authenticators', $mainFirewall = $securityConfig['security']['firewalls']['main']);
44+
self::assertSame(['App\Security\FixtureAuthenticator'], $mainFirewall['custom_authenticators']);
4745
}),
4846
];
4947
}

0 commit comments

Comments
 (0)