public async Task <IActionResult> Edit(UserForEdit userForEdit)
        {
            if (userForEdit != null && ModelState.IsValid)
            {
                IdentityResult result = await _dataProcessingConveyor.EditUserAsync(userForEdit);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Login", "Account"));
                }
                else
                {
                    string errorText, key;
                    foreach (var error in result.Errors)
                    {
                        errorText = error.Description;
                        key       = String.Empty;
                        if (error.Code == "DuplicateUserName")
                        {
                            errorText = "Имя уже занято, выберете другое";
                            key       = "Name";
                        }
                        ModelState.AddModelError(key, errorText);
                    }
                    return(View(userForEdit));
                }
            }
            return(View(userForEdit));
        }
Exemple #2
0
        public async Task <IdentityResult> EditUserAsync(UserForEdit UserForEdit)
        {
            User user = await _userManager.FindByIdAsync(UserForEdit.Id);

            if (user == null)
            {
                user = await _userManager.FindByNameAsync(UserForEdit.Id);
            }
            IdentityResult result = new IdentityResult();

            try
            {
                if (user != null)
                {
                    user.Email    = UserForEdit.Email;
                    user.UserName = UserForEdit.Name;
                    result        = await _userManager.UpdateAsync(user);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Fail edit user");
            }
            return(result);
        }
        public async Task <UserForEdit> GetCurrentUserDto()
        {
            var currentUser = await GetCurrentUser();

            var currentUserDto = new UserForEdit();

            _mapper.Map(currentUser, currentUserDto);
            return(currentUserDto);
        }
        public async Task <bool> EditUser(UserForEdit userForEdit)
        {
            var currentUser = await GetCurrentUser();

            _mapper.Map(userForEdit, currentUser);
            var updateResult = await _userManager.UpdateAsync(currentUser);

            return(updateResult.Succeeded);
        }
Exemple #5
0
        public async Task <IdentityResult> EditUserAsync(UserForEdit userForEdit)
        {
            IdentityResult result = await _fcdUserAndSignManager.EditUserAsync(userForEdit);

            if (result.Succeeded)
            {
                _cacheService.Remove("UserList");
            }
            return(result);
        }
Exemple #6
0
        public async Task <IActionResult> Delete(string id, int currentPosition)
        {
            _session.SetString("CurrentPositionUser", currentPosition.ToString());
            UserForEdit deleteUser = await _dataProcessingConveyor.GetUserForEditAsync(id);

            if (deleteUser != null)
            {
                return(View(deleteUser));
            }
            return(NotFound());
        }
        public async Task <IActionResult> Edit()
        {
            if (User.Identity.IsAuthenticated)
            {
                UserForEdit editUserViewModel = await _dataProcessingConveyor.GetUserForEditAsync(User.Identity.Name);

                if (editUserViewModel != null)
                {
                    return(View(editUserViewModel));
                }
            }
            return(NotFound());
        }
        public async Task <IActionResult> Delete()
        {
            if (User.Identity.IsAuthenticated)
            {
                UserForEdit deleteUser = await _dataProcessingConveyor.GetUserForEditAsync(User.Identity.Name);

                if (deleteUser != null)
                {
                    return(View(deleteUser));
                }
            }
            return(NotFound());
        }
        public async Task <IActionResult> UpdateUser(int id, UserForEdit userForEdit)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(userForEdit, userFromRepo);
            await _repo.SaveAll();

            return(NoContent());
        }
Exemple #10
0
        public async Task <IActionResult> DroppingPassword(string id, int currentPosition)
        {
            _session.SetString("CurrentPositionUser", currentPosition.ToString());
            if (id != null)
            {
                UserForEdit deleteUser = await _dataProcessingConveyor.GetUserForEditAsync(id);

                if (deleteUser != null)
                {
                    return(View(deleteUser));
                }
            }
            return(RedirectToAction("UsersView", "Admin"));
        }
