From b4d6a41186c38c6398d40993e19277a9db31a560 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 13 Mar 2021 10:24:10 +1300 Subject: [PATCH] [REF] Cleanup on permission code The function returns either an array of arrays or an array of the first key in the array, depending on the 'desriptions' parameter. This cleans it up so that instead of handling the descriptions parameter in multiple places we get the array in all cases and do the formatting at the end --- CRM/Core/Permission.php | 69 +++++++++++++++++++----------------- CRM/Core/Permission/Base.php | 1 + 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/CRM/Core/Permission.php b/CRM/Core/Permission.php index cd21819d18..2837ad3ac1 100644 --- a/CRM/Core/Permission.php +++ b/CRM/Core/Permission.php @@ -589,44 +589,16 @@ class CRM_Core_Permission { public static function assembleBasicPermissions($all = FALSE, $descriptions = FALSE) { $config = CRM_Core_Config::singleton(); $permissions = self::getCorePermissions(); + $permissions = array_merge($permissions, self::getComponentPermissions($all)); + // Add any permissions defined in hook_civicrm_permission implementations. + $module_permissions = $config->userPermissionClass->getAllModulePermissions(TRUE); + $permissions = array_merge($permissions, $module_permissions); if (!$descriptions) { foreach ($permissions as $name => $attr) { $permissions[$name] = array_shift($attr); } } - if (!$all) { - $components = CRM_Core_Component::getEnabledComponents(); - } - else { - $components = CRM_Core_Component::getComponents(); - } - - foreach ($components as $comp) { - $perm = $comp->getPermissions($all, $descriptions); - if ($perm) { - $info = $comp->getInfo(); - foreach ($perm as $p => $attr) { - - if (!is_array($attr)) { - $attr = [$attr]; - } - - $attr[0] = $info['translatedName'] . ': ' . $attr[0]; - - if ($descriptions) { - $permissions[$p] = $attr; - } - else { - $permissions[$p] = $attr[0]; - } - } - } - } - - // Add any permissions defined in hook_civicrm_permission implementations. - $module_permissions = $config->userPermissionClass->getAllModulePermissions($descriptions); - $permissions = array_merge($permissions, $module_permissions); return $permissions; } @@ -1711,4 +1683,37 @@ class CRM_Core_Permission { return FALSE; } + /** + * @param bool $includeDisabled + * + * @return array + * @throws \CRM_Core_Exception + */ + protected static function getComponentPermissions(bool $includeDisabled): array { + if (!$includeDisabled) { + $components = CRM_Core_Component::getEnabledComponents(); + } + else { + $components = CRM_Core_Component::getComponents(); + } + + $permissions = []; + foreach ($components as $comp) { + $perm = $comp->getPermissions($includeDisabled, TRUE); + if ($perm) { + $info = $comp->getInfo(); + foreach ($perm as $p => $attr) { + + if (!is_array($attr)) { + $attr = [$attr]; + } + + $attr[0] = $info['translatedName'] . ': ' . $attr[0]; + $permissions[$p] = $attr; + } + } + } + return $permissions; + } + } diff --git a/CRM/Core/Permission/Base.php b/CRM/Core/Permission/Base.php index 1ceae88d7e..0c7bf678a3 100644 --- a/CRM/Core/Permission/Base.php +++ b/CRM/Core/Permission/Base.php @@ -395,6 +395,7 @@ class CRM_Core_Permission_Base { * Array of permissions, in the same format as CRM_Core_Permission::getCorePermissions(). */ public function getAllModulePermissions($descriptions = FALSE) { + // Passing in false here is to be deprecated. $permissions = []; CRM_Utils_Hook::permission($permissions); -- 2.25.1