Some permissions allow actions that may not be desirable to have as a search action that can be done en masse. e.g. I may want contacts to merge records but not delete records en masse on a search display. Other actions don't have a specific permission tied to them that have no other way of being controlled.
Goal: Control what available search actions show in core for a given user based on a given permission that exists in Civi.
How do you go about restricting Search Kit actions at a more granular level (for each action?). Or is this something better to work into core?
We have done this for Advanced Search and all other entity searches in legacy search screens. Relevant snippet below done from a custom Wordpress MU plugin according to a org specific custom permissions structure:
add_filter( 'civicrm_searchTasks', 'advanced_search_actions_control_callback', 10, 4 );
//Remove some available actions on search pages for all users if login user does not have 'access_civicrm_advanced_search_actions' capability
function advanced_search_actions_control_callback( $objectType, &$tasks ) {
$original_tasks = $tasks;
$titles = array_column($tasks, 'title');
$to_remove = array();
if ( $objectType = array('contact','event','activities','membership')) {
$capability = 'access_civicrm_advanced_search_actions';
$actions_remove = array('Add relationship - to Household',
'Delete permanently',
'Delete contacts',
'Group - create smart group',
'Tag - add to activities',
'Tag - remove from activities',
'Tag - remove from activities',
'Add relationship - to Individual',
'Add relationship - to Organization',
'Communication preferences - alter',
'Email - unhold addresses',
'Group - create smart group',
'Register participants for event',
'Restore contacts from trash',
'Tag - add to contacts',
'Tag - remove from contacts',
'Update multiple contacts',
'Update multiple memberships'
);
foreach($tasks as $key => $value) {
if ( in_array( $value['title'], $actions_remove ) ) {
//$to_remove[] = $key;
unset($tasks[$key]);
}
}
if(current_user_can( $capability) ) {
$tasks = $original_tasks;
}
}
}
Looking at https://docs.civicrm.org/dev/en/latest/searchkit/tasks/#angular-based-tasks the Search Kit hook is civicrm_searchKitTasks
so I replicated the function above and used but is not working. Obviously, using Angular is a major difference so more changes probably are needed. It would be ideal to not have to permission search actions independently of one other in this searching transtion time. I had hoped the permissioning we've done in advanced search legacy actions would apply over.
add_filter( 'civicrm_searchKitTasks', 'searchkit_actions_control_callback', 10, 4 );
//Remove some available actions on search pages for all users if login user does not have 'access_civicrm_advanced_search_actions' capability
function searchkit_actions_control_callback( $objectType, &$tasks ) {
$original_tasks = $tasks;
$titles = array_column($tasks, 'title');
$to_remove = array();
if ( $objectType = array('contact','event','activities','membership')) {
$capability = 'access_civicrm_advanced_search_actions';
$actions_remove = array('Add relationship - to Household',
'Delete permanently',
'Delete contacts',
'Group - create smart group',
'Tag - add to activities',
'Tag - remove from activities',
'Tag - remove from activities',
'Add relationship - to Individual',
'Add relationship - to Organization',
'Communication preferences - alter',
'Email - unhold addresses',
'Group - create smart group',
'Register participants for event',
'Restore contacts from trash',
'Tag - add to contacts',
'Tag - remove from contacts',
'Update multiple contacts',
'Update multiple memberships'
);
foreach($tasks as $key => $value) {
if ( in_array( $value['title'], $actions_remove ) ) {
//$to_remove[] = $key;
unset($tasks[$key]);
}
}
if(current_user_can( $capability) ) {
$tasks = $original_tasks;
}
}
}