adding roles to user

sunnysunny
Kilo Contributor

This is the script for adding roles to the user, which I copied from wiki. Can anyone explain what is accumulated_roles here and why we convert this to string?

var gr = new GlideRecord("sys_user");

gr.query();

while(gr.next()) {

  if (gr.accumulated_roles.toString().indexOf(",self_service,") == -1) {

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

  gr.update();

}

}

1 ACCEPTED SOLUTION

Here is the script that   will add self service role to all the active users




var gr = new GlideRecord("sys_user");


gr.addQuery('active', true)


gr.query();




while(gr.next()) {




      var grd = new GlideRecord('sys_user_has_role');


              grd.user = gr.sys_id;


grd.role = '[sys_id of the role you want to add]'; // sys_id of self_service role


grd.insert();




}

View solution in original post

13 REPLIES 13

dvp
Mega Sage
Mega Sage

There is a field called Accumulated Roles on the user record which is of type string.


So in     this line if (gr.accumulated_roles.toString().indexOf(",self_service,") == -1) {


it check if the that field has self_service role if not then they are adding that role to the accumulated_roles field



sunnysunny
Kilo Contributor

I copied the entire code and running in background script, but it not adding self_service role to the users?


In OOB only the users with maint role has read and write access.



If you want to test it then in ACL's disable the read and write access on sys_user.accumulated_roles


sunnysunny
Kilo Contributor

Ok, I disabled the above acls I copied and paste the above script in the background script, executed it, then I open the user table, abel tutor and opened the roles


and donot see self_service roles in there?