colincameron
colincameron

Reputation: 2703

Replacement for deprecated RoleController.GetUserRoles()

Prior to DotNetNuke 7 it was possible to get a list of the roles a user belongs to using the following method:

DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();

foreach (Entities.Users.UserRoleInfo roleInfo in rc.GetUserRoles(portalID, userID))
{
    string roleName = roleInfo.RoleName;
}

However, since DNN7, the GetUserRoles function is deprecated.

What alternative is there to this function?

Upvotes: 3

Views: 2245

Answers (2)

Chris Hammond
Chris Hammond

Reputation: 8943

If you populate the UserInfo object there is a Roles array there that lists off all of the roles that a user is in.

Upvotes: 1

colincameron
colincameron

Reputation: 2703

I couldn't find any info on this anywhere, so I browsed the APIs and came across the following:

int portalID = PortalController.GetCurrentPortalSettings().PortalId;
DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
Entities.Users.UserInfo info = DotNetNuke.Entities.Users.UserController.GetUserById(portalID, userID);

foreach (string roleName in info.Roles)
{
    Security.Roles.RoleInfo role = rc.GetRoleByName(portalID, roleName);
    Entities.Users.UserRoleInfo roleInfo = rc.GetUserRole(portalID, userID, role.RoleID);
}

This serves as a replacement to the above code, getting the UserRoleInfo object - if in a round-about way!

Upvotes: 7

Related Questions