Skip to content

Commit

Permalink
bug #5853 Priority of the getDashboardDto routeName fixed. (inalbilal)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.x branch.

Discussion
----------

Priority of the getDashboardDto routeName fixed.

In our application, when we log in using the "/customer/admin" URL, the login process is successful. However, there is an unexpected behavior with the "AdminUrlGenerator" object while generating URLs. Instead of producing the "customer_admin" value, it produces the "publisher_admin" value. This issue is occurring because the "publisher_admin" route definition takes precedence over the "customer_admin" route definition.

Looking at our code, we have a controller named "DashboardController" with a method named "index":
```php
#[Route('/publisher/admin', name: 'publisher_admin')]
#[Route('/customer/admin', name: 'customer_admin')]
public function index(): Response
{
$adminUrlGenerator = $this->container->get(AdminUrlGenerator::class);

    return $this->redirect($adminUrlGenerator->setRoute('my_route')->generateUrl());
}
```

The root cause of the issue is that the "getDashboardDto" method is unable to select the correct route definition. Since the "publisher_admin" route definition comes before the "customer_admin" route definition, the method prioritizes it, resulting in users being redirected to the "/publisher/admin" URL and encountering "Access denied" errors.

To resolve this problem;

```php
// AdminContextFactory.php
private function getDashboardDto(Request $request, DashboardControllerInterface $dashboardControllerInstance): DashboardDto
{
    $dashboardRoutesCachePath = $this->cacheDir.'/'.CacheWarmer::DASHBOARD_ROUTES_CACHE;
    $dashboardControllerRoutes = !file_exists($dashboardRoutesCachePath) ? [] : require $dashboardRoutesCachePath; // [ "publisher_admin" => "App\Controller\Admin\DashboardController::index" "customer_admin" => "App\Controller\Admin\DashboardController::index" ]
    $dashboardController = $dashboardControllerInstance::class.'::index';
    $dashboardRouteName = null;
    $currentRouteName = $request->attributes->get('_route');

    foreach ($dashboardControllerRoutes as $routeName => $controller) {
        if ($controller === $dashboardController && $currentRouteName === $routeName) {

            $dashboardRouteName = preg_replace('~\.\w{2}$~', '', $routeName);

            break;
        }
    }

    .
    .
    .

    $dashboardDto = $dashboardControllerInstance->configureDashboard()->getAsDto();
    $dashboardDto->setRouteName($dashboardRouteName);

    return $dashboardDto;
}
```

Commits
-------

0a9ff07 Priority of the getDashboardDto routeName fixed.
  • Loading branch information
javiereguiluz committed Jul 31, 2023
2 parents 87cbad7 + 0a9ff07 commit 8e35082
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Factory/AdminContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ private function getDashboardDto(Request $request, DashboardControllerInterface
$dashboardControllerRoutes = !file_exists($dashboardRoutesCachePath) ? [] : require $dashboardRoutesCachePath;
$dashboardController = $dashboardControllerInstance::class.'::index';
$dashboardRouteName = null;
$currentRouteName = $request->attributes->get('_route');

foreach ($dashboardControllerRoutes as $routeName => $controller) {
if ($controller === $dashboardController) {
if ($controller === $dashboardController && $currentRouteName === $routeName) {
// if present, remove the suffix of i18n route names (it's a two-letter locale at the end
// of the route name; e.g. 'dashboard.en' -> remove '.en', 'admin.index.es' -> remove '.es')
$dashboardRouteName = preg_replace('~\.\w{2}$~', '', $routeName);
Expand Down

3 comments on commit 8e35082

@Atko
Copy link

@Atko Atko commented on 8e35082 Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guys this simple change made a 500 internal server error on my project. I have several actions defined in my DashboardController and when I try to access them by their custom route /admin/customroute I got a 500 error with the following error log:

[Fri Aug 04 13:59:41.832936 2023] [fastcgi:error] [pid 6439] [client x.x.x.x:y] FastCGI: server "php8.1" stderr: PHP message: [critical] Uncaught PHP Exception RuntimeException: "The name of the route associated to "App\\Controller\\Admin\\DashboardController::index" cannot be determined. Clear the application cache to run the EasyAdmin cache warmer, which generates the needed data to find this route." at /vendor/easycorp/easyadmin-bundle/src/Factory/AdminContextFactory.php line 92

@javiereguiluz
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Atko I'm sorry about this. Please, try to create an issue with a few details so we can check what happens. Thanks!

@risototh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem as Atko... Upgraded Symfony to 6.3.3 and updated the EA to 4.7.1 and now half of my system does not work and returns the mentioned exception...

Please sign in to comment.