public async Task <IdentityResult> UpdatePasswordIfAvailable(AppUser entity, UpdateAppUserModel model) { if (!string.IsNullOrWhiteSpace(model.PasswordReset)) { var resetPassToken = await _userManager.GeneratePasswordResetTokenAsync(entity); var result = await _userManager.ResetPasswordAsync(entity, resetPassToken, model.PasswordReset); return(result); } return(null); }
public ValidationData ValidateUpdateAppUser(ClaimsPrincipal principal, AppUser entity, UpdateAppUserModel model) { var validationData = new ValidationData(); if (entity.UserRoles.First().Role.Name == Data.Constants.RoleName.ADMIN && principal.Identity.Name != entity.Id) { validationData = validationData.Fail(code: AppResultCode.AccessDenied); } if (!string.IsNullOrWhiteSpace(model.PasswordReset) && model.PasswordReset.Length < 6) { validationData = validationData.Fail("Invalid password reset", code: Constants.AppResultCode.FailValidation); } return(validationData); }
public async Task <IActionResult> Update(string id, UpdateAppUserModel model) { var entity = _service.Users.Id(id).FirstOrDefault(); if (entity == null) { return(NotFound(AppResult.NotFound())); } var validationData = _service.ValidateUpdateAppUser(User, entity, model); if (!validationData.IsValid) { return(BadRequest(AppResult.FailValidation(data: validationData))); } _service.UpdateAppUser(entity, model); context.SaveChanges(); var result = await _service.UpdatePasswordIfAvailable(entity, model); if (!result.Succeeded) { throw new Exception("Error change password"); } return(NoContent()); }
public void UpdateAppUser(AppUser entity, UpdateAppUserModel model) { model.CopyTo(entity); PrepareUpdate(entity); }