From 14564ba58476ad7131bdedcaff5cf68892b56bab Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 1 Jun 2021 13:40:07 -0400 Subject: [PATCH] SearchKit - Update hook_civicrm_searchKitTasks signature to include checkPermissions and userId --- .../Action/SearchDisplay/GetSearchTasks.php | 27 +++++++++++++++---- ext/search_kit/search_kit.php | 10 ++++--- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php index d09629433b..1c58800f43 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/GetSearchTasks.php @@ -64,10 +64,16 @@ class GetSearchTasks extends \Civi\Api4\Generic\AbstractAction { } if ($entity['name'] === 'Contact') { - // Add contact tasks which support standalone mode (with a 'url' property) - $contactTasks = \CRM_Contact_Task::permissionedTaskTitles(\CRM_Core_Permission::getPermission()); + // Add contact tasks which support standalone mode + $contactTasks = $this->checkPermissions ? \CRM_Contact_Task::permissionedTaskTitles(\CRM_Core_Permission::getPermission()) : NULL; foreach (\CRM_Contact_Task::tasks() as $id => $task) { - if (isset($contactTasks[$id]) && !empty($task['url']) && $task['url'] !== 'civicrm/task/delete-contact') { + if ( + (!$this->checkPermissions || isset($contactTasks[$id])) && + // Must support standalone mode (with a 'url' property) + !empty($task['url']) && + // The delete task is redundant with the new api-based one + $task['url'] !== 'civicrm/task/delete-contact' + ) { if ($task['url'] === 'civicrm/task/pick-profile') { $task['title'] = E::ts('Profile Update'); } @@ -84,6 +90,7 @@ class GetSearchTasks extends \Civi\Api4\Generic\AbstractAction { } if ($entity['name'] === 'Contribution') { + // FIXME: tasks() function always checks permissions, should respect `$this->checkPermissions` foreach (\CRM_Contribute_Task::tasks() as $id => $task) { if (!empty($task['url'])) { $tasks[$entity['name']]['contribution.' . $id] = [ @@ -98,9 +105,19 @@ class GetSearchTasks extends \Civi\Api4\Generic\AbstractAction { } } + // Call `hook_civicrm_searchKitTasks`. + // Note - this hook serves 2 purposes, both to augment this list of tasks AND to + // get a full list of Angular modules which provide tasks. That's why this hook needs + // the base-level array and not just the array of tasks for `$this->entity`. + // Although it may seem wasteful to have extensions add tasks for all possible entities and then + // discard most of it (all but the ones relevant to `$this->entity`), it's necessary to do it this way + // so that they can be declared as angular dependencies - see search_kit_civicrm_angularModules(). $null = NULL; - \CRM_Utils_Hook::singleton()->invoke(['tasks'], $tasks, - $null, $null, $null, $null, $null, 'civicrm_searchKitTasks' + $checkPermissions = $this->checkPermissions; + $userId = $this->checkPermissions ? \CRM_Core_Session::getLoggedInContactID() : NULL; + \CRM_Utils_Hook::singleton()->invoke(['tasks', 'checkPermissions', 'userId'], + $tasks, $checkPermissions, $userId, + $null, $null, $null, 'civicrm_searchKitTasks' ); usort($tasks[$entity['name']], function($a, $b) { diff --git a/ext/search_kit/search_kit.php b/ext/search_kit/search_kit.php index 6c94f751a7..dc661c8b97 100644 --- a/ext/search_kit/search_kit.php +++ b/ext/search_kit/search_kit.php @@ -45,11 +45,13 @@ function search_kit_civicrm_managed(&$entities) { */ function search_kit_civicrm_angularModules(&$angularModules) { _search_kit_civix_civicrm_angularModules($angularModules); - // Fetch all search tasks provided by other modules and add them as crmSearchTasks dependencies - $tasks = $dependencies = []; + // Fetch all search tasks provided by extensions and add their Angular modules as crmSearchTasks dependencies + $tasks = []; $null = NULL; - CRM_Utils_Hook::singleton()->invoke(['tasks'], $tasks, - $null, $null, $null, $null, $null, 'civicrm_searchKitTasks' + $checkPermissions = FALSE; + \CRM_Utils_Hook::singleton()->invoke(['tasks', 'checkPermissions', 'userId'], + $tasks, $checkPermissions, $null, + $null, $null, $null, 'civicrm_searchKitTasks' ); foreach ($tasks as $entityTasks) { foreach ($entityTasks as $task) { -- 2.25.1