Hide a panel from Detailview based on the user

V.7.13

I have managed to hide panels in both the detail view and the editing view of the Accounts module.

I do it based on the condition of the value in the User’s “department” field so that it shows the panel according to the user.

The panel is therefore associated with the User and if it is not, the Javascript will be executed and it will not be displayed.

The difficulty of doing it based on the role, the id’s user or the group of users (it gave errors) has led me to this solution. And works.

The codes are these:

Code for detail view.

/custom/modules/Accounts/views/view.detail.php

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

require_once('modules/Accounts/views/view.detail.php');

class CustomAccountsViewDetail extends ViewDetail {

    public function display() {
        global $current_user;

        // Get the value of the current user's 'department' field
        $userDepartment = $current_user->department;

       // Defines the condition based on the value of the 'department' field
        if ($userDepartment != "1") {
          // JavaScript code to hide the panel
            $jScriptToHidePanel = <<<EndOfScript
            <script type="text/javascript">
              // Hide the panel
                $("a[href=#top-panel-0]").parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;

            // Call the base class method to show the detail view
            parent::display();

          // Print the JavaScript
            echo $jScriptToHidePanel;
        } else {
         // If the condition is not met, simply show the detail view without hiding the panel
            parent::display();
        }
    }
}

If the user does not have a 1 in their department box on the user record, the panel will not be displayed.

We must use the value that the panel has in “href”, mine is #top-panel-0. Use the browser inspection.

Code for detail edit

/custom/modules/Accounts/views/view.edit.php

<?php

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

require_once('modules/Accounts/metadata/editviewdefs.php');

class CustomAccountsViewEdit extends ViewEdit {

	public function display() {

        global $current_user;

        // Get the value of the current user's 'department' field
        $userDepartment = $current_user->department;
       // Defines the condition based on the value of the 'department' field
        if ($userDepartment != "1") {
          // JavaScript code to hide the panel
            $jScriptToHidePanel = <<<EndOfScript
            <script type="text/javascript">
              // Hide the panel
                $('div[id="detailpanel_0"]').parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;

           // Call the base class method to show the detail view
            parent::display();

          // Print the JavaScript
            echo $jScriptToHidePanel;
        } else {
         // If the condition is not met, simply show the detail view without hiding the panel
            parent::display();
        }
    }
}

If the user does not have a 1 in their department box on the user record, the panel will not be displayed when editing a new or already created record.

We must use the value that the panel has in “id”, use browser inspection, mine is “detailpanel_0”

If we want to hide different panels for different users, this is the example code to use:

/custom/modules/Accounts/views/view.detail.php

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

require_once('modules/Accounts/views/view.detail.php');

class CustomAccountsViewDetail extends ViewDetail {

    public function display() {
        global $current_user;

        // Get the value of the current user's 'department' field
        $userDepartment = $current_user->department;

       // Defines various conditions based on the value of the 'department' field
        $condition1 = ($userDepartment != "1");
        $condition2 = ($userDepartment != "2");
        $condition3 = ($userDepartment != "3");

        // Check each condition and hide the corresponding panels
        if ($condition1) {
            $jScriptToHidePanel1 = <<<EndOfScript
            <script type="text/javascript">
                // Hide the first panel
                $("a[href=#top-panel-0]").parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;

            // Print the JavaScript to hide the first panel
            echo $jScriptToHidePanel1;
        }

        if ($condition2) {
            $jScriptToHidePanel2 = <<<EndOfScript
            <script type="text/javascript">
                // Hide the second panel
                $("a[href=#top-panel-1]").parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;

            // Print the JavaScript to hide the second panel
            echo $jScriptToHidePanel2;
        }

        if ($condition3) {
            $jScriptToHidePanel3 = <<<EndOfScript
            <script type="text/javascript">
               // Hide the third panel
                $("a[href=#top-panel-2]").parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;

           // Print the JavaScript to hide the third panel
            echo $jScriptToHidePanel3;
        }

        // Call the base class method to show the detail view
        parent::display();
    }
}

This may be useful in some cases, but it’s important to note that it does not provide real security - the information is all there in the user’s browser for him to see.

If you actually require security to hide this information, you’d have to do your work in the backend in such a way that the panel does not even get sent to the front end.

Thank you for the advice. I would like to know how to do it.

And I have seen two problems to be corrected here:

When you save the new record, all the panels are displayed in that moment and when you edit an existing record, all the panels are displayed.

Instead of sending that bit of JavaScript to the front end to hide the panel, you should try to change the actual parent display function in order to keep the HTML of the panel from ever getting to the front end.

Looks like a job for Security Groups. Depending on the user’s Security Group, they get the panel HTML output on the page, or they don’t get it.

1 Like

