Read-only fields based on Roles

Hi

I am trying to make some “Date & Time” and “Dropdown” fields read-only (on edit page) according to the Role of the user.

I am able to do it with “type=text” fields using the following code from this post. See the code bellow.

But I am unable to do it for “Date & Time” and “Dropdown” field types.

On custom/modules/Contacts/metadata/editviewdefs.php


'customCode' => '<input type="text" class="phone" tabindex="0" title="" value="{$fields.phone_work.value}" maxlength="100" size="30" id="phone_work" name="phone_work" {$readOnly}>',

On custom/modules/Contacts/views/view.edit.php


function display(){ // add this if function display doesn't exist  
                global $current_user;
                // check if current user is in specific role
                // code taken from thread
                $IS_AM = in_array("<ROLE NAME>", ACLRole::getUserRoleNames($current_user->id));
                if($IS_AM)
                    $this->ev->ss->assign('readOnly', 'readonly = "readonly"');
                else
                    $this->ev->ss->assign('readOnly', '');
                parent::display(); // add this if function display doesn't exist 
} // add this if function display doesn't exist

Thanks

Hi berriop,

This is more of a development query than support. I’m sure there are developers out there who can assist you with this.

Thanks,

Will.

Hello Frnd,

I am looking for read only drop down field according to roles in sugarcrm. I found your topic as you were looking for the same thing.

Could you please help me out for making the dropdown field read only?

What to add in place of … for dropdown field?

Thanks in advance and waiting for your response.

Hello everyone,

Dropdown fields not accept readonly atribute in tag. And date & time field have a javascript onclick atribute in the calendar image, so the best way is hide the field. To make dropdown field “readonly” and hide the date field, include in the display() function:

function display(){ // add this if function display doesn't exist  
                global $current_user;
                // check if current user is in specific role
                // code taken from thread
                $IS_AM = in_array("<ROLE NAME>", ACLRole::getUserRoleNames($current_user->id));
                if($IS_AM){
                    $this->ev->ss->assign('readOnly', 'readonly = "readonly"');
                    $this->ev->ss->assign('readOnlyDropDown', 'disabled= "true"');
		    $this->ev->ss->assign('HideDateTime', 'style="display:none;"');
                }else{
                    $this->ev->ss->assign('readOnly', '');
                }
                parent::display(); // add this if function display doesn't exist 
} // add this if function display doesn't exist

In the editviewdefs:

Dropdown:

'customCode' => '<select {$readOnlyDropDown} name="account_type" id="account_type" title="">
<option label="" value=""></option>
<option label="Analista" value="Analyst">Analista</option>
<option label="Cliente" value="Customer">Cliente</option>
<option label="Concorrente" value="Competitor">Concorrente</option>
</select><script>$("#account_type").val("{$fields.account_type.value}");</script>',

Date&Time:

'customCode' => '<input {$HideDateTime} class="date_input" autocomplete="off" type="text" name="date_closed" id="date_closed" value="{$fields.date_closed.value}" title="" tabindex="0" size="11" maxlength="10">',

Not forget to do the Repair and Rebuild.

I hope this help! :slight_smile:

1 Like

Not working with Assigned to: .
Drop-down menu not visible.
Could you help me?Thanks

@berriop hello buddy , Actually i was trying to do the same with the image field , Is that possible? to do so ?
If its possible could you please tell me in detail.Thanks in advance.

Thanks @dugim, you approach was super helpful.

I sort of combined what you did and another post using {if} statements in the custom code. I needed to test if user was admin, if so then make it editable. Otherwise, just show the value.

My view.edit.php is:

class AccountsViewEdit extends ViewEdit
{
    public function __construct()
    {
        parent::__construct();
        $this->useForSubpanel = true;
        $this->useModuleQuickCreateTemplate = true;
    }
	public function display()
    {
        global $current_user;

        // Check if the user is an administrator
        $isAdministrator = $current_user->is_admin;

        // Set the $readOnly variable based on the user's administrator status
        $readOnly = $isAdministrator ? '' : 'readonly = "readonly"';

        // Assign $readOnly to Smarty template
        $this->ev->ss->assign('readOnly', $readOnly);
		
		//Assign $isAdministrator to Smarty template
		 $this->ev->ss->assign('isAdministrator', $isAdministrator);

        parent::display(); // Call the parent display function to render the view
    }
}

And my editvewdefs is:

1 => 
          array (
            'name' => 'price_level_date_c',
			'customCode' => '{if $isAdministrator}@@FIELD@@{else}<input type="text" class="date_input" tabindex="0" title="" value="{$fields.price_level_date_c.value}" maxlength="99" size="30" id="price_level_date_c" name="price_level_date_c" {$readOnly}>
				{/if}',
            'label' => 'LBL_PRICE_LEVEL_DATE',			
          ),
        ),

using the @@field@@ just shows the normal field in editvew.

1 Like