예제 #1
0
        public async Task <IActionResult> CreateUser(CreateUpdateUserViewModel updateUserModel)
        {
            var user = new ApplicationUser
            {
                Email     = updateUserModel.Email,
                FirstName = updateUserModel.FirstName,
                LastName  = updateUserModel.LastName,
                UserName  = updateUserModel.Email
            };

            var createdUser = _identityService.CreateUserAsync(user, updateUserModel.Password).Result;

            if (!createdUser.result.Succeeded)
            {
                return(View(updateUserModel));
            }

            var addedRole = await _identityService.AddRoleToUser(createdUser.userId, updateUserModel.RoleName);

            if (addedRole.Succeeded)
            {
                return(RedirectToAction("Index"));
            }

            return(View(updateUserModel));
        }
예제 #2
0
        public async Task <ActionResult <UserViewModel> > Post(CreateUpdateUserViewModel model)
        {
            var result = await userRepository.Add(new User(model.Username, model.Password.ComputeSha256Hash(), model.Fullname));

            await userRepository.SaveChanges();

            return(CreatedAtAction(nameof(GetById), new { id = result.Id }, result.ToViewModel()));
        }
예제 #3
0
        public async Task <IActionResult> UpdateUserPost(CreateUpdateUserViewModel updateUserViewModel)
        {
            var user = _mapper.Map <UpdateApplicationUser>(updateUserViewModel);

            var result = await _identityService.UpdateUser(user);

            if (result.Succeeded)
            {
                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("UpdateUser", updateUserViewModel.Id));
        }
예제 #4
0
        public async Task <ActionResult <UserViewModel> > Update(int id, CreateUpdateUserViewModel model)
        {
            var result = await userRepository.Find(id);

            if (result == null)
            {
                return(NotFound());
            }
            if (result.Id == -1)
            {
                return(BadRequest("You can not update admin user"));
            }
            result.SetUsername(model.Username).SetPassword(model.Password.ComputeSha256Hash()).SetFullname(model.Fullname);
            result = userRepository.Update(result);
            await userRepository.SaveChanges();

            return(Ok(result.ToViewModel()));
        }
        public async Task <IActionResult> CreateUpdateUser(string id, [FromBody] CreateUpdateUserViewModel user)
        {
            ApplicationUser appUser = await _accountManager.GetUserByIdAsync(id);

            string[] currentRoles = appUser != null ? (await _accountManager.GetUserRolesAsync(appUser)).ToArray() : null;

            var manageUsersPolicy = _authorizationService.AuthorizeAsync(this.User, id, AccountManagementOperations.Update);

            //var assignRolePolicy = _authorizationService.AuthorizeAsync(this.User, (user.Roles, currentRoles), Authorization.Policies.AssignAllowedRolesPolicy);


            // if ((await Task.WhenAll(manageUsersPolicy, assignRolePolicy)).Any(r => !r.Succeeded))
            //     return new ChallengeResult();


            if (ModelState.IsValid)
            {
                // if (user == null)
                //     return BadRequest($"{nameof(user)} cannot be null");

                // if (!string.IsNullOrWhiteSpace(user.Id) && id != user.Id)
                //     return BadRequest("Conflicting user id in parameter and model data");

                // if (appUser == null)
                //     return NotFound(id);

                // bool isPasswordChanged = !string.IsNullOrWhiteSpace(user.NewPassword);
                // bool isUserNameChanged = !appUser.UserName.Equals(user.UserName, StringComparison.OrdinalIgnoreCase);

                if (WebApi.Helpers.Utilities.GetUserId(this.User) == id)
                {
                    // if (string.IsNullOrWhiteSpace(user.CurrentPassword))
                    // {
                    //     if (isPasswordChanged)
                    //         AddError("Current password is required when changing your own password", "Password");

                    //     if (isUserNameChanged)
                    //         AddError("Current password is required when changing your own username", "Username");
                    // }
                    // else if (isPasswordChanged || isUserNameChanged)
                    // {
                    //     if (!await _accountManager.CheckPasswordAsync(appUser, user.CurrentPassword))
                    //         AddError("The username/password couple is invalid.");
                    // }
                }

                // if (ModelState.IsValid)
                // {
                //     _mapper.Map<CreateUpdateUserViewModel, ApplicationUser>(user, appUser);

                //     var result = await _accountManager.UpdateUserAsync(appUser, user.Roles);
                //     if (result.Succeeded)
                //     {
                //         if (isPasswordChanged)
                //         {
                //             if (!string.IsNullOrWhiteSpace(user.CurrentPassword))
                //                 result = await _accountManager.UpdatePasswordAsync(appUser, user.CurrentPassword, user.NewPassword);
                //             else
                //                 result = await _accountManager.ResetPasswordAsync(appUser, user.NewPassword);
                //         }

                //         if (result.Succeeded)
                //             return NoContent();
                //     }

                //     AddError(result.Errors);
                // }
            }

            return(BadRequest(ModelState));
        }