I need to create a permission to allow users to see the content overview page (/admin/content) only during business hours (9 AM to 5 PM).
Drupal core comes with the "Access the Content overview page" permission; but I need "Access the Content overview page during work hours".
I know how to create a permission and how to test that the user has the permission. What I don't understand is how to assign some logic to a permission itself (for example, within business hours).
I can do this in mymodule.permissions.yml.
access content overview within business hours:
title: 'Access the Content overview page within business hours'
restrict access: TRUE
I can check the user has the permission with \Drupal::currentUser()->hasPermission('access content overview within business hours')
.
How can I make the permission return TRUE
only during business hours?
How can I check the permission only when the users visits the content overview page?
I can foresee multiple use cases for needing some logic implementation within the permission itself, for example when allowing/restricting access to certain pages depending on whether the environment is development or production, or allowing/restricting access depending on the value of some fields.
- EDIT -
As suggested in the comments, I've started by creating the following three files on my module; but it doesn't work. The CustomAccessChecks::access
never gets called.
File: custom_access.services.yml
services:
custom_access.route_subscriber:
class: Drupal\custom_access\Routing\AdminRouteSubscriber
tags:
- { name: event_subscriber }
custom_access.access_checker:
class: Drupal\custom_access\Access\CustomAccessChecks
tags:
- { name: access_check }
File: src/Access/CustomAccessChecks.php
<?php
namespace Drupal\custom_access\Access;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResultAllowed;
use Symfony\Component\Routing\Route;
use Drupal\Core\Routing\RouteMatchInterface;
class CustomAccessChecks implements AccessInterface {
/**
* A custom access check.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Run access checks for this account.
*/
public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
return AccessResultAllowed::allowedIf((int) $account->id() === 1);
}
}
File: AdminRouteSubscriber.php
<?php
namespace Drupal\custom_access\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
class AdminRouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
public function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('system.admin_content')) {
$route->setRequirement('_custom_access', '\Drupal\custom_access\Access\CustomAccessChecks::access');
}
}
}
As expected, visiting '/admin/content' as the main administrator $account->id() === 1
works, but when visiting as another authenticated user, I still can access the page.
What I'm missing?