Commit c5b3010e authored by alexpott's avatar alexpott

Issue #2302463 by effulgentsia: Cleanup User::hasPermission() and...

Issue #2302463 by effulgentsia: Cleanup User::hasPermission() and UserSession::hasPermission() to follow Law of Demeter.
parent ff99927f
......@@ -141,15 +141,7 @@ public function hasPermission($permission) {
return TRUE;
}
$roles = \Drupal::entityManager()->getStorage('user_role')->loadMultiple($this->getRoles());
foreach ($roles as $role) {
if ($role->hasPermission($permission)) {
return TRUE;
}
}
return FALSE;
return $this->getRoleStorage()->isPermissionInRoles($permission, $this->getRoles());
}
/**
......@@ -250,4 +242,14 @@ public function getHostname() {
return $this->hostname;
}
/**
* Returns the role storage object.
*
* @return \Drupal\user\RoleStorageInterface
* The role storage object.
*/
protected function getRoleStorage() {
return \Drupal::entityManager()->getStorage('user_role');
}
}
......@@ -242,15 +242,7 @@ public function hasPermission($permission) {
return TRUE;
}
$roles = \Drupal::entityManager()->getStorage('user_role')->loadMultiple($this->getRoles());
foreach ($roles as $role) {
if ($role->hasPermission($permission)) {
return TRUE;
}
}
return FALSE;
return $this->getRoleStorage()->isPermissionInRoles($permission, $this->getRoles());
}
/**
......@@ -534,4 +526,14 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return $fields;
}
/**
* Returns the role storage object.
*
* @return \Drupal\user\RoleStorageInterface
* The role storage object.
*/
protected function getRoleStorage() {
return \Drupal::entityManager()->getStorage('user_role');
}
}
......@@ -14,6 +14,21 @@
*/
class RoleStorage extends ConfigEntityStorage implements RoleStorageInterface {
/**
* {@inheritdoc}
*/
public function isPermissionInRoles($permission, array $rids) {
$has_permission = FALSE;
foreach ($this->loadMultiple($rids) as $role) {
if ($role->hasPermission($permission)) {
$has_permission = TRUE;
break;
}
}
return $has_permission;
}
/**
* {@inheritdoc}
*/
......
......@@ -14,6 +14,19 @@
*/
interface RoleStorageInterface extends ConfigEntityStorageInterface {
/**
* Returns whether a permission is in one of the passed in roles.
*
* @param string $permission
* The permission.
* @param array $rids
* The list of role IDs to check.
*
* @return bool
* TRUE is the permission is in at least one of the roles. FALSE otherwise.
*/
public function isPermissionInRoles($permission, array $rids);
/**
* Delete role references.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment