// Get role and user objects RoleInfo role = RoleInfoProvider.GetRoleInfo("MyNewRole", CMSContext.CurrentSiteID); UserInfo user = UserInfoProvider.GetUserInfo("MyNewUser");
if ((role != null) && (user != null)) { // Create new user role object UserRoleInfo userRole = newUserRoleInfo(); // Set the properties userRole.UserID = user.UserID; userRole.RoleID = role.RoleID; // Save the user role UserRoleInfoProvider.SetUserRoleInfo(userRole); returntrue; } returnfalse; }
The following example removes a role from a user.
privatebool DeleteUserRole() {
// Get role and user objects RoleInfo role = RoleInfoProvider.GetRoleInfo("MyNewRole", CMSContext.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); returntrue; } returnfalse; }