Skip to content

Commit 47ee44b

Browse files
St0iKweaverryan
authored andcommitted
[make:auth] Added logout support and help for logged in user
1 parent d262c2c commit 47ee44b

File tree

52 files changed

+866
-133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+866
-133
lines changed

.travis.yml

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
11
language: php
2-
3-
dist: trusty
42
sudo: false
5-
6-
git:
7-
depth: 1
8-
9-
php:
10-
- 7.0
11-
- 7.1
12-
- 7.2
13-
- 7.3
14-
153
cache:
164
directories:
175
- $HOME/.composer/cache/files
18-
- .phpunit
19-
- vendor
6+
- $HOME/symfony-bridge/.phpunit
207

218
env:
229
global:
23-
- SYMFONY_PHPUNIT_DIR=.phpunit
10+
- PHPUNIT_FLAGS="-v"
11+
- SYMFONY_PHPUNIT_DIR="$HOME/symfony-bridge/.phpunit"
12+
13+
matrix:
14+
fast_finish: true
15+
include:
16+
- php: 7.1
17+
env: SYMFONY_PHPUNIT_VERSION=7.4
18+
- php: 7.1
19+
env: deps=high SYMFONY_PHPUNIT_VERSION=7.4
20+
- php: 7.1
21+
env: MAKER_TEST_VERSION=stable-dev
22+
- php: 7.1
23+
env: MAKER_TEST_VERSION=dev
24+
- php: 7.2
25+
env: deps=low
26+
- php: 7.3
27+
28+
allow_failures:
29+
# Dev-master is allowed to fail.
30+
- env: STABILITY="dev"
2431

2532
before_install:
26-
- phpenv config-rm xdebug.ini || echo "xdebug not available"
33+
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
34+
- if ! [ -z "$STABILITY" ]; then composer config minimum-stability ${STABILITY}; fi;
35+
- if ! [ -v "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi;
36+
- composer install
2737

2838
install:
29-
- composer update --no-suggest
39+
- |
40+
# Install symfony/flex
41+
if [[ $deps = low ]]; then
42+
export SYMFONY_REQUIRE='>=2.8'
43+
fi
44+
composer global require --no-progress --no-scripts --no-plugins symfony/flex dev-master
3045
- ./vendor/bin/simple-phpunit install
3146

3247
script:
33-
- ./vendor/bin/simple-phpunit
48+
- composer validate --strict --no-check-lock
49+
- ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS
3450
- ./vendor/bin/php-cs-fixer fix --dry-run --diff

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
"symfony/http-kernel": "^3.4|^4.0"
2525
},
2626
"require-dev": {
27-
"allocine/twigcs": "^3.0",
2827
"doctrine/doctrine-bundle": "^1.8",
2928
"doctrine/orm": "^2.3",
3029
"friendsofphp/php-cs-fixer": "^2.8",
30+
"friendsoftwig/twigcs": "^3.0",
31+
"symfony/http-client": "^4.3",
3132
"symfony/phpunit-bridge": "^3.4|^4.0",
3233
"symfony/process": "^3.4|^4.0",
34+
"symfony/security-core": "^3.4|^4.0",
3335
"symfony/yaml": "^3.4|^4.0"
3436
},
3537
"config": {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* Removes injected parameter arguments if they don't exist in this app.
19+
*
20+
* @author Ryan Weaver <ryan@symfonycasts.com>
21+
*/
22+
class RemoveMissingParametersPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container)
25+
{
26+
if (!$container->hasParameter('twig.default_path')) {
27+
$container->getDefinition('maker.file_manager')
28+
->replaceArgument(3, null);
29+
}
30+
}
31+
}

src/DependencyInjection/MakerExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public function load(array $configs, ContainerBuilder $container)
3939

4040
$rootNamespace = trim($config['root_namespace'], '\\');
4141

42+
$autoloaderFinderDefinition = $container->getDefinition('maker.autoloader_finder');
43+
$autoloaderFinderDefinition->replaceArgument(0, $rootNamespace);
44+
4245
$makeCommandDefinition = $container->getDefinition('maker.generator');
4346
$makeCommandDefinition->replaceArgument(1, $rootNamespace);
4447

src/FileManager.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ class FileManager
2929
private $rootDirectory;
3030
/** @var SymfonyStyle */
3131
private $io;
32+
private $twigDefaultPath;
3233

