Adding multiple users creates multiple roles

时间:2019-04-17 01:38:43

标签: c# asp.net-mvc foreach nhibernate hashset

I have this solution to enter single Users into my application but now we need to incorporate all(over 2k) our users, I have the following code that creates a single user from Model. Single user Entry.

WORKS GREAT FOR SINGLE USER.

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

My attempt to modify this to add multiple users have failed, I have used a list of users below as an example but will be taking users from a spreadsheet in the future. Below is an example of the foreach loop on each user which had created a new Role for each user, Not good as I am glad I'm working with a dev database.

WORKS FOR ADDING MULTIPLE USERS BUT ADDS A ROLE FOR EACH USER

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

My next attempt is where I am having issues, I can not seem to figure out the code to update the user with existing role once created, See Below.

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

        }

As you can see I am slightly confused here, Any help would be greatly appreciated.

Thanks in advance

1 个答案:

答案 0 :(得分:1)

您正在为现有角色创建新实例,这意味着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);