Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/Maker/MakeResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class MakeResetPassword extends AbstractMaker
private $fileManager;
private $doctrineHelper;
private $entityClassGenerator;
private $generateApi = false;

public function __construct(FileManager $fileManager, DoctrineHelper $doctrineHelper, EntityClassGenerator $entityClassGenerator)
{
Expand Down Expand Up @@ -172,6 +173,11 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
[Validator::class, 'notBlank']
)
);

$this->generateApi = $io->confirm('Do you want to implement API based Password Resets?', false);

//@TODO Check if API Platform is installed, if true - continue, if false - alert user composer require api-platform
// @TODO May make more sense to ask this at the top and fail if yes and api platform is not installed.
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
Expand All @@ -182,6 +188,26 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'Entity\\'
);

$userDoctrineDetails = $this->doctrineHelper->createDoctrineDetails($userClassNameDetails->getFullName());

$userRepoVars = [
'repository_full_class_name' => 'Doctrine\ORM\EntityManagerInterface',
'repository_class_name' => 'EntityManagerInterface',
'repository_property_var' => 'manager',
'repository_var' => '$manager',
];

if (null !== $userDoctrineDetails && null !== ($userRepository = $userDoctrineDetails->getRepositoryClass())) {
$userRepoClassDetails = $generator->createClassNameDetails('\\'.$userRepository, 'Repository\\', 'Repository');

$userRepoVars = [
'repository_full_class_name' => $userRepoClassDetails->getFullName(),
'repository_class_name' => $userRepoClassDetails->getShortName(),
'repository_property_var' => lcfirst($userRepoClassDetails->getShortName()),
'repository_var' => sprintf('$%s', lcfirst($userRepoClassDetails->getShortName())),
];
}

$controllerClassNameDetails = $generator->createClassNameDetails(
'ResetPasswordController',
'Controller\\'
Expand Down Expand Up @@ -266,6 +292,46 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'resetPassword/twig_reset.tpl.php'
);

if ($this->generateApi) {
$dtoClassNameDetails = $generator->createClassNameDetails(
'ResetPasswordInput',
'Dto\\'
);

$generator->generateClass(
$dtoClassNameDetails->getFullName(),
'resetPassword/ResetPasswordInput.tpl.php'
);

$dataTransformerClassNameDetails = $generator->createClassNameDetails(
'ResetPasswordInputDataTransformer',
'DataTransformer\\'
);

$generator->generateClass(
$dataTransformerClassNameDetails->getFullName(),
'resetPassword/ResetPasswordInputDataTransformer.tpl.php',
);

$dataPersisterClassNameDetails = $generator->createClassNameDetails(
'ResetPasswordDataPersister',
'DataPersister\\'
);

$generator->generateClass(
$dataPersisterClassNameDetails->getFullName(),
'resetPassword/ResetPasswordDataPersister.tpl.php',
array_merge([
'user_full_class_name' => $userClassNameDetails->getFullName(),
'user_class_name' => $userClassNameDetails->getShortName(),
],
$userRepoVars
)
);

// @TODO - Add API DocBlocks to Entity
}

$generator->writeChanges();

$this->writeSuccessMessage($io);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?= "<?php\n" ?>

namespace <?= $namespace ?>;

use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
use App\Dto\ResetPasswordInput;
use <?= $user_full_class_name ?>;
use App\Message\SendResetPasswordMessage;
use <?= $repository_full_class_name ?>;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;

