How to restrict admin in ACL and allow a specific role ?

Heena Dahiya
Kilo Contributor

I am working on Geneva version and I am trying to restrict admin override on ACL with the below script.

I intend to allow only users with "contact manager" role to view my field and restrict all others even if they are admin, I have unchecked admin override

I have tried the below two scripts, can someone help me figure out the glitch. Thanks Advance !!

// Allow access only if the contract manager role is present else restrict even if its admin.

if (gs.hasRoleExactly('contract_manager')) {

  answer = true;

}

else {

  answer = false;

}

and

// Allow access only if the contract manager role is present else restrict even if its admin.

if ((gs.hasRole('contract_manager') && gs.hasRole('admin')) || (gs.hasRole('contract_manager') && !gs.hasRole('admin'))) {

  answer = true;

}

else if (!gs.hasRole('contract_manager') && gs.hasRole('admin')){

  answer = false;

}

else {

  answer = false;

}

Regards,

Heena Dahiya

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Heena,



One of the unique things about the admin role is that hasRole() will always return true for admins whether they specifically have a role or not. You might try using gs.getSession().getRoles() which returns all roles for a user and then iterate through those in a script.



Getting a User Object - ServiceNow Wiki


View solution in original post

5 REPLIES 5

anupama8
Tera Expert

Hi Heena,



Instead of writing code in script part, if you can try this:


remove the script and add the required role in the "Requires role"


and uncheck the "admin override".



PS: Hit answered, like, Helpful or Correct depending on the impact of the response.


Hi Anupama,



That was the first thing I did, post that I tried this script. Apologies I forgot to mention that.


Though I appreciate you response



Regards,


Heena


Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Heena,



One of the unique things about the admin role is that hasRole() will always return true for admins whether they specifically have a role or not. You might try using gs.getSession().getRoles() which returns all roles for a user and then iterate through those in a script.



Getting a User Object - ServiceNow Wiki


Thanks Brad !! That was helpful.



Regards,


Heena