diff --git a/library/Icinga/Authentication/Role.php b/library/Icinga/Authentication/Role.php index f00d063e2..acebae26a 100644 --- a/library/Icinga/Authentication/Role.php +++ b/library/Icinga/Authentication/Role.php @@ -106,4 +106,41 @@ class Role $this->restrictions = $restrictions; return $this; } + + /** + * Whether this role grants the given permission + * + * @param string $permission + * + * @return bool + */ + public function grants($permission) + { + $requiredWildcard = strpos($permission, '*'); + foreach ($this->permissions as $grantedPermission) { + if ($grantedPermission === '*' || $grantedPermission === $permission) { + return true; + } + + if ($requiredWildcard !== false) { + if (($grantedWildcard = strpos($grantedPermission, '*')) !== false) { + $wildcard = min($requiredWildcard, $grantedWildcard); + } else { + $wildcard = $requiredWildcard; + } + } else { + $wildcard = strpos($grantedPermission, '*'); + } + + if ($wildcard !== false && $wildcard > 0) { + if (substr($permission, 0, $wildcard) === substr($grantedPermission, 0, $wildcard)) { + return true; + } + } elseif ($permission === $grantedPermission) { + return true; + } + } + + return false; + } } diff --git a/library/Icinga/User.php b/library/Icinga/User.php index ef5a0ff62..c5652ab62 100644 --- a/library/Icinga/User.php +++ b/library/Icinga/User.php @@ -563,27 +563,8 @@ class User */ public function can($requiredPermission) { - if (isset($this->permissions['*']) || isset($this->permissions[$requiredPermission])) { - return true; - } - - $requiredWildcard = strpos($requiredPermission, '*'); - foreach ($this->permissions as $grantedPermission) { - if ($requiredWildcard !== false) { - if (($grantedWildcard = strpos($grantedPermission, '*')) !== false) { - $wildcard = min($requiredWildcard, $grantedWildcard); - } else { - $wildcard = $requiredWildcard; - } - } else { - $wildcard = strpos($grantedPermission, '*'); - } - - if ($wildcard !== false && $wildcard > 0) { - if (substr($requiredPermission, 0, $wildcard) === substr($grantedPermission, 0, $wildcard)) { - return true; - } - } elseif ($requiredPermission === $grantedPermission) { + foreach ($this->getRoles() as $role) { + if ($role->grants($requiredPermission)) { return true; } }