Try to display different panels based on users' roles

Hi everyone,

I try to display different panels in my custom module’s detail view based on users’ roles.
Here is my code for view.detail.php

<?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/MVC/View/views/view.detail.php');

class a1234_meViewDetail extends ViewDetail
{
    function a1234_meViewDetail()
    {
        parent::ViewDetail();
    }
    function display()
    {
    	if(empty($this->bean->id)) {             
 			global $app_strings;             
 			sugar_die($app_strings['ERROR_NO_RECORD']);         
 		}

    	if(file_exists('cache/modules/' . $this->bean->module_dir . '/DetailView.tpl'))             unlink('cache/modules/' . $this->bean->module_dir . '/DetailView.tpl');  

    	require_once('modules/a1234_me/metadata/toggle_panel_fieldsdefs.php'); 
    	global $current_user;
    	foreach($toggle_panel_fieldsdefs as $role => $field_defs){
 			foreach($field_defs as $field => $visible){
 				if ($current_user->check_role_membership($role)){
 					foreach($this->dv->defs['panels'] as $panel => $panel_defs){                 
 						if ((strtoupper($panel) == strtoupper($field)) && ($visible == 0)) {
 							//$this->dv->defs['panels'][$panel] = array();
 							unset($this->dv->defs['panels'][$panel]);                
 						}              
 					}                 
 				}             
 			}         
 		}
 		parent::display();
    }
}

However, there is a problem.
After I login as one role, the code works well, for example, a sale panel is hidden.
I switch to another role which the sale panel should be displayed.
Nevertheless, the sale panel is still hidden from that user.

Once a panel is unset, it never come back until repair the system.
Can anybody help me?

Do you just the need the panel hidden in the browser (but present in HTML source), just for practical effect, or do you need it completely removed from HTML (for security reasons)?

have you tried to hide it with javascript?

Just want to hide it in browser.

Hi mikebeck,

I know I could hide it with javascript, but that would be more complex, and not easy to maintain. I just want to figure out how to realize it with my method.

Maybe you can search for clues here

include/SubPanel/SubPanelDefinitions.php

there are functions like set_hidden_subpanels and get_hidden_subpanels which seem to manipulate settings to hide subpanels. This is probably what the app saves in “Admin / Display modules and Subpanels” option.