Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Afform (et al) - Fetch more complete list of permissions via APIv4 #19536

Merged
merged 6 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CRM/Admin/Form/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ public function buildQuickForm() {

$this->add('text', 'icon', ts('Icon'), ['class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE]);

$getPerms = (array) \Civi\Api4\Permission::get(0)
->addWhere('group', 'IN', ['civicrm', 'cms', 'const'])
->setOrderBy(['title' => 'ASC'])
->execute();
$permissions = [];
foreach (CRM_Core_Permission::basicPermissions(TRUE, TRUE) as $id => $vals) {
$permissions[] = ['id' => $id, 'text' => $vals[0], 'description' => (array) CRM_Utils_Array::value(1, $vals)];
foreach ($getPerms as $perm) {
$permissions[] = ['id' => $perm['name'], 'text' => $perm['title'], 'description' => $perm['description'] ?? ''];
}
$this->add('select2', 'permission', ts('Permission'), $permissions, FALSE,
['placeholder' => ts('Unrestricted'), 'class' => 'huge', 'multiple' => TRUE]
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/Permission/Backdrop.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getAvailablePermissions() {
$modules = system_get_info('module');
foreach ($modules as $moduleName => $module) {
$prefix = isset($module['name']) ? ($module['name'] . ': ') : '';
foreach (module_invoke($moduleName, 'permission') as $permName => $perm) {
foreach (module_invoke($moduleName, 'permission') ?? [] as $permName => $perm) {
if (isset($allCorePerms[$permName])) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/Permission/Drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getAvailablePermissions() {
$modules = system_get_info('module');
foreach ($modules as $moduleName => $module) {
$prefix = isset($module['name']) ? ($module['name'] . ': ') : '';
foreach (module_invoke($moduleName, 'permission') as $permName => $perm) {
foreach (module_invoke($moduleName, 'permission') ?? [] as $permName => $perm) {
if (isset($allCorePerms[$permName])) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Core/Permission/List.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public static function findConstPermissions(GenericHookEvent $e) {
// There are a handful of special permissions defined in CRM/Core/Permission.
$e->permissions[\CRM_Core_Permission::ALWAYS_DENY_PERMISSION] = [
'group' => 'const',
'title' => ts('Constant: Always deny'),
'title' => ts('Generic: Deny all users'),
'is_synthetic' => TRUE,
];
$e->permissions[\CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION] = [
'group' => 'const',
'title' => ts('Constant: Always allow'),
'title' => ts('Generic: Allow all users (including anonymous)'),
'is_synthetic' => TRUE,
];
}
Expand Down
9 changes: 8 additions & 1 deletion CRM/Report/Form/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,17 @@ public static function buildForm(&$form) {
$form->freeze('is_reserved');
}

$getPerms = \Civi\Api4\Permission::get(0)
->addWhere('is_active', '=', 1)
->addWhere('group', 'IN', ['civicrm', 'cms', 'const'])
->setOrderBy(['title' => 'ASC'])
->execute();
$form->addElement('select',
'permission',
ts('Permission'),
['0' => ts('Everyone (includes anonymous)')] + CRM_Core_Permission::basicPermissions()
// FIXME: Historically, CiviReport hard-coded an extra '0' option. This should change to the more general ALWAYS_ALLOW_PERMISSION (but may require testing/migration).
['0' => ts('Everyone (includes anonymous)')] + array_combine($getPerms->column('name'), $getPerms->column('title')),
['class' => 'crm-select2']
);

// prepare user_roles to save as names not as ids
Expand Down
13 changes: 9 additions & 4 deletions ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,16 @@ public static function getGuiSettings() {
];

$data['permissions'] = [];
foreach (\CRM_Core_Permission::basicPermissions(TRUE, TRUE) as $name => $perm) {
$perms = \Civi\Api4\Permission::get()
->addWhere('group', 'IN', ['afformGeneric', 'const', 'civicrm', 'cms'])
->addWhere('is_active', '=', 1)
->setOrderBy(['title' => 'ASC'])
->execute();
foreach ($perms as $perm) {
$data['permissions'][] = [
'id' => $name,
'text' => $perm[0],
'description' => $perm[1] ?? NULL,
'id' => $perm['name'],
'text' => $perm['title'],
'description' => $perm['description'] ?? NULL,
];
}

Expand Down