添加多个用户会创建多个角色
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了添加多个用户会创建多个角色相关的知识,希望对你有一定的参考价值。
我有这个解决方案将单个用户输入我的应用程序,但现在我们需要合并所有(超过2k)我们的用户,我有以下代码从Model创建一个用户。单用户输入。
适合单个用户使用。
public interface ICreateUser
{
void Create(CreateUserModel model);
}
public class CreateUser : ICreateUser
{
private readonly IUserManagementService _userManagementService;
private readonly IRoleService _roleService;
private readonly IAuthorisationService _authorisationService;
private readonly IPasswordManagementService _passwordManagementService;
public CreateUser(IUserManagementService userManagementService, IRoleService roleService,
IAuthorisationService authorisationService, IPasswordManagementService passwordManagementService)
{
_userManagementService = userManagementService;
_roleService = roleService;
_authorisationService = authorisationService;
_passwordManagementService = passwordManagementService;
}
public void Create(CreateUserModel model)
{
var user = new User
{
Email = model.AdminEmail,
FirstName = newUser.FirstName,
LastName = newUser.LastName,
IsActive = true
};
_passwordManagementService.SetPassword(user, model.AdminPassword, model.ConfirmPassword);
_userManagementService.AddUser(user);
CurrentRequestData.CurrentUser = user;
var memberUserRole = new UserRole
{
Name = UserRole.Member
};
user.Roles = new HashSet<UserRole> { memberUserRole };
memberUserRole.Users = new HashSet<User> { user };
_roleService.SaveRole(memberUserRole);
_authorisationService.Logout();
_authorisationService.SetAuthCookie(user, true);
}
}
我尝试修改此项以添加多个用户失败,我使用下面的用户列表作为示例,但将来会从用户的电子表格中获取用户。下面是每个用户的foreach循环示例,它为每个用户创建了一个新角色,不好,因为我很高兴我正在使用dev数据库。
添加多个用户的工作,但为每个用户增加了一个角色
public void Create()
{
IList<NewUser> newUserList = new List<NewUser>() {
new NewUser(){ Email = "Someuser@yahoo.com", FirstName = "Some", LastName = "User", Password = "Password", PasswordConfirm = "Password" },
new NewUser(){ Email = "Someuser@eaglemg.com", FirstName = "Some", LastName = "User", Password = "Password", PasswordConfirm = "Password" },
new NewUser(){ Email = "Someuser@aol.com", FirstName = "Some", LastName = "User", Password = "Password", PasswordConfirm = "Password" },
new NewUser(){ Email = "Someuser@hotmail.com", FirstName = "Some", LastName = "User", Password = "Password", PasswordConfirm = "Password" },
new NewUser(){ Email = "Someuser@comcast.net", FirstName = "Some", LastName = "User", Password = "Password", PasswordConfirm = "Password" },
new NewUser(){ Email = "Someuser@aol.com", FirstName = "Some", LastName = "User", Password = "Password", PasswordConfirm = "Password" },
new NewUser(){ Email = "Someuser@verizon.net", FirstName = "Some", LastName = "User", Password = "Password", PasswordConfirm = "Password" }
};
foreach (NewUser newUser in newUserList)
{
var user = new User
{
Email = newUser.Email,
FirstName = newUser.FirstName,
LastName = newUser.LastName,
IsActive = true
};
_passwordManagementService.SetPassword(user, newUser.Password, newUser.PasswordConfirm);
_userManagementService.AddUser(user);
CurrentRequestData.CurrentUser = user;
var memberUserRole = new UserRole
{
Name = UserRole.Member
};
user.Roles = new HashSet<UserRole> { memberUserRole };
memberUserRole.Users = new HashSet<User> { user };
_roleService.SaveRole(memberUserRole);
_authorisationService.Logout();
_authorisationService.SetAuthCookie(user, true);
}
}
我的下一次尝试是我遇到问题的地方,我似乎无法弄清楚在创建后用现有角色更新用户的代码,请参见下文。
foreach (NewUser newUser in newUserList)
{
var user = new User
{
Email = newUser.Email,
FirstName = newUser.FirstName,
LastName = newUser.LastName,
IsActive = true
};
_passwordManagementService.SetPassword(user, newUser.Password, newUser.PasswordConfirm);
_userManagementService.AddUser(user);
CurrentRequestData.CurrentUser = user;
}
var roleName = "Member";
if (_roleService.GetRoleByName(roleName) != null)
{
//NEED HELP HERE
var umpireUserRole = new UserRole
{
Name = UserRole.Umpire
};
user.Roles = new HashSet<UserRole> { umpireUserRole };
umpireUserRole.Users = new HashSet<User> { user };
_roleService.SaveRole(umpireUserRole);
//END OF NEED HELP
}
else
{
var umpireUserRole = new UserRole
{
Name = UserRole.Umpire
};
user.Roles = new HashSet<UserRole> { umpireUserRole };
umpireUserRole.Users = new HashSet<User> { user };
_roleService.SaveRole(umpireUserRole);
}
正如你所看到的,我在这里有点困惑,任何帮助将不胜感激。
提前致谢
答案
您正在为现有角色创建新实例,这意味着NHibernate会将其视为新对象。你应该重用加载的对象:
var umpireUserRole =
_roleService.GetRoleByName(roleName)
?? new UserRole
{
Name = UserRole.Umpire,
Users = new HashSet<User>()
};
umpireUserRole.Users.Add(user);
user.Roles = new HashSet<UserRole> { umpireUserRole };
_roleService.SaveRole(umpireUserRole);
以上是关于添加多个用户会创建多个角色的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 SpriteKit update() 函数会创建多个 NSTimers?
为啥 System.Timers.Timer 在触发 Elapsed 事件时会创建多个线程?
在 UIPageViewController 中滚动多个页面会创建白色闪光