Exemple #1
0
        public async Task AddAsync(AdminAddUser model)
        {
            if (model.Password != model.ConfirmPassword)
            {
                throw new ApiException("Пароли не совпадают", 400);
            }

            if (model.Role != Roles.ADMIN && model.Role != Roles.CLIENT && model.Role != Roles.MANAGER)
            {
                throw new ApiException("Роль не существует", 400);
            }

            ApplicationUser user = _mapper.Map <ApplicationUser>(model);

            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                result = await _userManager.AddToRoleAsync(user, model.Role);

                if (result.Succeeded)
                {
                    result = await _userManager.AddPasswordAsync(user, model.Password);

                    if (!result.Succeeded)
                    {
                        throw new ApiException(result.Errors.First().Description, 400);
                    }
                }
            }
        }
Exemple #2
0
        private void button4_Click(object sender, EventArgs e)
        {
            // Dodaj Uzytkownika

            AdminAddUser x = new AdminAddUser();

            x.Show();
        }
Exemple #3
0
        public async Task UpdateAsync(string id, AdminAddUser model)
        {
            var user = await GetUser(id);

            if (model.Role != Roles.ADMIN && model.Role != Roles.CLIENT && model.Role != Roles.MANAGER)
            {
                throw new ApiException("Роль не существует", 400);
            }

            user = _mapper.Map(model, user);

            IdentityResult result;

            if (model.Email != user.Email)
            {
                result = await _userManager.SetEmailAsync(user, model.Email);

                if (!result.Succeeded)
                {
                    throw new ApiException(result.Errors.First().Description, 400);
                }
                result = await _userManager.SetUserNameAsync(user, model.Email);

                if (!result.Succeeded)
                {
                    throw new ApiException(result.Errors.First().Description, 400);
                }
            }

            var roles = await _userManager.GetRolesAsync(user);

            await _userManager.RemoveFromRolesAsync(user, roles);

            result = await _userManager.AddToRoleAsync(user, model.Role);

            if (!result.Succeeded)
            {
                throw new ApiException(result.Errors.First().Description, 400);
            }
        }
Exemple #4
0
 public void AddUserInfo(AdminAddUser userProfile)
 {
     if (ModelState.IsValid)
     {
         //Yes, these try/catches I have through out don't do anything. You can either take them out in your code, or do something in the catch part.
         //Most likely you will be doing some kind of logging and beautifying the error before you send it back to the user.
         try
         {
             //TODO: Check for the existence of the user ID already before trying to add it
             WebSecurity.CreateUserAndAccount(userProfile.UserName, userProfile.Password, new { IsActive = userProfile.IsActive, UserEmail = userProfile.UserEmail });
         }
         catch (Exception)
         {
             throw;
         }
     }
     else
     {
         string errors = ModelState.Values.SelectMany(modelState => modelState.Errors).Aggregate(string.Empty, (current, error) => current + (error.ErrorMessage + Environment.NewLine));
         throw new Exception(errors);
     }
 }
        public async Task <IActionResult> Update([FromRoute] string id, [FromBody] AdminAddUser model)
        {
            await _adminService.UpdateAsync(id, model);

            return(Ok());
        }
        public async Task <IActionResult> Add([FromBody] AdminAddUser model)
        {
            await _adminService.AddAsync(model);

            return(Ok());
        }