0

This happens a lot of the time in my custom Drupal 10 module and I cannot figure out why. Custom permissions return wrong value with currentUser but returns correct value when loading user by uid. $this->currentUser is valid and the correct account. What's the issue/cause and resolution?

Example:

Wrong value:

$hasViewPermission = $this->currentUser->hasPermission(
    sprintf('view %s categories', $termGroup)
);
// false

Correct value

$user = User::load(\Drupal::currentUser()->id());
$hasViewPermission = $user->hasPermission(
    sprintf('view %s categories', $termGroup)
);
// true

Role config

permissions:
  ...
  - 'view EDITORIAL categories'

Edit: added demo of currentUser initialisation

    protected $currentUser;

    public function __construct(
        array $configuration, $plugin_id,
        $plugin_definition,
        array $serializer_formats,
        LoggerInterface $logger,
        AccountProxyInterface $current_user,
        Request $current_request
    ) {
        parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
        $this->currentUser = $current_user;
        $this->currentRequest = $current_request;
    }


    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
    {
        return new static(
            $configuration,
            $plugin_id,
            $plugin_definition,
            $container->getParameter('serializer.formats'),
            $container->get('logger.factory')->get('custom_rest'),
            $container->get('current_user'),
            $container->get('request_stack')->getCurrentRequest()
        );
    }
6
  • 1
    This question is not answerable with the given context. Obvisouly $this->currentUser from 1st example is not the same as $user from 2nd example. Blind guessing: either $this->currentUser was never initialized, or $this->currentUser is incorrectly cached.
    – Hudri
    Commented May 28 at 12:11
  • Is $termGroup set to EDITORIAL in both cases?
    – mona lisa
    Commented May 28 at 14:15
  • @monalisa yeah permission is the same. currentUser is initialised and all good. I think maybe cache like @Hudri mentioned. Flushing caches doesn't make a difference though. What other caches are there for user object? Commented May 28 at 14:16
  • We need some context as to where your code exists. Also have to dumped these objects to compare them? They may not be the same object. This could be drupal.org/project/drupal/issues/3182145, for example.
    – mona lisa
    Commented May 28 at 14:42
  • 1
    @AbdulSadikYalcin Can you provide context about where $this->currentUser is getting called from? Service, controller, form, etc? And does your editor hint what the type of $this->currentUser is? It is a little hard to guess here, but I suspect you have an Account and not a User. This is a really confusing part of core, where when you have an Account you need to load the full user for some scenarios.
    – mpdonadio
    Commented May 28 at 23:03

0

Your Answer

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

Browse other questions tagged or ask your own question.