public async Task <UserDTO> UpdateUser(UserToUpdateDTO userToUpdate, int userId) { User user = await _userRepo.GetFromIdAsync(userId); if (user == null) { return(null); } //Add check for empty strings if (userToUpdate.Password != null) { byte[] passwordHash, passwordSalt; CreatePasswordHash(userToUpdate.Password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; } if (userToUpdate.FirstName != null) { user.FirstName = userToUpdate.FirstName; } if (userToUpdate.LastName != null) { user.LastName = userToUpdate.LastName; } if (userToUpdate.Phone != null) { user.Phone = userToUpdate.Phone; } if (userToUpdate.Email != null && IsValidEmail(userToUpdate.Email)) { user.Email = userToUpdate.Email; } if (userToUpdate.PictureURL != null) { user.PictureURL = userToUpdate.PictureURL; } if (userToUpdate.Description != null) { user.Description = userToUpdate.Description; } return(ConvertUser(await _genericRepo.UpdateAsync(user))); }
public async Task <IActionResult> Post(UserToUpdateDTO userToUpdate) { userToUpdate.FirstName = _javaScriptEncoder.Encode(_htmlEncoder.Encode(userToUpdate.FirstName)); userToUpdate.LastName = _javaScriptEncoder.Encode(_htmlEncoder.Encode(userToUpdate.LastName)); userToUpdate.Phone = _javaScriptEncoder.Encode(_htmlEncoder.Encode(userToUpdate.Phone)); userToUpdate.Email = _javaScriptEncoder.Encode(_htmlEncoder.Encode(userToUpdate.Email)); userToUpdate.Username = _htmlEncoder.Encode(_javaScriptEncoder.Encode(userToUpdate.Username)); userToUpdate.Description = _htmlEncoder.Encode(_javaScriptEncoder.Encode(userToUpdate.Description)); if (userToUpdate.Email != null && !_userService.IsValidEmail(userToUpdate.Email)) { return(StatusCode(400, "Thats not an email address...")); } int userId = Int32.Parse(this.User.FindFirstValue(ClaimTypes.NameIdentifier)); if (await _userService.UpdateUser(userToUpdate, userId) == null) { return(StatusCode(500, "kaka")); } return(StatusCode(200)); }
public async Task <IActionResult> UpdateUserData(UserToUpdateDTO user) { if (user.Id == null) { return(BadRequest()); } var userToUpdate = await _userManager.FindByIdAsync(user.Id); if (userToUpdate == null) { return(BadRequest()); } userToUpdate.FirstName = user.FirstName; userToUpdate.LastName = user.LastName; userToUpdate.PhoneNumber = user.PhoneNumber; try { if (!(await _userManager.IsInRoleAsync(userToUpdate, "Employee"))) { _userManager.AddToRoleAsync(userToUpdate, "Employee").Wait(); } if (user.IsManager) { _userManager.AddToRoleAsync(userToUpdate, "Manager").Wait(); //add to all categories } else { if (await _userManager.IsInRoleAsync(userToUpdate, "Manager")) { _userManager.RemoveFromRoleAsync(userToUpdate, "Manager").Wait(); } } if (user.IsTeamLeader) { _userManager.AddToRoleAsync(userToUpdate, "TeamLeader").Wait(); } else { if (await _userManager.IsInRoleAsync(userToUpdate, "TeamLeader")) { _userManager.RemoveFromRoleAsync(userToUpdate, "TeamLeader").Wait(); } } } catch (Exception) { return(BadRequest()); } try { await _userCategoryRepo.Delete(uc => uc.UserId == user.Id); if (user.Categories != null && user.Categories.Count > 0) { foreach (int categoryId in user.Categories) { _userCategoryRepo.Add(new UserCategory() { CategoryId = categoryId, UserId = user.Id }); } await _userCategoryRepo.SaveAll(); } } catch (Exception) { return(BadRequest("Error when assigning user to categories")); } var result = await _userManager.UpdateAsync(userToUpdate); if (result.Succeeded) { return(Ok(_mapper.Map <UserWithIncludingsVM>(await _userService.GetUserWithAllIncludings(userToUpdate.Id)))); } else { return(BadRequest()); } }