class <?= $class_name ?> implements ContextAwareDataPersisterInterface
{
private <?= $use_typed_properties ? sprintf('%s %s', $repository_class_name, $repository_var) : $repository_var ?>;
private <?= $use_typed_properties ? 'ResetPasswordHelperInterface ' : null ?>$resetPasswordHelper;
private <?= $use_typed_properties ? 'MessageBusInterface ' : null ?>$messageBus;
private <?= $use_typed_properties ? 'UserPasswordEncoderInterface ' : null ?>$userPasswordEncoder;

public function __construct(<?= $repository_class_name ?> <?= $repository_var ?>, ResetPasswordHelperInterface $resetPasswordHelper, MessageBusInterface $messageBus, UserPasswordEncoderInterface $userPasswordEncoder)
{
$this-><?= $repository_property_var ?> = <?= $repository_var?>;
$this->resetPasswordHelper = $resetPasswordHelper;
$this->messageBus = $messageBus;
$this->userPasswordEncoder = $userPasswordEncoder;
}

public function supports($data, array $context = []): bool
{
if (!$data instanceof ResetPasswordInput) {
return false;
}

if (isset($context['collection_operation_name']) && 'post' === $context['collection_operation_name']) {
return true;
}

if (isset($context['item_operation_name']) && 'put' === $context['item_operation_name']) {
return true;
}

return false;
}

/**
* @param ResetPasswordInput $data
*/
public function persist($data, array $context = []): void
{
if (isset($context['collection_operation_name']) && 'post' === $context['collection_operation_name']) {
$this->generateRequest($data->email);

return;
}

if (isset($context['item_operation_name']) && 'put' === $context['item_operation_name']) {
if (!$context['previous_data'] instanceof <?= $user_class_name ?>) {
return;
}

$this->changePassword($context['previous_data'], $data->plainTextPassword);
}
}

public function remove($data, array $context = []): void
{
throw new \RuntimeException('Operation not supported.');
}

private function generateRequest(string $email): void
{
<?php if ('$manager' === $repository_var): ?>
$repository = $this-><?= $repository_property_var ?>->getRepository(<?= $user_class_name ?>::class);
$user = $repository->findOneBy(['email' => $data->email]);
<?php else: ?>
$user = $this-><?= $repository_property_var ?>->findOneBy(['email' => $data->email]);
<?php endif; ?>

if (!$user instanceof <?= $user_class_name?>) {
return;
}

$token = $this->resetPasswordHelper->generateResetToken($user);

$this->messageBus->dispatch(new SendResetPasswordMessage($user->getEmail(), $token));
}

private function changePassword(<?= $user_class_name?> $previousUser, string $plainTextPassword): void
{
$userId = $previousUser->getId();

<?php if ('$manager' === $repository_var): ?>
$repository = $this-><?= $repository_property_var ?>->getRepository(<?= $user_class_name ?>::class);
$user = $repository->find($userId);
<?php else: ?>
$user = $this-><?= $repository_property_var ?>->find($userId);
<?php endif; ?>

if (null === $user) {
return;
}

$encoded = $this->userPasswordEncoder->encodePassword($user, $plainTextPassword);

<?php if ('$manager' === $repository_var): ?>
$user->setPassword($encoded);

$repository->flush();
<?php else: ?>
$this-><?= $repository_property_var ?>->upgradePassword($user, $encoded);
<?php endif; ?>
}
}
28 changes: 28 additions & 0 deletions src/Resources/skeleton/resetPassword/ResetPasswordInput.tpl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?= "<?php\n" ?>

namespace <?= $namespace ?>;

use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

class <?= $class_name."\n" ?>
{
/**
* @Assert\NotBlank(groups={"postValidation"})
* @Assert\Email(groups={"postValidation"})
* @Groups({"reset-password:post"})
*/
public <?= $use_typed_properties ? '?string ' : null ?>$email = null;

/**
* @Assert\NotBlank(groups={"putValidation"})
* @Groups({"reset-password:put"})
*/
public <?= $use_typed_properties ? '?string ' : null ?>$token = null;

/**
* @Assert\NotBlank(groups={"putValidation"})
* @Groups({"reset-password:put"})
*/
public <?= $use_typed_properties ? '?string ' : null ?>$plainTextPassword = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?= "<?php\n" ?>

namespace <?= $namespace ?>;

use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
use App\Dto\ResetPasswordInput;
use App\Entity\ResetPasswordRequest;

class <?= $class_name ?> implements DataTransformerInterface
{
public function transform($object, string $to, array $context = []): object
{
return $object;
}

public function supportsTransformation($data, string $to, array $context = []): bool
{
if ($data instanceof ResetPasswordRequest) {
return false;
}

return ResetPasswordRequest::class === $to && ($context['input']['class'] ?? null) === ResetPasswordInput::class;
}
}
Loading