The CreatorCon Call for Content is officially open! Get started here.

How to traverse user role from getUserRoles() function?

kai6novice
Kilo Contributor

I am getting userRoles from:

var thisUser = gs.getUser();

var userRoles = thisUser.getUserRoles();

//var userRoles2 = thisUser.getRoles(); //this doesn't really work, because when I turn debug log on, and call gs.print(userRoles2); it only return one role.

gs.print(userRoles); //this return 2 roles.

Now I would like to traverse the role, but no matter what I try, i failed.

//gs.print(userRoles.length); failed, maybe because userRoles is not an array?

//for (role in userRoles){gs.print(role)}; failed, again because userRoles is not an array.

9 REPLIES 9

Thanks Goran,



    Thanks for the solution, it works. But I am wondering if I can work with the object directly without converting it to string. I hope any service now employee can help explain what type of object the getUserRoles() return or maybe point me to the API documentation of that function.



Thanks,


Kai


Hi Kai,



Why would you need that anyway ?



Regards,


Vikas


Hi vikas,


    I am building a drop down menu in self service portal. AMD I store the menu item in navigation menu record. I use JavaScript to retrieve the menu item from the navigation menu record and try to filter it by the roles item in the record against the login user's role.


My approach is the following:


1. Get a list of the current login user's roles. (Call getuser().getuserroles()) Loop through them one by one.


2. Check each role against each menu item's role. If the roles in menu item record is empty, then always include the menu item.


3. If the roles item in the menu item record is not empty, then only include the menu item when the users role exists in the menu items roles item.


-Kai


can't you use the "hasRole" to check if the user has the menu items role?



//Göran


I can't do the reverse. Because I use query to retrieve the menu item record. AddQuery() and addorcondition(). I will post my final solution later.


-Kai