fsi-open /
admin-security-bundle
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * (c) FSi sp. z o.o. <[email protected]> |
||
| 5 | * |
||
| 6 | * For the full copyright and license information, please view the LICENSE |
||
| 7 | * file that was distributed with this source code. |
||
| 8 | */ |
||
| 9 | |||
| 10 | declare(strict_types=1); |
||
| 11 | |||
| 12 | namespace FSi\Bundle\AdminSecurityBundle\Controller; |
||
| 13 | |||
| 14 | use FSi\Bundle\AdminBundle\Message\FlashMessages; |
||
| 15 | use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
||
| 16 | use FSi\Bundle\AdminSecurityBundle\Event\ChangePasswordEvent; |
||
| 17 | use FSi\Bundle\AdminSecurityBundle\Security\User\ChangeablePasswordInterface; |
||
| 18 | use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
||
| 19 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
| 20 | use Symfony\Component\Form\FormFactoryInterface; |
||
| 21 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||
| 22 | use Symfony\Component\HttpFoundation\Request; |
||
| 23 | use Symfony\Component\HttpFoundation\Response; |
||
| 24 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
| 25 | use Symfony\Component\Routing\RouterInterface; |
||
| 26 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
| 27 | |||
| 28 | class AdminController |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var TokenStorageInterface |
||
| 32 | */ |
||
| 33 | private $tokenStorage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var RouterInterface |
||
| 37 | */ |
||
| 38 | private $router; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var EventDispatcherInterface |
||
| 42 | */ |
||
| 43 | private $eventDispatcher; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var EngineInterface |
||
| 47 | */ |
||
| 48 | private $templating; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var FormFactoryInterface |
||
| 52 | */ |
||
| 53 | private $formFactory; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $changePasswordActionTemplate; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var FlashMessages |
||
| 62 | */ |
||
| 63 | private $flashMessages; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $changePasswordFormType; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $changePasswordFormValidationGroups; |
||
| 74 | |||
| 75 | public function __construct( |
||
| 76 | EngineInterface $templating, |
||
| 77 | FormFactoryInterface $formFactory, |
||
| 78 | TokenStorageInterface $tokenStorage, |
||
| 79 | RouterInterface $router, |
||
| 80 | EventDispatcherInterface $eventDispatcher, |
||
| 81 | FlashMessages $flashMessages, |
||
| 82 | string $changePasswordActionTemplate, |
||
| 83 | string $changePasswordFormType, |
||
| 84 | array $changePasswordFormValidationGroups |
||
| 85 | ) { |
||
| 86 | $this->templating = $templating; |
||
| 87 | $this->formFactory = $formFactory; |
||
| 88 | $this->tokenStorage = $tokenStorage; |
||
| 89 | $this->router = $router; |
||
| 90 | $this->eventDispatcher = $eventDispatcher; |
||
| 91 | $this->flashMessages = $flashMessages; |
||
| 92 | $this->changePasswordActionTemplate = $changePasswordActionTemplate; |
||
| 93 | $this->changePasswordFormType = $changePasswordFormType; |
||
| 94 | $this->changePasswordFormValidationGroups = $changePasswordFormValidationGroups; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function changePasswordAction(Request $request): Response |
||
| 98 | { |
||
| 99 | $user = $this->tokenStorage->getToken()->getUser(); |
||
| 100 | if (!($user instanceof ChangeablePasswordInterface)) { |
||
| 101 | throw new NotFoundHttpException(); |
||
| 102 | } |
||
| 103 | |||
| 104 | $form = $this->formFactory->create( |
||
| 105 | $this->changePasswordFormType, |
||
| 106 | $user, |
||
| 107 | ['validation_groups' => $this->changePasswordFormValidationGroups] |
||
| 108 | ); |
||
| 109 | |||
| 110 | if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) { |
||
| 111 | |||
| 112 | $this->eventDispatcher->dispatch( |
||
| 113 | AdminSecurityEvents::CHANGE_PASSWORD, |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 114 | new ChangePasswordEvent($user) |
||
| 115 | ); |
||
| 116 | |||
| 117 | $this->flashMessages->success( |
||
| 118 | 'admin.change_password_message.success', |
||
| 119 | [], |
||
| 120 | 'FSiAdminSecurity' |
||
| 121 | ); |
||
| 122 | |||
| 123 | return new RedirectResponse($this->router->generate('fsi_admin_security_user_login')); |
||
| 124 | } |
||
| 125 | |||
| 126 | return $this->templating->renderResponse( |
||
| 127 | $this->changePasswordActionTemplate, |
||
| 128 | ['form' => $form->createView()] |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | } |
||
| 132 |