Check if a user is in a role:
UserInfo user = UserInfoProvider.GetUserInfo("MyUserName");
if(user.IsInRole("roleName", "siteName"))
{
// user is in role specified so do something
}
Straight from API Examples:
/// <summary>
/// Adds a user to a role
/// </summary>
private bool CreateUserRole()
{
// Get role and user objects
RoleInfo role = RoleInfoProvider.GetRoleInfo("MyNewRole", SiteContext.CurrentSiteID);
UserInfo user = UserInfoProvider.GetUserInfo("MyNewUser");
if ((role != null) && (user != null))
{
// Create new user role object
UserRoleInfo userRole = new UserRoleInfo();
// Set the properties
userRole.UserID = user.UserID;
userRole.RoleID = role.RoleID;
// Save the user role
UserRoleInfoProvider.SetUserRoleInfo(userRole);
return true;
}
return false;
}
/// <summary>
/// Removes a user from a role
/// </summary>
private bool DeleteUserRole()
{
// Get role and user objects
RoleInfo role = RoleInfoProvider.GetRoleInfo("MyNewRole", SiteContext.CurrentSiteID);
UserInfo user = UserInfoProvider.GetUserInfo("MyNewUser");
if ((role != null) && (user != null))
{
// Get the user role
UserRoleInfo deleteRole = UserRoleInfoProvider.GetUserRoleInfo(user.UserID, role.RoleID);
// Delete the user role
UserRoleInfoProvider.DeleteUserRoleInfo(deleteRole);
return true;
}
return false;
}