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

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.


jared_wentzel
Kilo Contributor

Do I have to use the Sys ID? Is there a way I can just use the actual value? I'm also going to have to add a OR statment to it and I have to add a handful of locations


Unless you want to create a custom script include to return that, or populate a separate field on the user record to indicate that the user is in one of the locations you want, you'll have to use a sys_id.