SugarCRM Hide Panels Based on a User’s Role
24 Jan 2012After a few weeks of looking around, digging into example after example after example, I finally stumbled across a simple method to dynamically hide a panel on the Detail and Edit View in SugarCRM.
As you’re probably aware, SugarCRM has a ton of business logic hooks, whether for the application frame, the modules themselves, or the users–such as logging in, out, etc.
This example makes use of the ‘after_ui_frame’ application logic hook, which, in Sugar’s terminology, is “fired after the frame has been invoked and before the footer has been invoked.” But if you’re anything like me, this means almost nothing to you. In simpler English, the ‘after_ui_frame’ hook will run your class and method on just about every page. So yes, this is where you would put custom javascript, such as in the event you wanted an alert window on every page.
But you can also put CSS in here! Here’s how I used the ‘after_ui_frame’ to hide a panel so that some users don’t see a specific panel on a specific module.
Step 1
First, create a file called “logic_hooks.php” and place it in your custom/modules/ folder. Your logic_hooks.php file should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); // Set the version hooks should use, currently Sugar only supports version 1. $hook_version = 1; // Initialize the hook array $hook_array = Array(); // Initialize the hook type $hook_array['after_ui_frame'] = Array(); $hook_array['after_ui_frame'][] = array( 1, //Hook version 'Hides panels depending on roles.', //Label 'custom/modules/hidepanels.php', //Include file 'HidePanelsClass', //Class 'HidePanelsMethod' //Method ); ?>
Step 2
Next, create a file called “hidepanels.php” and place it in the same directory as above. In this document, place the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 <?php class HidePanelsClass{ function HidePanelsMethod(&$bean, $event, $arguments) { // HIDE PANEL ON WEB CHANGE REQUEST MODULE // // if($_GET[module]=='<YOUR MODULE>' OR $_POST[module]=='<YOUR MODULE>') { // Because EditView and DetailView use POST and GET respectively global $current_user; // so you can see who your user is // include the roles code include("modules/ACLRoles/ACLRole.php"); $acl_role_obj = new ACLRole(); $user_roles = $acl_role_obj->getUserRoles($current_user->id); // get all the roles the user belongs to $show_role = "<PUT YOUR ROLE HERE>"; // choose the role who you want to SEE this panel, for example $user_in_role = (in_array($show_role,$user_roles) ? 1 : 0); // check to see if the user's role is in the array // $bean->fetched_row['status'] is the stored status of the case, $bean->status would be the new value /custom/themes/default/images if($user_in_role!=1) { // if the user isn't part of the role, then hide the panel echo '<style type="text/css"> #LBL_PANEL1 {display:none !important;}</style>'; // css to hide the panel } } // // // END HIDE PANEL } } ?>
That’s it! Of course, you can customize this to your liking. Perhaps you want to hide a panel from everyone unless they’re an admin, or maybe you want to hide financial information in an order fulfillment module. The only thing is–this method superficially hides the panel, but the content of the panel will still be visible if the user views the source.
Happy coding…
