Krishnan Suba

Krishnan Suba

  • 1.7k
  • 82
  • 15k

How to assign the multiple specific roles in single user?

Sep 3 2019 3:51 AM
In kentico 10 we using bulk user upload from excel, the field name is Username, Email, fname, lname, Role1, Role2 and Role3.
 
The user will be insert working fine.
 
But, Roles are not insert. If I debug the code, the cursor not going to inside the if condition.
 
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);
UserInfoProvider.SetUserInfo(newUser);
UserInfoProvider.AddUserToSite(newUser.UserName, site.SiteName);
// var rolelist = userDto.Role1 ?? $"{userDto.Role2} {userDto.Role3}";
var rolelist = new[] { $"{userDto.Role1}", userDto.Role2, userDto.Role3}
.Where(af => !string.IsNullOrWhiteSpace(af));
foreach (var u in rolelist){
List<string> userRoles = new List<string> { userDto.Role1, userDto.Role2, userDto.Role3 };
foreach (string r in userRoles){
var roles = RoleInfoProvider.GetRoles().WhereEquals("LOWER(RoleDescription)", r.ToLower()).FirstOrDefault();
if (userRoles != null){
UserInfoProvider.AddUserToRole(newUser.UserID, roles.RoleID);
}}}
return newUser;}

Answers (1)