Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all users in a role including roles in roles?

I have a Sitecore site that uses the AD module for connecting to an Active Directory. Let's say that we have a Role defined in Sitecore called "Content Authors". Content Authors may contain individual user accounts - "jsmith" - or it might contain an entire AD Group - "Northeast Managers". I need to get a list of all users who are in the "Content Authors" role, either directly or indirectly (through an AD group). Right now my code only seems to be returning users that are directly a member of the "Content Authors" role. Here is my code:

string[] _roleUserNames = System.Web.Security.Roles.GetUsersInRole("Content Authors");

I was assuming that this code would return the "effective" list of everyone who is in that role. It seems to only return people who are directly in that role. Does anyone know if there is some other way of getting everyone in a role?

like image 843
Corey Burnett Avatar asked Feb 21 '23 02:02

Corey Burnett


1 Answers

I figured out that this is a specific issue to Sitecore as Sitecore allows Roles in Roles and that functionality is built on top of the MS ASP.NET Membership stuff. To get all users in a role including "indirect" users you should use the following code:

IEnumerable<User> _roleUsers = Sitecore.Security.Accounts.RolesInRolesManager.GetUsersInRole(Role.FromName("Content Authors"), true);

This will give you all of the users including indirect users.

like image 88
Corey Burnett Avatar answered Mar 27 '23 04:03

Corey Burnett