添加多个用户创建多个角色

Adding multiple users creates multiple roles

我有这个解决方案可以将单个用户输入我的应用程序,但现在我们需要合并所有(超过 2k)用户,我有以下代码从模型创建单个用户。 单用户条目。

非常适合单用户使用。

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 循环示例,它为每个用户创建了一个新角色,不好,因为我很高兴我正在使用开发数据库。

可以添加多个用户,但需要为每个用户添加一个角色

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);