Skip to main content
Post Reopened by leymannx, mpdonadio
edited tags
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Use version tags only to highlight differences between major Drupal version, not to tell which version you are using
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

This happens a lot of the time within my custom resourcesDrupal 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()
        );
    }

This happens a lot of the time with custom resources 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()
        );
    }

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()
        );
    }
added more context regarding currentUser object
Source Link

This happens a lot of the time with custom resources 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()
        );
    }

This happens a lot of the time with custom resources 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'

This happens a lot of the time with custom resources 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()
        );
    }
Post Closed as "Needs details or clarity" by Hudri, Patrick Kenny, mona lisa
Source Link
Loading