Last active
September 24, 2019 16:47
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
uwl_webform.route_subscriber: | |
class: Drupal\custom_module\Routing\RouteSubscriber | |
tags: | |
- { name: event_subscriber } | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful documentation: https://www.drupal.org/node/2092643#section-requirements
https://www.drupal.org/docs/8/api/routing-system/altering-existing-routes-and-adding-new-routes-based-on-dynamic-ones