示例#1
0
        public CorporationUserCreatedConfirmation CreateUser(Corporation user, string password)
        {
            if (!CheckUniqueUsername(user.Username, false, null))
            {
                //Unique violation
                throw new UniqueValueViolationException("Username should be unique");
            }
            if (!CkeckUniqueEmail(user.Email))
            {
                throw new UniqueValueViolationException("Email should be unique");
            }
            if (!CheckCity(user.CityId))
            {
                throw new ForeignKeyConstraintViolationException("Foreign key constraint violated");
            }
            CorporationUserCreatedConfirmation userCreated = _corporationUserRepository.CreateUser(user);

            _corporationUserRepository.SaveChanges();

            //Adding to identityuserdbcontext tables
            string         username = string.Join("", user.Username.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
            var            acc      = new AccountInfo(username, user.Email, userCreated.UserId);
            IdentityResult result   = _userManager.CreateAsync(acc, password).Result;

            if (result.Succeeded)
            {
                _userManager.AddToRoleAsync(acc, "Regular user").Wait();
            }
            else
            {
                _corporationUserRepository.DeleteUser(userCreated.UserId);
                //throw new ExectionException("Erorr trying to create user");
            }
            return(userCreated);
        }