Restrict User to edit a document based on Role

Hi Everyone,

I want to restrict a user that s/he should not be able to edit the document if s/he doesn’t have the “Item Manager” role.

Below is my code but it is also stopping the user if the user is having the “Item Manager” role whereas it should not, can anyone help me to find out the issue in my code.

if (frm.doc.workflow_state=="Approval" user_roles.indexOf("Item Manager")!=-1)
   {
           frappe.msgprint("You cannot makes changes to this item.");
           validated=false;
   } 

Regards
Ruchin Sharma

1 Like

@ruchin78 , You can manage this by using Role Permission Manager instead of writing code .

  1. Go to Role Permission Manager
  2. Select Document
  3. Set Read, Write permission to only Item Manager role and set only Read permission to other roles

Hi @priya_s
My requirement is different
A Role say “Item Create”, whosoever is having “Item Create” role can create the item as per workflow.
Now, once he submit the item for approval s/he cannot make any changes to it, but before that they can make n number of changes to the item.

Once s/he submit the item for approval only user with “Item Manger” role can make change therefore I cannot set it on the basis of Permission Manager.

Regards
Ruchin Sharma

I think you are missing && or || operator in your if condition. Please correct and try…

Ah - Sorry, that I have missed while removing some more stuff in the condition at the time of pasting the code in the forum. However it is already there in my code.

@ruchin78

Try that

  if (frm.doc.workflow_state=="Approval" &&/ || !(frappe.user.has_role(['Item Manager'])))

{
frappe.msgprint(“You cannot makes changes to this item.”);
validated=false;
}