33-
public function __construct(Filesystem $fs, AutoloaderUtil $autoloaderUtil, string $rootDirectory)
34+
public function __construct(Filesystem $fs, AutoloaderUtil $autoloaderUtil, string $rootDirectory, string $twigDefaultPath = null)
3435
{
3536
// move FileManagerTest stuff
3637
// update EntityRegeneratorTest to mock the autoloader
3738
$this->fs = $fs;
3839
$this->autoloaderUtil = $autoloaderUtil;
3940
$this->rootDirectory = rtrim($this->realPath($this->normalizeSlashes($rootDirectory)), '/');
41+
$this->twigDefaultPath = $twigDefaultPath ? rtrim($this->relativizePath($twigDefaultPath), '/') : null;
4042
}
4143

4244
public function setIO(SymfonyStyle $io)
@@ -118,7 +120,7 @@ public function getFileContents(string $path): string
118120
return file_get_contents($this->absolutizePath($path));
119121
}
120122

121-
public function createFinder(string $in)
123+
public function createFinder(string $in): Finder
122124
{
123125
$finder = new Finder();
124126
$finder->in($this->absolutizePath($in));
@@ -174,6 +176,15 @@ public function getRootDirectory(): string
174176
return $this->rootDirectory;
175177
}
176178

179+
public function getPathForTemplate(string $filename): string
180+
{
181+
if (null === $this->twigDefaultPath) {
182+
throw new \RuntimeException('Cannot get path for template: is Twig installed?');
183+
}
184+
185+
return $this->twigDefaultPath.'/'.$filename;
186+
}
187+
177188
/**
178189
* Resolve '../' in paths (like real_path), but for non-existent files.
179190
*

src/Generator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,20 @@ public function generateController(string $controllerClassName, string $controll
225225
]
226226
);
227227
}
228+
229+
/**
230+
* Generate a template file.
231+
*
232+
* @param string $targetPath
233+
* @param string $templateName
234+
* @param array $variables
235+
*/
236+
public function generateTemplate(string $targetPath, string $templateName, array $variables)
237+
{
238+
$this->generateFile(
239+
$this->fileManager->getPathForTemplate($targetPath),
240+
$templateName,
241+
$variables
242+
);
243+
}
228244
}

src/GeneratorTwigHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getEntityFieldPrintCode($entity, $field): string
6767

6868
public function getHeadPrintCode($title): string
6969
{
70-
if ($this->fileManager->fileExists('templates/base.html.twig')) {
70+
if ($this->fileManager->fileExists($this->fileManager->getPathForTemplate('base.html.twig'))) {
7171
return <<<TWIG
7272
{% extends 'base.html.twig' %}
7373

src/Maker/MakeAuthenticator.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ function ($answer) {
164164
'username-field',
165165
$interactiveSecurityHelper->guessUserNameField($io, $userClass, $securityData['security']['providers'])
166166
);
167+
168+
$command->addArgument('logout-setup', InputArgument::REQUIRED);
169+
$input->setArgument(
170+
'logout-setup',
171+
$io->confirm(
172+
'Do you want to generate a \'/logout\' URL?',
173+
true
174+
)
175+
);
167176
}
168177
}
169178

@@ -187,15 +196,20 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
187196
$this->fileManager->getFileContents($path = 'config/packages/security.yaml'),
188197
$input->getOption('firewall-name'),
189198
$input->getOption('entry-point'),
190-
$input->getArgument('authenticator-class')
199+
$input->getArgument('authenticator-class'),
200+
$input->hasArgument('logout-setup') ? $input->getArgument('logout-setup') : false
191201
);
192202
$generator->dumpFile($path, $newYaml);
193203
$securityYamlUpdated = true;
194204
} catch (YamlManipulationFailedException $e) {
195205
}
196206

197207
if (self::AUTH_TYPE_FORM_LOGIN === $input->getArgument('authenticator-type')) {
198-
$this->generateFormLoginFiles($input->getArgument('controller-class'), $input->getArgument('username-field'));
208+
$this->generateFormLoginFiles(
209+
$input->getArgument('controller-class'),
210+
$input->getArgument('username-field'),
211+
$input->getArgument('logout-setup')
212+
);
199213
}
200214

201215
$generator->writeChanges();
@@ -245,7 +259,7 @@ private function generateAuthenticatorClass(array $securityData, string $authent
245259
);
246260
}
247261

