From 0be672f8442db9ec9a34711a64f44ce722ab6890 Mon Sep 17 00:00:00 2001 From: Robert Swoboda <53216935+Gengar-i@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:02:20 +0100 Subject: [PATCH 1/8] IBX-7044: Added dynamic route to settings submit form --- .../Controller/UserSettingsController.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/bundle/Controller/UserSettingsController.php b/src/bundle/Controller/UserSettingsController.php index 3f82378..1b41568 100644 --- a/src/bundle/Controller/UserSettingsController.php +++ b/src/bundle/Controller/UserSettingsController.php @@ -90,9 +90,9 @@ public function updateAction(Request $request, UpdateView $view) $data = new UserSettingUpdateData($userSettingGroup->getIdentifier(), $values); $form = $this->formFactory->updateUserSetting($userSettingGroup->getIdentifier(), $data); $form->handleRequest($request); - + if ($form->isSubmitted()) { - $result = $this->submitHandler->handle($form, function (UserSettingUpdateData $data) use ($form) { + $result = $this->submitHandler->handle($form, function (UserSettingUpdateData $data) use ($form, $request) { foreach ($data->getValues() as $identifier => $value) { $this->userSettingService->setUserSetting($identifier, (string)$value['value']); } @@ -106,12 +106,25 @@ public function updateAction(Request $request, UpdateView $view) if ($form->getClickedButton() instanceof Button && $form->getClickedButton()->getName() === UserSettingUpdateType::BTN_UPDATE_AND_EDIT - ) { + ) { return $this->redirectToRoute('ibexa.user_settings.update', [ 'identifier' => $data->getIdentifier(), ]); } + if ($request->query->has('route')) { + $route = $request->query->get('route'); + $contentId = $request->query->get('content_id'); + $versionNo = $request->query->get('version'); + $language = $request->query->get('language'); + + return $this->redirectToRoute($route, [ + 'contentId' => $contentId, + 'versionNo' => $versionNo, + 'language' => $language, + ]); + } + return new RedirectResponse($this->generateUrl('ibexa.user_settings.list')); }); From 31c7226e201a48d2138e696d18d3451d2f9dc2f0 Mon Sep 17 00:00:00 2001 From: Robert Swoboda <53216935+Gengar-i@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:05:15 +0100 Subject: [PATCH 2/8] Fix-cs --- src/bundle/Controller/UserSettingsController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bundle/Controller/UserSettingsController.php b/src/bundle/Controller/UserSettingsController.php index 1b41568..a9041d0 100644 --- a/src/bundle/Controller/UserSettingsController.php +++ b/src/bundle/Controller/UserSettingsController.php @@ -90,7 +90,7 @@ public function updateAction(Request $request, UpdateView $view) $data = new UserSettingUpdateData($userSettingGroup->getIdentifier(), $values); $form = $this->formFactory->updateUserSetting($userSettingGroup->getIdentifier(), $data); $form->handleRequest($request); - + if ($form->isSubmitted()) { $result = $this->submitHandler->handle($form, function (UserSettingUpdateData $data) use ($form, $request) { foreach ($data->getValues() as $identifier => $value) { @@ -106,7 +106,7 @@ public function updateAction(Request $request, UpdateView $view) if ($form->getClickedButton() instanceof Button && $form->getClickedButton()->getName() === UserSettingUpdateType::BTN_UPDATE_AND_EDIT - ) { + ) { return $this->redirectToRoute('ibexa.user_settings.update', [ 'identifier' => $data->getIdentifier(), ]); From 6f36183265c5c2d09d231c1de572d4ad9028ff66 Mon Sep 17 00:00:00 2001 From: Robert Swoboda <53216935+Gengar-i@users.noreply.github.com> Date: Tue, 20 Feb 2024 14:13:03 +0100 Subject: [PATCH 3/8] Fixed variables passed by url --- src/bundle/Controller/UserSettingsController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bundle/Controller/UserSettingsController.php b/src/bundle/Controller/UserSettingsController.php index a9041d0..50e1e1b 100644 --- a/src/bundle/Controller/UserSettingsController.php +++ b/src/bundle/Controller/UserSettingsController.php @@ -114,8 +114,8 @@ public function updateAction(Request $request, UpdateView $view) if ($request->query->has('route')) { $route = $request->query->get('route'); - $contentId = $request->query->get('content_id'); - $versionNo = $request->query->get('version'); + $contentId = $request->query->get('contentId'); + $versionNo = $request->query->get('versionNo'); $language = $request->query->get('language'); return $this->redirectToRoute($route, [ From 5f2826756df1c551c624b9d04e8dfbfe6daccfb9 Mon Sep 17 00:00:00 2001 From: Robert Swoboda <53216935+Gengar-i@users.noreply.github.com> Date: Mon, 18 Mar 2024 11:00:29 +0100 Subject: [PATCH 4/8] Save logic with route implemented --- src/bundle/Controller/UserSettingsController.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/bundle/Controller/UserSettingsController.php b/src/bundle/Controller/UserSettingsController.php index 50e1e1b..cd1efcc 100644 --- a/src/bundle/Controller/UserSettingsController.php +++ b/src/bundle/Controller/UserSettingsController.php @@ -104,20 +104,24 @@ public function updateAction(Request $request, UpdateView $view) 'ibexa_user_settings' ); + $route = $request->query->get('route') ?? null; + $contentId = $request->query->get('contentId') ?? null; + $versionNo = $request->query->get('versionNo') ?? null; + $language = $request->query->get('language') ?? null; + if ($form->getClickedButton() instanceof Button && $form->getClickedButton()->getName() === UserSettingUpdateType::BTN_UPDATE_AND_EDIT ) { return $this->redirectToRoute('ibexa.user_settings.update', [ 'identifier' => $data->getIdentifier(), + 'route' => $route, + 'contentId' => $contentId, + 'versionNo' => $versionNo, + 'language' => $language, ]); } - if ($request->query->has('route')) { - $route = $request->query->get('route'); - $contentId = $request->query->get('contentId'); - $versionNo = $request->query->get('versionNo'); - $language = $request->query->get('language'); - + if ($route !== null) { return $this->redirectToRoute($route, [ 'contentId' => $contentId, 'versionNo' => $versionNo, From 5cdcef0244d790792c7130bc633774ec0becd850 Mon Sep 17 00:00:00 2001 From: Robert Swoboda <53216935+Gengar-i@users.noreply.github.com> Date: Fri, 5 Apr 2024 15:53:49 +0200 Subject: [PATCH 5/8] Added routeParameters for custom route --- src/bundle/Controller/UserSettingsController.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/bundle/Controller/UserSettingsController.php b/src/bundle/Controller/UserSettingsController.php index cd1efcc..244ad55 100644 --- a/src/bundle/Controller/UserSettingsController.php +++ b/src/bundle/Controller/UserSettingsController.php @@ -105,9 +105,7 @@ public function updateAction(Request $request, UpdateView $view) ); $route = $request->query->get('route') ?? null; - $contentId = $request->query->get('contentId') ?? null; - $versionNo = $request->query->get('versionNo') ?? null; - $language = $request->query->get('language') ?? null; + $routeParameters = $request->query->get('routeParameters') ?? []; if ($form->getClickedButton() instanceof Button && $form->getClickedButton()->getName() === UserSettingUpdateType::BTN_UPDATE_AND_EDIT @@ -115,18 +113,12 @@ public function updateAction(Request $request, UpdateView $view) return $this->redirectToRoute('ibexa.user_settings.update', [ 'identifier' => $data->getIdentifier(), 'route' => $route, - 'contentId' => $contentId, - 'versionNo' => $versionNo, - 'language' => $language, + 'routeParameters' => $routeParameters, ]); } - + if ($route !== null) { - return $this->redirectToRoute($route, [ - 'contentId' => $contentId, - 'versionNo' => $versionNo, - 'language' => $language, - ]); + return $this->redirectToRoute($route, $routeParameters); } return new RedirectResponse($this->generateUrl('ibexa.user_settings.list')); From c595335268afd1545732738df2abc34e7c7dc8f4 Mon Sep 17 00:00:00 2001 From: Robert Swoboda <53216935+Gengar-i@users.noreply.github.com> Date: Tue, 9 Apr 2024 10:08:45 +0200 Subject: [PATCH 6/8] Fixed whitespace blank line --- src/bundle/Controller/UserSettingsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bundle/Controller/UserSettingsController.php b/src/bundle/Controller/UserSettingsController.php index 244ad55..221486e 100644 --- a/src/bundle/Controller/UserSettingsController.php +++ b/src/bundle/Controller/UserSettingsController.php @@ -116,7 +116,7 @@ public function updateAction(Request $request, UpdateView $view) 'routeParameters' => $routeParameters, ]); } - + if ($route !== null) { return $this->redirectToRoute($route, $routeParameters); } From bd24d6e2ddbb9ad45b130ea6d83e42b55a6a6c6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Tue, 9 Apr 2024 11:41:20 +0200 Subject: [PATCH 7/8] Added route check --- .../Controller/UserSettingsController.php | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/src/bundle/Controller/UserSettingsController.php b/src/bundle/Controller/UserSettingsController.php index 221486e..a5d2225 100644 --- a/src/bundle/Controller/UserSettingsController.php +++ b/src/bundle/Controller/UserSettingsController.php @@ -19,13 +19,20 @@ use Ibexa\User\View\UserSettings\ListView; use Ibexa\User\View\UserSettings\UpdateView; use JMS\TranslationBundle\Annotation\Desc; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use Symfony\Component\Form\Button; -use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Exception\ExceptionInterface as RouteExceptionInterface; +use Symfony\Component\Routing\RouterInterface; -class UserSettingsController extends Controller +class UserSettingsController extends Controller implements LoggerAwareInterface { + use LoggerAwareTrait; + /** @var \Ibexa\User\Form\Factory\FormFactory */ private $formFactory; @@ -43,13 +50,17 @@ class UserSettingsController extends Controller private PermissionResolver $permissionResolver; + private RouterInterface $router; + public function __construct( FormFactory $formFactory, SubmitHandler $submitHandler, UserSettingService $userSettingService, ValueDefinitionRegistry $valueDefinitionRegistry, ActionResultHandler $actionResultHandler, - PermissionResolver $permissionResolver + PermissionResolver $permissionResolver, + RouterInterface $router, + LoggerInterface $logger = null ) { $this->formFactory = $formFactory; $this->submitHandler = $submitHandler; @@ -57,6 +68,8 @@ public function __construct( $this->valueDefinitionRegistry = $valueDefinitionRegistry; $this->actionResultHandler = $actionResultHandler; $this->permissionResolver = $permissionResolver; + $this->router = $router; + $this->logger = $logger ?? new NullLogger(); } /** @@ -104,8 +117,12 @@ public function updateAction(Request $request, UpdateView $view) 'ibexa_user_settings' ); - $route = $request->query->get('route') ?? null; - $routeParameters = $request->query->get('routeParameters') ?? []; + $route = $request->query->get('route'); + $routeParameters = $request->query->all('routeParameters'); + if (!$this->routeExists($route, $routeParameters)) { + $route = null; + $routeParameters = []; + } if ($form->getClickedButton() instanceof Button && $form->getClickedButton()->getName() === UserSettingUpdateType::BTN_UPDATE_AND_EDIT @@ -121,7 +138,7 @@ public function updateAction(Request $request, UpdateView $view) return $this->redirectToRoute($route, $routeParameters); } - return new RedirectResponse($this->generateUrl('ibexa.user_settings.list')); + return $this->redirectToRoute('ibexa.user_settings.list'); }); if ($result instanceof Response) { @@ -136,6 +153,24 @@ public function updateAction(Request $request, UpdateView $view) return $view; } + + private function routeExists($route, array $routeParameters): bool + { + try { + $this->router->generate($route, $routeParameters); + + return true; + } catch (RouteExceptionInterface $e) { + $this->logger->warning( + sprintf('Invalid route in query. %s.', $e->getMessage()), + [ + 'exception' => $e, + ], + ); + } + + return false; + } } class_alias(UserSettingsController::class, 'EzSystems\EzPlatformUserBundle\Controller\UserSettingsController'); From 66f7268bffe3eebb9407aac38eddf4ee9463f1d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Tue, 9 Apr 2024 12:16:42 +0200 Subject: [PATCH 8/8] Added route check --- src/bundle/Controller/UserSettingsController.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/bundle/Controller/UserSettingsController.php b/src/bundle/Controller/UserSettingsController.php index a5d2225..26b0cc4 100644 --- a/src/bundle/Controller/UserSettingsController.php +++ b/src/bundle/Controller/UserSettingsController.php @@ -27,7 +27,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Exception\ExceptionInterface as RouteExceptionInterface; -use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class UserSettingsController extends Controller implements LoggerAwareInterface { @@ -50,7 +50,7 @@ class UserSettingsController extends Controller implements LoggerAwareInterface private PermissionResolver $permissionResolver; - private RouterInterface $router; + private UrlGeneratorInterface $urlGenerator; public function __construct( FormFactory $formFactory, @@ -59,7 +59,7 @@ public function __construct( ValueDefinitionRegistry $valueDefinitionRegistry, ActionResultHandler $actionResultHandler, PermissionResolver $permissionResolver, - RouterInterface $router, + UrlGeneratorInterface $urlGenerator, LoggerInterface $logger = null ) { $this->formFactory = $formFactory; @@ -68,7 +68,7 @@ public function __construct( $this->valueDefinitionRegistry = $valueDefinitionRegistry; $this->actionResultHandler = $actionResultHandler; $this->permissionResolver = $permissionResolver; - $this->router = $router; + $this->urlGenerator = $urlGenerator; $this->logger = $logger ?? new NullLogger(); } @@ -154,10 +154,13 @@ public function updateAction(Request $request, UpdateView $view) return $view; } - private function routeExists($route, array $routeParameters): bool + /** + * @param array $routeParameters + */ + private function routeExists(string $route, array $routeParameters): bool { try { - $this->router->generate($route, $routeParameters); + $this->urlGenerator->generate($route, $routeParameters); return true; } catch (RouteExceptionInterface $e) {