Assign role based on user's location

jared_wentzel
Kilo Contributor

Is there a way to assign a role based on the user's location? So for all the users that have the value of "USA" in their Location field, grant the "usa_user" role. I think a script like the one below should work but I'm not sure where to put it.

var gr = new GlideRecord("sys_user");

gr.query();

while(gr.next()) {

if (gr.location == "USA") {

gr.roles = gr.roles + ",usa_user";

gr.update();

}

}

1 ACCEPTED SOLUTION

gs.getUser().getLocation() == 'THE SYS_ID OF YOUR LOCATION HERE'



Remember, it's critical that your location sys_ids are consistent between instances so that you can use location data for permissions and security in this area (and many other built-in areas).



Please mark this as the answer to your question once you've confirmed that it works.


View solution in original post

7 REPLIES 7

Mark Stanger
Giga Sage

You'll need to add records to the 'sys_user_has_role' table using a GlideRecord insert in order to do that.   You'll want to be careful though since role allocation usually comes with an additional license cost for each of those users.   Why not just check their location instead of adding a duplicate attribute?


Well the end goal is to limit visibility of a UI Action form button I added to the incident form. It only works for US users so I am trying to hide it using the conditions field. Is there a way I can limit it's visibility based on current users location?


Absolutely.   You can get any information from a user record and match against it.   Getting a location value for a user is as simple as this...



gs.getUser().getLocation()



You could set that up in your condition to match against a specific sys_id value, just make sure that your sys_id values for locations are consistent across environments!



This guru article explains all of the different ways to get user information.


» User Object Cheat Sheet


UIAction.JPGHow would I use it as a condition for the UI Action? This didn't work