Skip to content

Instantly share code, notes, and snippets.

@lincoln-chawora
Last active September 24, 2019 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lincoln-chawora/d97f85be5a2798b9a98ee47378ab09f1 to your computer and use it in GitHub Desktop.
Save lincoln-chawora/d97f85be5a2798b9a98ee47378ab09f1 to your computer and use it in GitHub Desktop.
Route subscriber to prevent users without the certain permission's or role from accessing pages (Webform admin pages example)
services:
uwl_webform.route_subscriber:
class: Drupal\custom_module\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
<?php
namespace Drupal\custom_module\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
$routes = $collection->all();
foreach ($routes as $route_name => $route) {
switch ($route_name) {
case 'webform.addons':
case 'webform.help':
case 'webform.config':
$route->setRequirement('_permission', 'administer webform');
break;
}
}
}
}