How to get the list of all the roles which are added as part of the group.

Uma Udikeri
Tera Contributor

Hi All, 

 

I wanted to get the list of all the roles associated with the logged in user added as part of the group of which he is a member of , this should also include all the contains roles. For example if the user is added to group "ABC" and it has 3 roles ( role1, role2, role3) and role2 contains (crole1, cRole2) and role3 contains role(crole3, crole4) and so on.

I have written a script which give me this result if there is a one level hierarchy of contains role I have to add one more loop and so on. what if crole1 again has contains role(ccrole1,ccrole2 and so on how many loops should we process to get all the roles the user got as part of the group. How to know its the end and this role does not contain any more contains role.

 

My script which contains gets first level of hierarchal role.

 

var user = gs.getUserID();
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", user);
gr.query();
var arr = [];
while (gr.next()) {
   arr.push(gr.group.getDisplayValue());
   var grGroup = new GlideRecord('sys_group_has_role');
   grGroup.addQuery('group.name', gr.group.getDisplayValue());
   grGroup.query();
   while(grGroup.next())
   {
     gs.info("Role : " + grGroup.role.getDisplayValue());
     var cr = new GlideRecord('sys_user_role_contains');
     cr.addQuery('role.name',grGroup.role.getDisplayValue());
     cr.query();
     while(cr.next())
     {
        gs.info("Contains Role " + cr.contains.getDisplayValue());
       //How many more loops should I add , is there a limit?
     }
   }
}


 

 

Thanks,

Uma

0 REPLIES 0