Skip to content

Instantly share code, notes, and snippets.

@dmulvi
Created September 21, 2017 16:32
Show Gist options
  • Select an option

  • Save dmulvi/41a3ca4ae8fe68dabf9597b20dc862cf to your computer and use it in GitHub Desktop.

Select an option

Save dmulvi/41a3ca4ae8fe68dabf9597b20dc862cf to your computer and use it in GitHub Desktop.
Prevent Access to module via Logic Hooks
<?php
// add these hook definitions to any module you want to prevent access to
<?php
$hook_version = 1;
$hook_array = Array();
$hook_array['before_delete'] = Array();
$hook_array['before_delete'][] = Array(1,'Prevent Delete','custom/PreventAccessHook.php','PreventAccessHook','preventAccess');
$hook_array['after_retrieve'] = Array();
$hook_array['after_retrieve'][] = Array(1,'Prevent Record Access','custom/PreventAccessHook.php','PreventAccessHook','preventAccess');
$hook_array['process_record'] = Array();
$hook_array['process_record'][] = Array(1,'Prevent List/Subpanel Access','custom/PreventAccessHook.php','PreventAccessHook','preventAccess');
<?php
class PreventAccessHook
{
public function preventAccess($bean, $event, $args) {
global $db, $current_user, $log;
// allow admins to view records
if ($current_user->is_admin && $event !== 'before_delete') {
return;
}
// log this attempted access
$log->fatal('Prevent Access Hook Triggered'."\n".' User: '.$current_user->user_name."\n".' Module: '.$bean->module_name."\n".' Event: '.$event);
// pop up alert
throw new SugarApiExceptionInvalidParameter('Access Denied');
// prevent further execution
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment