Mitchell
Mitchell

Reputation: 253

DNN Check if User is in Role Group

I'm adding onto my DNN module a check to exclude certain users from having to answer some questions when logging in. Instead of hard coding each individual role I'd like to instead just exclude anyone within a particular role group. That way if we have more roles in the future we can just add them into the role group if we want them to be excluded.

However, I don't know how you check if a user is in role group. I know how to check the role, but not the group if they are in one.

SOLUTION: Here's the code I put together based on the answers I got. Should work.

RoleGroupInfo RoleGrp = RoleController.GetRoleGroupByName(this.PortalId, "Role Group");
bool bShouldSkipQuestions = false;
if (RoleGrp != null)
{
    Dictionary<string, RoleInfo> GroupChk = RoleGrp.Roles;
    if (GroupChk.Count > 0)
    {
        foreach (var item in GroupChk.Values)
        {
            if (_user.IsInRole(item.RoleName))
            {
                bShouldSkipQuestions = true;
                break;
            }
        }
    }
}

Upvotes: 4

Views: 7269

Answers (1)

bdukes
bdukes

Reputation: 155935

Role groups aren't really intended to be used like that (they're intended just for end-user organization), so there isn't a direct way to check that. You'll want to get all of the roles in the group (RoleController.GetRolesByRoleGroup) and then check PortalSecurity.IsInRoles, passing in a comma-separated string of the role names.

Try this code:

var roleGroup = RoleController.GetRoleGroupByName(this.PortalId, "Role Group");
var shouldSkipQuestions = roleGroup != null 
                          && roleGroup.Roles.Keys.Any(role => _user.IsInRole(role));

Upvotes: 7

Related Questions