Exemple #11
0
        public async Task <IActionResult> SetRoles(string id, int currentPosition)
        {
            _session.SetString("CurrentPositionUser", currentPosition.ToString());
            if (id != null)
            {
                UserForEdit UserForEdit = await _dataProcessingConveyor.GetUserForEditRolesAsync(id);

                if (UserForEdit != null)
                {
                    return(View(UserForEdit));
                }
            }
            return(NotFound());
        }
        public async Task <IActionResult> UpdateUser(int id, UserForEdit user)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userToUpdate = await _userRepository.GetById(id);

            _mapper.Map(user, userToUpdate);

            if (await _userRepository.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception($"Update Operation for user id : {id} failed");
        }
Exemple #13
0
        public async Task <UserForEdit> GetUserForEditAsync(string userName)
        {
            var user = await _userManager.FindByNameAsync(userName);

            if (user == null)
            {
                user = await _userManager.FindByIdAsync(userName);
            }
            UserForEdit userForEdit = new UserForEdit();

            if (user != null)
            {
                userForEdit.Id    = user.Id;
                userForEdit.Name  = user.UserName;
                userForEdit.Email = user.Email;
            }
            return(userForEdit);
        }
        public IActionResult EditUser([FromRoute] Guid userId, [FromBody] UserForEdit dto)
        {
            if (!(null != dto))
            {
                return(BadRequest());
            }
            if (string.IsNullOrWhiteSpace(dto.FirstName) || string.IsNullOrWhiteSpace(dto.LastName))
            {
                return(UnprocessableEntity(ModelState));
            }

            if (dto.FirstName == "John")
            {
                ModelState.AddModelError(nameof(dto.FirstName), "я заебался");
            }

            if (dto.LastName == "Doe")
            {
                ModelState.AddModelError(nameof(dto.LastName), "я заебался еще больбше");
            }

            if (ModelState.IsValid != true)
            {
                return(UnprocessableEntity(ModelState));
            }
            dto.Id = userId;
            var userEntity = _mapper.Map <UserEntity>(dto);

            if (userId == Guid.Empty)
            {
                return(BadRequest());
            }
            _userRepository.UpdateOrInsert(userEntity, out var isInserted);
            if (isInserted != false) // гы)
            {
                return(CreatedAtRoute(
                           nameof(GetUserById),
                           new { userId = userId },
                           userId));
            }
            return(NoContent());
        }
Exemple #15
0
        public async Task <UserForEdit> GetUserForEditRolesAsync(string userId)
        {
            var user = await _userManager.FindByIdAsync(userId);

            UserForEdit userForEdit = new UserForEdit();

            if (user != null)
            {
                var roles = await _userManager.GetRolesAsync(user);

                var allRoles = await _roleManager.Roles.ToListAsync();

                userForEdit.Id       = user.Id;
                userForEdit.Name     = user.UserName;
                userForEdit.Email    = user.Email;
                userForEdit.Roles    = roles;
                userForEdit.AllRoles = allRoles;
            }
            return(userForEdit);
        }
Exemple #16
0
        public async Task <object> UpdateUser(int id, UserForEdit userForEdit)
        {
            // Get the post content in DB
            var existingUser = await _context.Users
                               .Where(u => u.Id == id)
                               .FirstOrDefaultAsync();

            if (existingUser == null)
            {
                return(new { status = "Failure" });
            }
            else
            {
                existingUser.name             = userForEdit.name;
                existingUser.username         = userForEdit.username;
                existingUser.avatarBackground = userForEdit.avatarBackground;

                await _context.SaveChangesAsync();

                var updatedUser = await _context.Users
                                  .Where(u => u.Id == id)
                                  .Include(u => u.Posts)
                                  // .Include(u => u.FollowedBy)
                                  .Select(u => new
                {
                    Id             = u.Id,
                    name           = u.name,
                    username       = u.username,
                    userBackground = u.avatarBackground,
                    // followCount = u.FollowedBy.Count(),
                    postCount = u.Posts.Count()
                })
                                  .FirstOrDefaultAsync();

                return(updatedUser);
            }
        }
Exemple #17
0
        public async Task <IActionResult> EditUser(UserForEdit userForEdit)
        {
            var result = await _accountService.EditUser(userForEdit);

            return(Ok(result));
        }
Exemple #18
0
        public async Task <object> UpdateUser(int id, [FromBody] UserForEdit userEditObj)
        {
            var user = await _userService.UpdateUser(id, userEditObj);

            return(user);
        }