Restrict access to user edit pages etc

Submitted bybarnettech onWed, 12/02/2020 - 12:31

https://drupal.stackexchange.com/questions/127194/prevent-users-from-ed…


<?php
  
namespace Drupal\ipc_commerce\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) {

    // Always deny access to unwanted routes.
    $disallow_routes = [
      'app_group.user_vouchers',
      'commerce_order.address_book.overview',
      'commerce_order.address_book.add_form',
      'entity.commerce_payment_method.collection',
      'entity.user.edit_form',
    ];
    foreach ($disallow_routes as $disallow_route) {
      if ($route = $collection->get($disallow_route)) {
        $route->setRequirement('_access', 'FALSE');
      }
    }
  }

}