-
-
Notifications
You must be signed in to change notification settings - Fork 424
create maker for symfony casts reset password bundle #567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,315 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\Maker; | ||
|
||
use Symfony\Bundle\MakerBundle\ConsoleStyle; | ||
use Symfony\Bundle\MakerBundle\DependencyBuilder; | ||
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; | ||
use Symfony\Bundle\MakerBundle\FileManager; | ||
use Symfony\Bundle\MakerBundle\Generator; | ||
use Symfony\Bundle\MakerBundle\InputConfiguration; | ||
use Symfony\Bundle\MakerBundle\Security\InteractiveSecurityHelper; | ||
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator; | ||
use Symfony\Bundle\MakerBundle\Validator; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Yaml\Yaml; | ||
use SymfonyCasts\Bundle\ResetPassword\SymfonyCastsResetPasswordBundle; | ||
|
||
/** | ||
* @author Romaric Drigon <[email protected]> | ||
* @author Jesse Rushlow <[email protected]> | ||
* @author Ryan Weaver <[email protected]> | ||
* | ||
* @internal | ||
* @final | ||
*/ | ||
class MakeResetPassword extends AbstractMaker | ||
{ | ||
private $fileManager; | ||
|
||
public function __construct(FileManager $fileManager) | ||
{ | ||
$this->fileManager = $fileManager; | ||
} | ||
|
||
public static function getCommandName(): string | ||
{ | ||
return 'make:reset-password'; | ||
} | ||
|
||
public function configureCommand(Command $command, InputConfiguration $inputConfig) | ||
{ | ||
$command | ||
->setDescription('Create controller, entity, and repositories for use with symfonycasts/reset-password-bundle.') | ||
; | ||
} | ||
|
||
public function configureDependencies(DependencyBuilder $dependencies) | ||
{ | ||
$dependencies->addClassDependency(SymfonyCastsResetPasswordBundle::class, 'symfonycasts/reset-password-bundle'); | ||
} | ||
|
||
public function interact(InputInterface $input, ConsoleStyle $io, Command $command) | ||
{ | ||
$io->title('Let\'s make a password reset feature!'); | ||
|
||
// initialize arguments & commands that are internal (i.e. meant only to be asked) | ||
$command | ||
->addArgument('from-email-address', InputArgument::REQUIRED) | ||
->addArgument('from-email-name', InputArgument::REQUIRED) | ||
->addArgument('controller-reset-success-redirect', InputArgument::REQUIRED) | ||
->addArgument('user-class') | ||
->addArgument('email-property-name') | ||
->addArgument('email-getter') | ||
->addArgument('password-setter') | ||
; | ||
|
||
$interactiveSecurityHelper = new InteractiveSecurityHelper(); | ||
|
||
if (!$this->fileManager->fileExists($path = 'config/packages/security.yaml')) { | ||
throw new RuntimeCommandException('The file "config/packages/security.yaml" does not exist. This command needs that file to accurately build the reset password form.'); | ||
} | ||
|
||
$manipulator = new YamlSourceManipulator($this->fileManager->getFileContents($path)); | ||
$securityData = $manipulator->getData(); | ||
$providersData = $securityData['security']['providers'] ?? []; | ||
|
||
$input->setArgument( | ||
'user-class', | ||
$userClass = $interactiveSecurityHelper->guessUserClass( | ||
$io, | ||
$providersData, | ||
'What is the User entity that should be used with the "forgotten password" feature? (e.g. <fg=yellow>App\\Entity\\User</>)' | ||
) | ||
); | ||
|
||
$input->setArgument( | ||
'email-property-name', | ||
$interactiveSecurityHelper->guessEmailField($io, $userClass) | ||
); | ||
$input->setArgument( | ||
'email-getter', | ||
$interactiveSecurityHelper->guessEmailGetter($io, $userClass) | ||
); | ||
$input->setArgument( | ||
'password-setter', | ||
$interactiveSecurityHelper->guessPasswordSetter($io, $userClass) | ||
); | ||
jrushlow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$io->text(sprintf('Implementing reset password for <info>%s</info>', $userClass)); | ||
|
||
$io->section('- ResetPasswordController -'); | ||
$io->text('A named route is used for redirecting after a successful reset. Even a route that does not exist yet can be used here.'); | ||
$input->setArgument('controller-reset-success-redirect', $io->ask( | ||
'What route should users be redirected to after their password has been successfully reset?', | ||
'app_home', | ||
[Validator::class, 'notBlank'] | ||
) | ||
); | ||
|
||
$io->section('- Email Templates -'); | ||
$emailText[] = 'These are used to generate the email code. Don\'t worry, you can change them in the code later!'; | ||
$io->text($emailText); | ||
|
||
$input->setArgument('from-email-address', $io->ask( | ||
'What email address will be used to send reset confirmations? e.g. [email protected]', | ||
null, | ||
[Validator::class, 'validateEmailAddress'] | ||
)); | ||
|
||
$input->setArgument('from-email-name', $io->ask( | ||
'What "name" should be associated with that email address? e.g. "Acme Mail Bot"', | ||
null, | ||
[Validator::class, 'notBlank'] | ||
) | ||
); | ||
} | ||
|
||
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) | ||
{ | ||
$userClass = $input->getArgument('user-class'); | ||
$userClassNameDetails = $generator->createClassNameDetails( | ||
'\\'.$userClass, | ||
'Entity\\' | ||
); | ||
|
||
$controllerClassNameDetails = $generator->createClassNameDetails( | ||
'ResetPasswordController', | ||
'Controller\\' | ||
); | ||
|
||
$requestClassNameDetails = $generator->createClassNameDetails( | ||
'ResetPasswordRequest', | ||
'Entity\\' | ||
); | ||
|
||
$repositoryClassNameDetails = $generator->createClassNameDetails( | ||
'ResetPasswordRequestRepository', | ||
'Repository\\' | ||
); | ||
|
||
$requestFormTypeClassNameDetails = $generator->createClassNameDetails( | ||
'ResetPasswordRequestFormType', | ||
'Form\\' | ||
); | ||
|
||
$changePasswordFormTypeClassNameDetails = $generator->createClassNameDetails( | ||
'ChangePasswordFormType', | ||
'Form\\' | ||
); | ||
|
||
$generator->generateController( | ||
$controllerClassNameDetails->getFullName(), | ||
'resetPassword/ResetPasswordController.tpl.php', | ||
[ | ||
'user_full_class_name' => $userClassNameDetails->getFullName(), | ||
'user_class_name' => $userClassNameDetails->getShortName(), | ||
'request_form_type_full_class_name' => $requestFormTypeClassNameDetails->getFullName(), | ||
'request_form_type_class_name' => $requestFormTypeClassNameDetails->getShortName(), | ||
'reset_form_type_full_class_name' => $changePasswordFormTypeClassNameDetails->getFullName(), | ||
'reset_form_type_class_name' => $changePasswordFormTypeClassNameDetails->getShortName(), | ||
'password_setter' => $input->getArgument('password-setter'), | ||
'success_redirect_route' => $input->getArgument('controller-reset-success-redirect'), | ||
'from_email' => $input->getArgument('from-email-address'), | ||
'from_email_name' => $input->getArgument('from-email-name'), | ||
'email_getter' => $input->getArgument('email-getter'), | ||
] | ||
); | ||
|
||
$generator->generateClass( | ||
$requestClassNameDetails->getFullName(), | ||
'resetPassword/ResetPasswordRequest.tpl.php', | ||
[ | ||
'repository_class_name' => $repositoryClassNameDetails->getFullName(), | ||
'user_full_class_name' => $userClassNameDetails->getFullName(), | ||
] | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's save this for later (cause I think it will take some work), but: It would be ideal if we could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. made a note on the board to implement in the near future.. |
||
|
||
$generator->generateClass( | ||
$repositoryClassNameDetails->getFullName(), | ||
'resetPassword/ResetPasswordRequestRepository.tpl.php', | ||
[ | ||
'request_class_full_name' => $requestClassNameDetails->getFullName(), | ||
'request_class_name' => $requestClassNameDetails->getShortName(), | ||
] | ||
); | ||
|
||
$this->setBundleConfig($io, $generator, $repositoryClassNameDetails->getFullName()); | ||
|
||
$generator->generateClass( | ||
$requestFormTypeClassNameDetails->getFullName(), | ||
'resetPassword/ResetPasswordRequestFormType.tpl.php', | ||
[ | ||
'email_field' => $input->getArgument('email-property-name'), | ||
] | ||
); | ||
|
||
$generator->generateClass( | ||
$changePasswordFormTypeClassNameDetails->getFullName(), | ||
'resetPassword/ChangePasswordFormType.tpl.php' | ||
); | ||
|
||
$generator->generateTemplate( | ||
'reset_password/check_email.html.twig', | ||
'resetPassword/twig_check_email.tpl.php' | ||
); | ||
|
||
$generator->generateTemplate( | ||
'reset_password/email.html.twig', | ||
'resetPassword/twig_email.tpl.php' | ||
); | ||
|
||
$generator->generateTemplate( | ||
'reset_password/request.html.twig', | ||
'resetPassword/twig_request.tpl.php', | ||
[ | ||
'email_field' => $input->getArgument('email-property-name'), | ||
] | ||
); | ||
|
||
$generator->generateTemplate( | ||
'reset_password/reset.html.twig', | ||
'resetPassword/twig_reset.tpl.php' | ||
); | ||
|
||
$generator->writeChanges(); | ||
|
||
$this->writeSuccessMessage($io); | ||
$this->successMessage($input, $io, $requestClassNameDetails->getFullName()); | ||
} | ||
|
||
private function setBundleConfig(ConsoleStyle $io, Generator $generator, string $repositoryClassFullName) | ||
{ | ||
$configFileExists = $this->fileManager->fileExists($path = 'config/packages/reset_password.yaml'); | ||
|
||
/* | ||
* reset_password.yaml does not exist, we assume flex was present when | ||
* the bundle was installed & a customized configuration is in use. | ||
* Remind the developer to set the repository class accordingly. | ||
*/ | ||
if (!$configFileExists) { | ||
$io->text(sprintf('We can\'t find %s. That\'s ok, you probably have a customized configuration.', $path)); | ||
$io->text('Just remember to set the <fg=yellow>request_password_repository</> in your configuration.'); | ||
$io->newLine(); | ||
|
||
return; | ||
} | ||
|
||
$manipulator = new YamlSourceManipulator($this->fileManager->getFileContents($path)); | ||
$data = $manipulator->getData(); | ||
|
||
$symfonyCastsKey = 'symfonycasts_reset_password'; | ||
|
||
/* | ||
* reset_password.yaml exists, and was probably created by flex; | ||
* Let's replace it with a "clean" file. | ||
*/ | ||
if (1 >= \count($data[$symfonyCastsKey])) { | ||
$yaml = [ | ||
$symfonyCastsKey => [ | ||
'request_password_repository' => $repositoryClassFullName, | ||
], | ||
]; | ||
|
||
$generator->dumpFile($path, Yaml::dump($yaml)); | ||
|
||
return; | ||
} | ||
|
||
/* | ||
* reset_password.yaml exists and appears to have been customized | ||
* before running make:reset-password. Let's just change the repository | ||
* value and preserve everything else. | ||
*/ | ||
$data[$symfonyCastsKey]['request_password_repository'] = $repositoryClassFullName; | ||
|
||
$manipulator->setData($data); | ||
|
||
$generator->dumpFile($path, $manipulator->getContents()); | ||
} | ||
|
||
private function successMessage(InputInterface $input, ConsoleStyle $io, string $requestClassName) | ||
{ | ||
$closing[] = 'Next:'; | ||
$closing[] = sprintf(' 1) Run <fg=yellow>"php bin/console make:migration"</> to generate a migration for the new <fg=yellow>"%s"</> entity.', $requestClassName); | ||
$closing[] = ' 2) Review forms in <fg=yellow>"src/Form"</> to customize validation and labels.'; | ||
$closing[] = ' 3) Review and customize the templates in <fg=yellow>`templates/reset_password`</>.'; | ||
$closing[] = ' 4) Make sure your <fg=yellow>MAILER_DSN</> env var has the correct settings.'; | ||
|
||
$io->text($closing); | ||
$io->newLine(); | ||
$io->text('Then open your browser, go to "/reset-password" and enjoy!'); | ||
$io->newLine(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?= "<?php\n" ?> | ||
|
||
namespace <?= $namespace ?>; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\PasswordType; | ||
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Validator\Constraints\Length; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
|
||
class <?= $class_name ?> extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('plainPassword', RepeatedType::class, [ | ||
'type' => PasswordType::class, | ||
'first_options' => [ | ||
'constraints' => [ | ||
new NotBlank([ | ||
'message' => 'Please enter a password', | ||
]), | ||
new Length([ | ||
'min' => 6, | ||
'minMessage' => 'Your password should be at least {{ limit }} characters', | ||
// max length allowed by Symfony for security reasons | ||
'max' => 4096, | ||
]), | ||
], | ||
'label' => 'New password', | ||
], | ||
'second_options' => [ | ||
'label' => 'Repeat Password', | ||
], | ||
'invalid_message' => 'The password fields must match.', | ||
// Instead of being set onto the object directly, | ||
// this is read and encoded in the controller | ||
'mapped' => false, | ||
]) | ||
; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([]); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.