Academatica.Api/Academatica.Api.Users/Controllers/UsersController.cs
/// <summary>
/// Endpoint used to perform pasword reset for specified user with a confirmation code.
/// </summary>
/// <param name="id">User ID.</param>
/// <param name="restorePasswordRequestDto">New password, new password confirmation and confirmation code.</param>
Endpoint used to perform pasword reset for specified user with a confirmation code.
Endpoint used to perform pasword reset for specified user with a confirmation code .
[HttpPatch]
        [Route("{id}/password/restore")]
        public async Task<IActionResult> RestoreUserPassword(Guid id, [FromBody] UserRestorePasswordRequestDto restorePasswordRequestDto)
        {
            if (ModelState.IsValid)
            {
                var userId = User.FindFirst("sub")?.Value;
                if (userId != id.ToString())
                {
                    return Forbid();
                }
                var user = _academaticaDbContext.Users.Where(x => x.Id == id).FirstOrDefault();
                if (user == null)
                {
                    return NotFound("Invalid user ID.");
                }
                List<string> passwordValidationErrors = new List<string>();
                var validators = _userManager.PasswordValidators;
                foreach (var validator in validators)
                {
                    var result = await validator.ValidateAsync(_userManager, null, restorePasswordRequestDto.NewPassword);
                    if (!result.Succeeded)
                    {
                        foreach (var error in result.Errors)
                        {
                            passwordValidationErrors.Add(error.Description);
                        }
                    }
                }
                if (passwordValidationErrors.Count != 0)
                {
                    return BadRequest(string.Join("; ", passwordValidationErrors));
                }
                if (string.IsNullOrEmpty(restorePasswordRequestDto.ConfirmationCode))
                {
                    return BadRequest("Confirmation code was null.");
                }
                else
                {
                    var cachedCode = await _confirmationCodeManager.GetPasswordConfirmationCode(user.Id);
                    if (string.IsNullOrEmpty(cachedCode))
                    {
                        return StatusCode(500, "Code cache was null.");
                    }
                    else
                    {
                        if (cachedCode != restorePasswordRequestDto.ConfirmationCode)
                        {
                            return Ok(new UserChangeEmailResponseDto
                            {
                                ConfirmationPending = false,
                                Success = false,
                                Error = "Invalid confirmation code."
                            });
                        }
                        var code = await _userManager.GeneratePasswordResetTokenAsync(user);
                        await _userManager.ResetPasswordAsync(user, code, restorePasswordRequestDto.NewPassword);
                        await _userEmailService.SendPasswordChangeNotification(user);
                        await _confirmationCodeManager.RemovePasswordConfirmationCode(user.Id);
                        return Ok(new UserRestorePasswordResponseDto
                        {
                            ConfirmationPending = false,
                            Success = true
                        });
                    }
                }
            }
            else
            {
                var message = string.Join(" | ", ModelState.Values
                                    .SelectMany(v => v.Errors)
                                    .Select(e => e.ErrorMessage));
                return BadRequest(message);
            }
        }
[ HttpPatch ] [ Route ( " " ) ] public async Task < IActionResult > RestoreUserPassword ( Guid id , [ FromBody ] UserRestorePasswordRequestDto restorePasswordRequestDto ) { if ( ModelState . IsValid ) { var userId = User . FindFirst ( " " ) ? . Value ; if ( userId != id . ToString ( ) ) { return Forbid ( ) ; } var user = _academaticaDbContext . Users . Where ( x => x . Id == id ) . FirstOrDefault ( ) ; if ( user == null ) { return NotFound ( " " ) ; } List < string > passwordValidationErrors = new List < string > ( ) ; var validators = _userManager . PasswordValidators ; foreach ( var validator in validators ) { var result = await validator . ValidateAsync ( _userManager , null , restorePasswordRequestDto . NewPassword ) ; if ( ! result . Succeeded ) { foreach ( var error in result . Errors ) { passwordValidationErrors . Add ( error . Description ) ; } } } if ( passwordValidationErrors . Count != 0 ) { return BadRequest ( string . Join ( " " , passwordValidationErrors ) ) ; } if ( string . IsNullOrEmpty ( restorePasswordRequestDto . ConfirmationCode ) ) { return BadRequest ( " " ) ; } else { var cachedCode = await _confirmationCodeManager . GetPasswordConfirmationCode ( user . Id ) ; if ( string . IsNullOrEmpty ( cachedCode ) ) { return StatusCode ( 500 , " " ) ; } else { if ( cachedCode != restorePasswordRequestDto . ConfirmationCode ) { return Ok ( new UserChangeEmailResponseDto { ConfirmationPending = false , Success = false , Error = " " } ) ; } var code = await _userManager . GeneratePasswordResetTokenAsync ( user ) ; await _userManager . ResetPasswordAsync ( user , code , restorePasswordRequestDto . NewPassword ) ; await _userEmailService . SendPasswordChangeNotification ( user ) ; await _confirmationCodeManager . RemovePasswordConfirmationCode ( user . Id ) ; return Ok ( new UserRestorePasswordResponseDto { ConfirmationPending = false , Success = true } ) ; } } } else { var message = string . Join ( " " , ModelState . Values . SelectMany ( v => v . Errors ) . Select ( e => e . ErrorMessage ) ) ; return BadRequest ( message ) ; } }
Endpoint used to perform pasword reset for specified user with a confirmation code.
Endpoint used to perform pasword reset for specified user with a confirmation code .
id
Guid
None
UserRestorePasswordRequestDto
[FromBody]
restorePasswordRequestDto
New password, new password confirmation and confirmation code.
New password new password confirmation and confirmation code .