I tried to do this, but if you use the user id or security group id variables it hides the entire detail view. I don’t know why, but I had to leave that option.

I have managed to get it to work in all cases and based on the security group id!!!

Code for detail view.

/custom/modules/Accounts/views/view.detail.php

The securitygroups table in the database has the group ids.

The panel to hide has href:#top-panel-0 in DetailView. Look at it with the browser inspector.

Panel value is different in DetailView and EditView

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

require_once('modules/Accounts/views/view.detail.php');
require_once('modules/SecurityGroups/SecurityGroup.php');

class CustomAccountsViewDetail extends ViewDetail {

public function getSecurityGroupId($user_id) {
    $current_security_groups = SecurityGroup::getUserSecurityGroups($user_id); // Get the security groups of the current user (returns an array)
    foreach ($current_security_groups as $security_group) {
        return $security_group['id'];
    }
    return ''; // If the user does not belong to any group, return an empty string or whatever value you want.
}

    public function display() {
         global $current_user;
    $user_id = $current_user->id;
    $group_id_to_show_panel = 'ddcdea94-613e-e025-75b1-6543fc5f8ad3'; // ID of the group that can see the panel

   // Get the ID of the group the user belongs to
    $user_group_id = $this->getSecurityGroupId($user_id);

    // Check if the user's group ID does not match the ID that the dashboard can see
    if ($user_group_id !== $group_id_to_show_panel) {

// JavaScript code to hide the panel
            $jScriptToHidePanel = <<<EndOfScript
            <script type="text/javascript">
                // Hide the panel
                $("a[href=#top-panel-0]").parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;

           // Call the base class method to show the detail view
            parent::display();

            // Print the JavaScript
            echo $jScriptToHidePanel;
        } else {
           // If the condition is not met, simply show the detail view without hiding the panel
            parent::display();
    }
}
}

Code for detail edit

/custom/modules/Accounts/views/view.edit.php

The securitygroups table in the database has the group ids.

The panel to hide has id:detailpanel_0 in EditView. Look at it with the browser inspector.

Panel value is different in DetailView and EditView

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

require_once('modules/Accounts/metadata/editviewdefs.php');
require_once('modules/SecurityGroups/SecurityGroup.php');

class CustomAccountsViewEdit extends ViewEdit {

public function getSecurityGroupId($user_id) {
    $current_security_groups = SecurityGroup::getUserSecurityGroups($user_id); // Get the security groups of the current user (returns an array)
    foreach ($current_security_groups as $security_group) {
        return $security_group['id'];
    }
    return ''; // If the user does not belong to any group, return an empty string or whatever value you want.
}


    public function display() {
       global $current_user;
    $user_id = $current_user->id;
    $group_id_to_show_panel = 'ddcdea94-613e-e025-75b1-6543fc5f8ad3'; // ID of the group that can see the panel

   // Get the ID of the group the user belongs to
    $user_group_id = $this->getSecurityGroupId($user_id);

    // Check if the user's group ID does not match the ID that the dashboard can see
    if ($user_group_id !== $group_id_to_show_panel) {

          // JavaScript code to hide the panel
            $jScriptToHidePanel = <<<EndOfScript
            <script type="text/javascript">
                // Hide the panel
                $('div[id="detailpanel_0"]').parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;

           // Call the base class method to show the edit view
            parent::display();

          // Print the JavaScript
            echo $jScriptToHidePanel;
        } else {
         // If the condition is not met, simply show the edit view without hiding the panel
            parent::display();
    }
}
}

And the codes if we want to use different security group id conditions for different panels.

Code for detail view.

/custom/modules/Accounts/views/view.detail.php

The securitygroups table in the database has the group ids.

The panel to hide has href:#top-panel-0 in DetailView. Look at it with the browser inspector.

Panel value is different in DetailView and EditView

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

require_once('modules/Accounts/views/view.detail.php');
require_once('modules/SecurityGroups/SecurityGroup.php');

class CustomAccountsViewDetail extends ViewDetail {

    public function getSecurityGroupId($user_id) {
        $current_security_groups = SecurityGroup::getUserSecurityGroups($user_id);
        foreach ($current_security_groups as $security_group) {
            return $security_group['id'];
        }
        return ''; // If the user does not belong to any group, return an empty string or whatever value you want.
    }

