How to assign the multiple roles in single user?

Ramakrishnan G asked on August 29, 2019 16:52

In kentico 10 we using bulk user upload using excel, the field name is Username, Email, fname, lname, Role1, Role2 and Role3.

The username, email,fname, lname saving the data in db its working fine.

How to assign the 3 roles in single user. I using below code var newUser = new UserInfo{ UserName = userDto.UserName,
FirstName = userDto.FirstName, LastName = userDto.LastName,
Email = userDto.Email, SiteIndependentPrivilegeLevel = CMS.Base.UserPrivilegeLevelEnum.None }; var newUserSettings = newUser.UserSettings ?? new UserSettingsInfo(); UserInfoProvider.SetUserInfo(newUser); UserInfoProvider.AddUserToSite(newUser.UserName, site.SiteName); var role = new RoleProvider().GetAllRoles(site.SiteID) .FirstOrDefault(r => r.Description == userDto.Role1); // .(r => r.Description == userDto.Role1, userDto.Role2, userDto.Role3);
UserInfoProvider.AddUserToRole(newUser.UserID, role.ID); return newUser;

Recent Answers


Brenden Kehren answered on August 29, 2019 17:36

What I would do is create a list of type string for the role names. During the user creation/update, loop through the list of role names, look the role name up by name or code name, if it exists, assign the user you're creating/updating to that role.

foreach (User u in userList)
{
    List<string> userRoles = new List<string> { u.Role1, u.Role2, u.Role3 };
    foreach (string r in userRoles
    {
        var role = RoleInfoProvider.GetRoles().WhereEquals("LOWER(RoleDescription)", r.ToLower()).FirstOrDefault();
        if (role != null)
        {
            UserInfoProvider.AddUserToRole(u.UserID, role.ID);
        }
    }
}

Just typed this off the cuff so you'll have to correct any mistakes or API calls but this should get you in the right direction.

1 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on August 29, 2019 17:47 (last edited on August 29, 2019 17:50)

Just follow the example:

// Gets the user
UserInfo user = UserInfoProvider.GetUserInfo("Username");

    // Gets the role
    RoleInfo role = RoleInfoProvider.GetRoleInfo("Rolename", SiteContext.CurrentSiteName);

    if ((user != null) && (role != null))
    {
        // Adds the user to the role
        UserInfoProvider.AddUserToRole(user.UserName, role.RoleName, SiteContext.CurrentSiteName);
    }
0 votesVote for this answer Mark as a Correct answer

Ramakrishnan G answered on August 30, 2019 13:40

I used below code for Assign the specify roles to user from excel. Its not working

private UserInfo CreateUser(UserDto userDto, SiteInfo site) { var newUser = new UserInfo { UserName = userDto.UserName, UserEnabled = true, FirstName = userDto.FirstName, LastName = userDto.LastName, FullName = userDto.FirstName + " " + userDto.LastName, Email = userDto.Email, SiteIndependentPrivilegeLevel = CMS.Base.UserPrivilegeLevelEnum.None }; var newUserSettings = newUser.UserSettings ?? new UserSettingsInfo(); UserInfoProvider.SetPassword(newUser, userDto.UserPassword); newUserSettings.UserPhone = userDto.PhoneNumber; //var role = new RoleProvider().GetAllRoles(site.SiteID) // .FirstOrDefault(r => r.Description == userDto.Role1); var userList = GetDtosFromExcelRows

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.