Make an Edit View field Read Only based on User Role (SugarCRM CE)

I recently had a SugarCRM project which had a requirement to make a Select field read only depending on the current users role. The requirment was that if the currenlty logged in user was in the Admin or Admin Manager role then they would be able to see the full select box in the edit view of the module. Otherwise they would only see the current value of the field and therefore not be able to change the field at all.

In order to achive this I first had to create a custom viewEdit file for my module like so:

custom/modules/[your_module]/views/view.edit.php (if this folder does not exist just create it and the file)

      class [your_module]ViewEdit extends ViewEdit
{
    function display(){
        require_once 'modules/ACLRoles/ACLRole.php';

        //Get the current user's role
        $objACLRole = new ACLRole();
        $roles = $objACLRole->getUserRoles($GLOBALS['current_user']->id);
        //check if they are in the Admin or Admin Manager's role
        if(in_array('Admin',$roles) || in_array('Admin Manager',$roles)){
            $this->ev->ss->assign('ReadOnly', '');
        }
        else{ //If not pass in a variable with the value readonly
            $this->ev->ss->assign('ReadOnly', 'readonly');
        }
        //Call the parent display function
        parent::display();
    }
}

The above code gets the currently logged in users roles and checks if the user is in the Admin or Admin Manager role if not it then assigns a value of ‘readonly’ to a variable called ReadOnly and passes it to the smarty template.

We then have to create a customCode field in the editviewdefs that checks if the ReadOnly variable is set to readonly and if so only displays the value otherwise it displays the full select box:

custom/modules/[your_module]/metadata/editviewdefs.php

(If this folder and file does not exist just go into studio and make a small change to the edit view layout for your chosen module and save it. This will create the custom metadata files for the module)

Scroll through the editview defs and find your field in the array then add the ‘customCode’ attribute below the lable like below.


 'panels' => 
    array (
      'default' => 
      array (
        0 => 
        array (
          0 => 'name',
          1 => 
          array (
            'name' => 'status',
            'studio' => 'visible',
            'label' => 'LBL_STATUS',
            'customCode' => '{if $ReadOnly == "readonly"}<span>{$fields.status.value}</span>{else}{html_options name="status" id="status" options=$fields.status.options selected=$fields.status.value}{/if}',
          ),
        ),
        1 => 

The code in brackets like this {if $readOnly == “readonly”} is smarty template code.

Andy
About

Software Developer from Falkirk, Central Scotland.

Posted in Uncategorised

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>