added code to restrict deletion of relationship
This commit is contained in:
		| @@ -133,3 +133,72 @@ function deleterelationshipperm_civicrm_alterSettingsFolders(&$metaDataFolders = | |||||||
| function deleterelationshipperm_civicrm_entityTypes(&$entityTypes) { | function deleterelationshipperm_civicrm_entityTypes(&$entityTypes) { | ||||||
|   _deleterelationshipperm_civix_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); | ||||||
|  |   } | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user