Skip to content
Snippets Groups Projects
Commit b4d6a411 authored by eileen's avatar eileen
Browse files

[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
parent 2bc16779
Branches
Tags
No related merge requests found
......@@ -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;
}
}
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment