I am trying to create a custom access control in my custom module in Drupal 8.
But I am having issues in achieving it.
I cant find a proper way to inject UserInterface class in access method.
Basically what I am trying to do is similar to this
Similar to above code I am tring to get details of user object in access method. But cant find a proper way to achieve this.
My Code is as follows:
custom_module\src\Access\CustomRoleAccessCheck.php
<?php
namespace Drupal\custom_module\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\UserDataInterface;
use Drupal\user\UserInterface;
/**
* Access check for Custom Controller route.
*/
class CustomRoleAccessCheck implements AccessInterface {
/**
* The contact settings config object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The user data service.
*
* @var \Drupal\user\UserDataInterface
*/
protected $userData;
/**
* Constructs a ContactPageAccess instance.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\user\UserDataInterface $user_data
* The user data service.
*/
public function __construct(ConfigFactoryInterface $config_factory, UserDataInterface $user_data) {
$this->configFactory = $config_factory;
$this->userData = $user_data;
}
/**
* Checks access to the given user's contact page.
*
* @param \Drupal\user\UserInterface $user
* The user being contacted.
* @param \Drupal\Core\Session\AccountInterface $account
* The currently logged in account.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
public function access(UserInterface $user, AccountInterface $account) {
$contact_account = $user;
dpm($contact_account); // Need this values for some other functionality.
if ($contact_account->isAnonymous()) {
return AccessResult::forbidden();
}
return AccessResult::allowed();
}
}
services.yml
custom_module.custom_role_access_check:
class: Drupal\custom_module\Access\CustomRoleAccessCheck
arguments: ['@config.factory', '@user.data']
tags:
- { name: access_check, applies_to: _custom_role_access_check }
routing.yml
custom_module.accesscheck:
path: '/custom-module/accesscheck'
defaults:
_controller: '\Drupal\custom_module\Controller\CustomModuleAccessCheckController::accessCheck'
_title: 'A Test Page for Custom Module Access Check'
requirements:
_custom_role_access_check: 'TRUE'
# _permission: 'access content'
When I am trying to acess /custom-module/accesscheck
I am getting the following error
The website encountered an unexpected error. Please try again later.RuntimeException: Callable "Drupal\custom_module\Access\CustomRoleAccessCheck::access" requires a value for the "$user" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (line 142 of core\lib\Drupal\Component\Utility\ArgumentsResolver.php). Drupal\Component\Utility\ArgumentsResolver->getArgument(Object) (Line: 54) Drupal\Component\Utility\ArgumentsResolver->getArguments(Array) (Line: 157) Drupal\Core\Access\AccessManager->performCheck('custom_module.custom_role_access_check', Object) (Line: 135) Drupal\Core\Access\AccessManager->check(Object, Object, Object, 1) (Line: 112) Drupal\Core\Access\AccessManager->checkRequest(Object, Object, 1) (Line: 109) Drupal\Core\Routing\AccessAwareRouter->checkAccess(Object) (Line: 94) Drupal\Core\Routing\AccessAwareRouter->matchRequest(Object) (Line: 115) Symfony\Component\HttpKernel\EventListener\RouterListener->onKernelRequest(Object, 'kernel.request', Object) call_user_func(Array, Object, 'kernel.request', Object) (Line: 111) Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.request', Object) (Line: 127) Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68) Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57) Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99) Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78) Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693) Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
I also tried to construct a $accoutData object in constructor in the following way but same error occured.
public function __construct(ConfigFactoryInterface $config_factory, UserDataInterface $user_data, AccountInterface $account_data) {
$this->configFactory = $config_factory;
$this->userData = $user_data;
$this->accountData = $account_data;
}
Can anyone please suggest a proper way to do it.
Edit :
I am trying to get details of current user.
I know there is a way to obtain current user details by using
\Drupal::currentUser();
but it doesnt have all the user information so I am trying to do that with DI.