-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
679 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Account deletion | ||
================ | ||
|
||
The NucleosUserBundle has built-in support for deleting the user account. | ||
|
||
Enable feature | ||
-------------- | ||
|
||
The feature is disabled by default. You can enable it by using the following configuration: | ||
|
||
.. code-block:: yaml | ||
# config/packages/nucleos_user.yaml | ||
nucleos_user: | ||
# ... | ||
deletion: | ||
enable: true | ||
Add the routing config: | ||
|
||
.. code-block:: yaml | ||
# config/routes/nucleos_user.yaml | ||
nucleos_user_deletion: | ||
resource: "@NucleosUserBundle/Resources/config/routing/deletion.xml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the NucleosUserBundle package. | ||
* | ||
* (c) Christian Gripp <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\UserBundle\Action; | ||
|
||
use Nucleos\UserBundle\Event\AccountDeletionEvent; | ||
use Nucleos\UserBundle\Event\AccountDeletionResponseEvent; | ||
use Nucleos\UserBundle\Event\GetResponseAccountDeletionEvent; | ||
use Nucleos\UserBundle\Form\Model\AccountDeletion; | ||
use Nucleos\UserBundle\Form\Type\AccountDeletionFormType; | ||
use Nucleos\UserBundle\Model\UserInterface; | ||
use Nucleos\UserBundle\Model\UserManagerInterface; | ||
use Nucleos\UserBundle\NucleosUserEvents; | ||
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
use Symfony\Component\Form\FormFactoryInterface; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Twig\Environment; | ||
|
||
final class AccountDeletionAction | ||
{ | ||
/** | ||
* @var Environment | ||
*/ | ||
private $twig; | ||
|
||
/** | ||
* @var RouterInterface | ||
*/ | ||
private $router; | ||
|
||
/** | ||
* @var UserManagerInterface | ||
*/ | ||
private $userManager; | ||
|
||
/** | ||
* @var TokenStorageInterface | ||
*/ | ||
private $tokenStorage; | ||
|
||
/** | ||
* @var FormFactoryInterface | ||
*/ | ||
private $formFactory; | ||
|
||
/** | ||
* @var EventDispatcherInterface | ||
*/ | ||
private $eventDispatcher; | ||
|
||
public function __construct( | ||
Environment $twig, | ||
RouterInterface $router, | ||
UserManagerInterface $userManager, | ||
TokenStorageInterface $tokenStorage, | ||
FormFactoryInterface $formFactory, | ||
EventDispatcherInterface $eventDispatcher | ||
) { | ||
$this->twig = $twig; | ||
$this->router = $router; | ||
$this->userManager = $userManager; | ||
$this->formFactory = $formFactory; | ||
$this->eventDispatcher = $eventDispatcher; | ||
$this->tokenStorage = $tokenStorage; | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
$user = $this->getUser(); | ||
|
||
if (!$user instanceof UserInterface) { | ||
throw new AccessDeniedException('Access Denied.'); | ||
} | ||
|
||
$event = new GetResponseAccountDeletionEvent($user, $request); | ||
$this->eventDispatcher->dispatch($event, NucleosUserEvents::ACCOUNT_DELETION_INITIALIZE); | ||
|
||
if (null !== $response = $event->getResponse()) { | ||
return $response; | ||
} | ||
|
||
$form = $this->formFactory | ||
->create(AccountDeletionFormType::class, new AccountDeletion(), [ | ||
'action' => $this->router->generate('nucleos_user_delete_account'), | ||
]) | ||
->add('delete', SubmitType::class, [ | ||
'label' => 'deletion.submit', | ||
]) | ||
; | ||
$form->handleRequest($request); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
return $this->processDeletion($user, $request); | ||
} | ||
|
||
return new Response($this->twig->render('@NucleosUser/Account/deletion.html.twig', [ | ||
'form' => $form->createView(), | ||
])); | ||
} | ||
|
||
private function getUser(): ?UserInterface | ||
{ | ||
$token = $this->tokenStorage->getToken(); | ||
|
||
if (null === $token) { | ||
return null; | ||
} | ||
|
||
$user = $token->getUser(); | ||
|
||
if ($user instanceof UserInterface) { | ||
return $user; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function processDeletion(UserInterface $user, Request $request): Response | ||
{ | ||
$event = new AccountDeletionEvent($user, $request); | ||
$this->eventDispatcher->dispatch($event, NucleosUserEvents::ACCOUNT_DELETION); | ||
|
||
$this->userManager->deleteUser($user); | ||
|
||
$event = new AccountDeletionResponseEvent($user, $request, new RedirectResponse($this->router->generate('nucleos_user_security_logout'))); | ||
$this->eventDispatcher->dispatch($event, NucleosUserEvents::ACCOUNT_DELETION_SUCCESS); | ||
|
||
return $event->getResponse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the NucleosUserBundle package. | ||
* | ||
* (c) Christian Gripp <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\UserBundle\Event; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
class AccountDeletionEvent extends Event | ||
{ | ||
/** | ||
* @var Request | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var UserInterface | ||
*/ | ||
private $user; | ||
|
||
public function __construct(UserInterface $user, Request $request) | ||
{ | ||
$this->user = $user; | ||
$this->request = $request; | ||
} | ||
|
||
public function getRequest(): Request | ||
{ | ||
return $this->request; | ||
} | ||
|
||
public function getUser(): UserInterface | ||
{ | ||
return $this->user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the NucleosUserBundle package. | ||
* | ||
* (c) Christian Gripp <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\UserBundle\Event; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
final class AccountDeletionResponseEvent extends AccountDeletionEvent | ||
{ | ||
/** | ||
* @var Response | ||
*/ | ||
private $response; | ||
|
||
public function __construct(UserInterface $user, Request $request, Response $response) | ||
{ | ||
parent::__construct($user, $request); | ||
|
||
$this->response = $response; | ||
} | ||
|
||
public function getResponse(): Response | ||
{ | ||
return $this->response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the NucleosUserBundle package. | ||
* | ||
* (c) Christian Gripp <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nucleos\UserBundle\Event; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
final class GetResponseAccountDeletionEvent extends AccountDeletionEvent | ||
{ | ||
/** | ||
* @var Response|null | ||
*/ | ||
private $response; | ||
|
||
public function setResponse(Response $response): void | ||
{ | ||
$this->response = $response; | ||
} | ||
|
||
public function getResponse(): ?Response | ||
{ | ||
return $this->response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.