248-
private function generateFormLoginFiles(string $controllerClass, string $userNameField)
262+
private function generateFormLoginFiles(string $controllerClass, string $userNameField, bool $logoutSetup)
249263
{
250264
$controllerClassNameDetails = $this->generator->createClassNameDetails(
251265
$controllerClass,
@@ -273,17 +287,21 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
273287

274288
$securityControllerBuilder = new SecurityControllerBuilder();
275289
$securityControllerBuilder->addLoginMethod($manipulator);
290+
if ($logoutSetup) {
291+
$securityControllerBuilder->addLogoutMethod($manipulator);
292+
}
276293

277294
$this->generator->dumpFile($controllerPath, $manipulator->getSourceCode());
278295

279296
// create login form template
280-
$this->generator->generateFile(
281-
'templates/security/login.html.twig',
297+
$this->generator->generateTemplate(
298+
'security/login.html.twig',
282299
'authenticator/login_form.tpl.php',
283300
[
284301
'username_field' => $userNameField,
285302
'username_is_email' => false !== stripos($userNameField, 'email'),
286303
'username_label' => ucfirst(Str::asHumanWords($userNameField)),
304+
'logout_setup' => $logoutSetup,
287305
]
288306
);
289307
}
@@ -314,7 +332,7 @@ private function generateNextMessage(bool $securityYamlUpdated, string $authenti
314332
$nextTexts[] = sprintf('- Check the user\'s password in <info>%s::checkCredentials()</info>.', $authenticatorClass);
315333
}
316334

317-
$nextTexts[] = '- Review & adapt the login template: <info>templates/security/login.html.twig</info>.';
335+
$nextTexts[] = '- Review & adapt the login template: <info>'.$this->fileManager->getPathForTemplate('security/login.html.twig').'</info>.';
318336
}
319337

320338
return $nextTexts;

src/Maker/MakeController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
6666
);
6767

6868
if ($this->isTwigInstalled() && !$noTemplate) {
69-
$generator->generateFile(
70-
'templates/'.$templateName,
69+
$generator->generateTemplate(
70+
$templateName,
7171
'controller/twig_template.tpl.php',
7272
[
7373
'controller_path' => $controllerPath,

src/Maker/MakeCrud.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
194194
];
195195

196196
foreach ($templates as $template => $variables) {
197-
$generator->generateFile(
198-
'templates/'.$templatesPath.'/'.$template.'.html.twig',
197+
$generator->generateTemplate(
198+
$templatesPath.'/'.$template.'.html.twig',
199199
'crud/templates/'.$template.'.tpl.php',
200200
$variables
201201
);

src/Maker/MakeEntity.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,7 @@ private function askRelationDetails(ConsoleStyle $io, string $generatedEntityCla
477477
} elseif (class_exists($answeredEntityClass)) {
478478
$targetEntityClass = $answeredEntityClass;
479479
} else {
480-
$io->error(sprintf('Unknown class "%s"', $targetEntityClass));
481-
$targetEntityClass = null;
482-
480+
$io->error(sprintf('Unknown class "%s"', $answeredEntityClass));
483481
continue;
484482
}
485483
}

src/Maker/MakeFunctionalTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Input\InputArgument;
2121
use Symfony\Component\Console\Input\InputInterface;
2222
use Symfony\Component\CssSelector\CssSelectorConverter;
23+
use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait;
2324

2425
/**
2526
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
@@ -52,7 +53,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
5253
$generator->generateClass(
5354
$testClassNameDetails->getFullName(),
5455
'test/Functional.tpl.php',
55-
[]
56+
[
57+
'web_assertions_are_available' => trait_exists(WebTestAssertionsTrait::class),
58+
]
5659
);
5760

5861
$generator->writeChanges();

src/Maker/MakeRegistrationForm.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Console\Command\Command;
3333
use Symfony\Component\Console\Input\InputInterface;
3434
use Symfony\Component\Form\AbstractType;
35+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
3536
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
3637
use Symfony\Component\Routing\RouterInterface;
3738
use Symfony\Component\Validator\Validation;
@@ -217,8 +218,8 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
217218
);
218219

219220
// 3) Generate the template
220-
$generator->generateFile(
221-
'templates/registration/register.html.twig',
221+
$generator->generateTemplate(
222+
'registration/register.html.twig',
222223
'registration/twig_template.tpl.php',
223224
[
224225
'username_field' => $usernameField,
@@ -311,6 +312,17 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator
311312
'max' => 4096,
312313
]),
313314
],
315+
EOF
316+
],
317+
'agreeTerms' => [
318+
'type' => CheckboxType::class,
319+
'options_code' => <<<EOF
320+
'mapped' => false,
321+
'constraints' => [
322+
new IsTrue([
323+
'message' => 'You should agree to our terms.',
324+
]),
325+
],
314326
EOF
315327
],
316328
];
@@ -322,6 +334,7 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator
322334
[
323335
'Symfony\Component\Validator\Constraints\NotBlank',
324336
'Symfony\Component\Validator\Constraints\Length',
337+
'Symfony\Component\Validator\Constraints\IsTrue',
325338
]
326339
);
327340

0 commit comments

Comments
 (0)