2

I'm working on a D8 project trying to add custom access rules to some pages.

I want to restrict access to specific users based on their id.

I checked this and seems easy but I can't make it work!

I want to change the access for the route

entity.webform_submission.canonical /admin/structure/webform/manage/{webform}/submission/{webform_submission}

But as I didn't manage to make it work I tried an easier example

contact.site_page
/contact

(Which also doesn't work)

So I created my routing file under my_module/src/Routing folder :

namespace Drupal\my_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) {
    if ($route_item = $collection->get('contact.site_page')) {
      $requirements = $route_item->getRequirements();
      unset($requirements['_permission']);
      $route_item->setRequirement('_access', 'FALSE');
    }
  }
}

After that I added the service file in my_module's root folder:

services:
  my_module.route_subscriber:
    class: Drupal\my_module\Routing\RouteSubscriber
    tags:
      - { name: event_subscriber }

But I don't get Access denied when I visit those pages.

Also, is there any way to test it by using dsm or var_dump?

Any idea/help/suggestion?

7
  • Yes, you can put var_dump($route_item) in alterRoutes(). You see the output when you clear the cache on the command line.
    – 4uk4
    Aug 23, 2017 at 16:43
  • You're removing _permission from a local copy of the requirements array, not from the route object, could that be the problem? Try $requirements['_access'] = 'FALSE'; after the unset, and then $route_item->setRequirements($requirements);
    – Clive
    Aug 23, 2017 at 18:51
  • @Clive, this is not the issue, setting _access to FALSE is all what is needed. Problem here seems to be to get the code running (on a cache clear, routes are not built in normal requests)
    – 4uk4
    Aug 23, 2017 at 19:02
  • @4k4 Thanks, couldn't be bothered to check which had higher priority ;)
    – Clive
    Aug 23, 2017 at 19:03
  • 1
    @4k4 I don't know what happened, I just freed some space at the container, but ti worked! Does it make any sense? I mean the space at the container. Do you think that this is related somehow?
    – Ismini
    Aug 24, 2017 at 9:25

1 Answer 1

0

you need to enable kint() to get the routs but you need to make sure the permissions for using kint() is correct. and you can add your debugging into alterRouts()

1
  • Thank you @Ahmad. var_dump works fine. It was a caching issues I think. Also I had to free some space at my container.
    – Ismini
    Oct 30, 2017 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.