diff --git a/src/Plugin/Condition/UserHasRole.php b/src/Plugin/Condition/UserHasRole.php index 357e262..8c0490f 100644 --- a/src/Plugin/Condition/UserHasRole.php +++ b/src/Plugin/Condition/UserHasRole.php @@ -4,6 +4,7 @@ namespace Drupal\rules\Plugin\Condition; use Drupal\rules\Core\RulesConditionBase; use Drupal\rules\Exception\InvalidArgumentException; +use Drupal\user\RoleInterface; use Drupal\user\UserInterface; use Drupal\user\Entity\Role; @@ -58,21 +59,24 @@ class UserHasRole extends RulesConditionBase { $drupal_roles = Role::loadMultiple(); } // If the role is not a role entity yet, load it if we can. - if (!$role instanceof Role) { + if (!$role instanceof RoleInterface) { // Try to load the role by role id. - if ($r = Role::load(strtolower($role))) { + if ($r = Role::load(strtolower(trim($role)))) { $role = $r; } else { // Try to load the role by role label. foreach ($drupal_roles as $drupal_role) { - if ($drupal_role->get('label') == trim($role)) { + if ($drupal_role->get('label') === trim($role)) { $role = $drupal_role; break; } } } } + if (!$role instanceof RoleInterface) { + throw new \LogicException('Role ' . $role . ' does not exist.'); + } return $role->id(); }, $roles);