    public function display() {
        global $current_user;
        $user_id = $current_user->id;
        $group_id_to_show_1_panel = 'ddcdea94-613e-e025-75b1-6543fc5f8ad3'; // ID of the group that can see the panel 1
        $group_id_to_show_2_panel = '643006fd-c022-e4ab-df66-6543fc09801e'; // ID of the group that can see the panel 2

        // Get the ID of the group the user belongs to
        $user_group_id = $this->getSecurityGroupId($user_id);

        // Check if the user belongs to the group that can see the panel 1
        $canSee1Panel = ($user_group_id === $group_id_to_show_1_panel);

        // Check if the user belongs to the group that can see  panel 2
        $canSee2Panel = ($user_group_id === $group_id_to_show_2_panel);

        if (!$canSee1Panel) {
            // Hide panel 1
            $jScriptToHidePanel1 = <<<EndOfScript
            <script type="text/javascript">
                $("a[href=#top-panel-0]").parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;
            echo $jScriptToHidePanel1;
        }

        if (!$canSee2Panel) {
            // Hide panel 2
            $jScriptToHidePanel2 = <<<EndOfScript
            <script type="text/javascript">
                $("a[href=#top-panel-1]").parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;
            echo $jScriptToHidePanel2;
        }

// Call the base class method to show the detail view
        parent::display();

        // Print the JavaScript
        echo $jScriptToHidePanel1;
        if (isset($jScriptToHidePanel2)) {
            echo $jScriptToHidePanel2;
        }
    }
}

Code for detail edit

/custom/modules/Accounts/views/view.edit.php

The securitygroups table in the database has the group ids.

The panel to hide has id:detailpanel_0 in EditView. Look at it with the browser inspector.

Panel value is different in DetailView and EditView

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

require_once('modules/Accounts/metadata/editviewdefs.php');
require_once('modules/SecurityGroups/SecurityGroup.php');

class CustomAccountsViewEdit extends ViewEdit {

public function getSecurityGroupId($user_id) {
    $current_security_groups = SecurityGroup::getUserSecurityGroups($user_id); // Obtener los grupos de seguridad del usuario actual (devuelve un array)
    foreach ($current_security_groups as $security_group) {
        return $security_group['id'];
    }
    return ''; // If the user does not belong to any group, return an empty string or whatever value you want.
}


    public function display() {
       global $current_user;
    $user_id = $current_user->id;
        $group_id_to_show_1_panel = 'ddcdea94-613e-e025-75b1-6543fc5f8ad3'; // ID of the group that can see the panel 1

        $group_id_to_show_2_panel = '643006fd-c022-e4ab-df66-6543fc09801e'; // ID of the group that can see the panel 2


        // Get the ID of the group the user belongs to
        $user_group_id = $this->getSecurityGroupId($user_id);

        // Check if the user belongs to the group that can see the panel 1
        $canSee1Panel = ($user_group_id === $group_id_to_show_1_panel);

        // Check if the user belongs to the group that can see  panel 2
        $canSee2Panel = ($user_group_id === $group_id_to_show_2_panel);

        if (!$canSee1Panel) {
            /// Hide panel 1
            $jScriptToHidePanel1 = <<<EndOfScript
            <script type="text/javascript">
                $('div[id="detailpanel_0"]').parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;
            echo $jScriptToHidePanel1;
        }

        if (!$canSee2Panel) {
            // Hide panel 2
            $jScriptToHidePanel2 = <<<EndOfScript
            <script type="text/javascript">
                $('div[id="detailpanel_1"]').parents('.panel-default').addClass('hidden');
            </script>
            EndOfScript;
            echo $jScriptToHidePanel2;
        }

     // Call the base class method to show the detail view
        parent::display();

        // Print the JavaScript
        echo $jScriptToHidePanel1;
        if (isset($jScriptToHidePanel2)) {
            echo $jScriptToHidePanel2;
        }
    }
}

May I suggest - instead of overriding display, override pre_display. Don’t call the parent method in your method, just copy the entire contents, to start:

    public function preDisplay()
    {
        $metadataFile = $this->getMetaDataFile();
        $this->dv = new DetailView2();
        $this->dv->ss =&  $this->ss;
        $this->dv->setup($this->module, $this->bean, $metadataFile, get_custom_file_if_exists('include/DetailView/DetailView.tpl'));
    }

You can keep your logic looking at security groups, etc. But instead of injecting JS into the view, to remove bits of the screen, do your magic before the display ever runs. There are many ways to do this but I would try having two separate tpls ready, one with the panel, one without, and select which one to use when calling setup.

Or you could use a single tpl and make it have an optional section.

It’s just that I get itchy with things that look like security but aren’t…

thank you!!! You’re great.

In my case the panels that are hidden will be empty, I don’t care that they may be in the browser.

But you have given the way for whoever needs to hide them using two tpls.

1 Like

There are more automated ways available on the web. If that helps

https://www.google.com/search?q=suitecrm+hide+manager&oq=suitecrm+hide+manager&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIHCAEQIRigATIHCAIQIRigATIHCAMQIRigAdIBCDMyODFqMGo3qAIAsAIA&sourceid=chrome&ie=UTF-8