Condition: Role of user - Why not adding string value on UserTrait::loadUserAccount?

Created on 13 December 2024, 9 days ago

Heya everyone

In my use case, I retrieve user IDs through a view, save the UIDs in a list, and iterate over them. At a later stage, I need to check if a user has a specific role. However, upon reviewing the code behind the "Role of user" condition, I noticed that checking for a role is only possible when an AccountInterface object, a UserInterface object, or a numeric value (presumably the UID) is available for loading the user account. Here’s the relevant code:

  public function evaluate(): bool {
    if ($account = $this->loadUserAccount()) {
      $userRoles = $account->getRoles();
      $result = in_array($this->configuration['role'], $userRoles, TRUE);
      $debug = 1;
      return $this->negationCheck($result);
    }
    return FALSE;
  }
  protected function loadUserAccount(): ?UserInterface {
    $account = $this->tokenService->getOrReplace($this->configuration['account']);
    if ($account instanceof AccountInterface) {
      if (!($account instanceof UserInterface)) {
        $account = $account->id();
      }
    }
    elseif (!is_numeric($account)) {
      $account = $this->tokenService->replaceClear($this->configuration['account']);

      // @see user_tokens().
      if ((string) $account === 'not yet assigned') {
        $account = 0;
      }
    }
    if (is_numeric($account)) {
      /**
       * @var \Drupal\user\UserInterface $account
       */
      try {
        $account = $this->entityTypeManager->getStorage('user')->load($account);
      }
      catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
        $account = NULL;
      }
    }
    return ($account instanceof UserInterface) ? $account : NULL;
  }

The issue I’m facing is that the UID returned from the view is a string. While I could create a separate action to load the user entity based on the view results, I would prefer to avoid adding another step in workflows to keep them simpler and less complex.

My question is: why isn’t the "Role of user" condition designed to support loading a user account directly from a string value? Alternatively, what is the reasoning behind restricting it to AccountInterface, UserInterface, or numeric values for loading the account?

Any insights would be greatly appreciated!

💬 Support request
Status

Active

Version

2.0

Component

Code

Created by

🇩🇪Germany Istari

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Comments & Activities

  • Issue created by @Istari
  • 🇩🇪Germany jurgenhaas Gottmadingen

    It does also work with a string that contains e.g. "123" as the PHP method is_numeric is documented as this:

    Finds whether a variable is a number or a numeric string

    However, when you're using a view to get a list of users, you already have the user entities, there shouldn't be any need to load the user entity again later.

  • 🇩🇪Germany Istari

    ah okay makes sense :)
    thanks for your answer

  • 🇩🇪Germany jurgenhaas Gottmadingen
Production build 0.71.5 2024