You are here

protected function UserRouteEventSubscriber::getUserRoutes in Username Enumeration Prevention 8

Get an array of user route IDs.

Return value

array An array of user route IDs.

1 call to UserRouteEventSubscriber::getUserRoutes()
UserRouteEventSubscriber::onException in src/UserRouteEventSubscriber.php

File

src/UserRouteEventSubscriber.php, line 76

Class

UserRouteEventSubscriber
Modifies user-related routes to respond with 404 rather than 403.

Namespace

Drupal\username_enumeration_prevention

Code

protected function getUserRoutes() : array {
  $userRouteIds = $this->cache
    ->get(static::ROUTE_CID);
  if ($userRouteIds !== FALSE) {
    return $userRouteIds->data;
  }
  $userLinkTemplates = $this->entityTypeManager
    ->getDefinition('user')
    ->getLinkTemplates();
  $routes = new RouteCollection();
  foreach ($userLinkTemplates as $path) {
    $routes
      ->addCollection($this->routeProvider
      ->getRoutesByPattern($path));
  }
  $userRouteIds = array_keys(array_filter(iterator_to_array($routes), function (Route $route) : bool {
    $parameters = $route
      ->getOption('parameters') ?? [];
    if (is_array($parameters)) {
      foreach ($parameters as $parameter) {

        // This captures most routes, however some legacy routes don't have
        // parameters, especially views.
        if ($parameter['type'] ?? NULL === 'entity:user') {
          return TRUE;
        }
      }
    }
    return strpos($route
      ->getPath(), '{user}') !== FALSE;
  }));
  $userRouteIds[] = 'user.cancel_confirm';
  $userRouteIds[] = 'shortcut.set_switch';
  $this->cache
    ->set(static::ROUTE_CID, $userRouteIds, Cache::PERMANENT, [
    'routes',
  ]);
  return $userRouteIds;
}