Exemple #1
0
        public PersonalUserCreatingConfirmation CreateAdmin(PersonalUser user, string password)
        {
            if (!CheckUniqueUsername(user.Username, false, null))
            {
                throw new UniqueValueViolationException("Username should be unique");
            }
            if (!CkeckUniqueEmail(user.Email))
            {
                throw new UniqueValueViolationException("Email should be unique");
            }
            if (!CheckCountry(user.CountryId))
            {
                throw new ForeignKeyConstraintViolationException("Foreign key constraint violated");
            }
            //Dodavanja u userDbContext
            PersonalUserCreatingConfirmation userCreated = _personalUserRepository.CreateAdmin(user);

            _personalUserRepository.SaveChanges();

            //Dodavanje u identityUserDbContext
            string         username = string.Join("", user.Username.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
            var            persuser = new AccountInfo(username, user.Email, userCreated.UserId);
            IdentityResult result   = _userManager.CreateAsync(persuser, password).Result;

            if (result.Succeeded)
            {
                _userManager.AddToRoleAsync(persuser, "Admin").Wait();
            }
            else
            {
                _personalUserRepository.DeleteUser(userCreated.UserId);
            }
            return(userCreated);
        }
        public PersonalUserCreatedConfirmation CreateAdmin(PersonalUser 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");
            }
            //Adding to userdbcontext tables
            PersonalUserCreatedConfirmation userCreated = _personalUserRepository.CreateAdmin(user);

            _personalUserRepository.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, "Admin").Wait();
            }
            else
            {
                _personalUserRepository.DeleteUser(userCreated.UserId);
                throw new ExectionException("Erorr trying to create user");
            }
            return(userCreated);
        }