From 4f337822b100d6a0411b9dd4126940d3a5f6aa67 Mon Sep 17 00:00:00 2001 From: Civiware Solutions Date: Fri, 29 Jun 2018 00:40:53 +0530 Subject: [PATCH] added code to restrict deletion of relationship --- deleterelationshipperm.php | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/deleterelationshipperm.php b/deleterelationshipperm.php index 541ed58..c2436f1 100644 --- a/deleterelationshipperm.php +++ b/deleterelationshipperm.php @@ -133,3 +133,72 @@ function deleterelationshipperm_civicrm_alterSettingsFolders(&$metaDataFolders = function deleterelationshipperm_civicrm_entityTypes(&$entityTypes) { _deleterelationshipperm_civix_civicrm_entityTypes($entityTypes); } +/** + * Implements hook_civicrm_permission(). + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_permission + */ +function deleterelationshipperm_civicrm_permission(&$permissions) { + $prefix = ts('CiviCRM') . ': '; + $permissions['delete relationships'] = [ + $prefix . ts('Delete relationships'), + ts('Delete contact relationships'), + ]; +} + +/** + * Implements hook_civicrm_links(). + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_links + */ +function deleterelationshipperm_civicrm_links($op, $objectName, $objectId, &$links, &$mask, &$values) { + if ($op == 'relationship.selector.row' && $objectName == 'Relationship') { + if (!CRM_Core_Permission::check('delete relationships')) { + foreach ($links as $key => $link) { + if ($link['name'] == 'Delete') { + unset($links[$key]); + } + } + } + } +} + +/** + * Implements hook_civicrm_pre(). + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_pre + */ +function deleterelationshipperm_civicrm_pre($op, $objectName, $id, &$params) { + if ($op == 'delete' && $objectName == 'Relationship') { + _deleterelationshipperm_civicrm_check_permission(); + } +} + +/** + * Implements hook_civicrm_apiWrappers(). + * + * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_apiWrappers + */ +function deleterelationshipperm_civicrm_apiWrappers(&$wrappers, $apiRequest) { + if ($apiRequest['entity'] == 'Relationship' && $apiRequest['action'] == 'delete') { + _deleterelationshipperm_civicrm_check_permission('exception'); + } +} + +/** + * Check if user has permission to delete relationship. + * + * @param string $errorType + */ +function _deleterelationshipperm_civicrm_check_permission($errorType = NULL) { + if (CRM_Core_Permission::check('delete relationships')) { + return FALSE; + } + $message = ts('You do not have the necessary permission to delete this relationship.'); + if ($errorType == 'exception') { + throw new API_Exception($message); + } + else { + CRM_Core_Error::statusBounce($message); + } +}