diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-11-14 07:56:56 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-11-14 08:05:36 +0100 |
| commit | 4b5597b3fe6e02f1655e6a731e83bdcdf1017d63 (patch) | |
| tree | 818580507641787380b58bdcfa7d0ed7026f4e82 /code/api/src | |
| parent | 99b0c09a6bb984d811b63788015cfad1855b5f3c (diff) | |
| download | greatoffice-4b5597b3fe6e02f1655e6a731e83bdcdf1017d63.tar.xz greatoffice-4b5597b3fe6e02f1655e6a731e83bdcdf1017d63.zip | |
refactor: Api files always returns Response
Diffstat (limited to 'code/api/src')
4 files changed, 43 insertions, 59 deletions
diff --git a/code/api/src/Data/Enums/PasswordResetRequestStatus.cs b/code/api/src/Data/Enums/PasswordResetRequestStatus.cs new file mode 100644 index 0000000..5629e6f --- /dev/null +++ b/code/api/src/Data/Enums/PasswordResetRequestStatus.cs @@ -0,0 +1,6 @@ +namespace IOL.GreatOffice.Api.Data.Enums; + +public enum PasswordResetRequestStatus +{ + +}
\ No newline at end of file diff --git a/code/api/src/Endpoints/Internal/PasswordResetRequests/CreateResetRequestRoute.cs b/code/api/src/Endpoints/Internal/PasswordResetRequests/CreateResetRequestRoute.cs index 49df35b..edf825e 100644 --- a/code/api/src/Endpoints/Internal/PasswordResetRequests/CreateResetRequestRoute.cs +++ b/code/api/src/Endpoints/Internal/PasswordResetRequests/CreateResetRequestRoute.cs @@ -1,6 +1,6 @@ namespace IOL.GreatOffice.Api.Endpoints.Internal.PasswordResetRequests; -public class CreateResetRequestRoute : RouteBaseAsync.WithRequest<CreateResetRequestRoute.Payload>.WithActionResult +public class CreateResetRequestRoute : RouteBaseAsync.WithRequest<string>.WithActionResult { private readonly ILogger<CreateResetRequestRoute> _logger; private readonly PasswordResetService _passwordResetService; @@ -12,34 +12,18 @@ public class CreateResetRequestRoute : RouteBaseAsync.WithRequest<CreateResetReq _database = database; } - public class Payload - { - public string Username { get; set; } - } - [AllowAnonymous] [HttpPost("~/_/password-reset-request/create")] - public override async Task<ActionResult> HandleAsync(Payload request, CancellationToken cancellationToken = default) { - if (!request.Username.IsValidEmailAddress()) { - _logger.LogInformation("Username is invalid, not doing request for password change"); - return KnownProblem("Invalid email address", request.Username + " looks like an invalid email address"); - } - + public override async Task<ActionResult> HandleAsync([FromQuery(Name = "for_user")] string username, CancellationToken cancellationToken = default) { var tz = GetRequestTimeZone(_logger); _logger.LogInformation("Creating forgot password request with local date time: " + tz.LocalDateTime.ToString("u")); - try { - var user = _database.Users.SingleOrDefault(c => c.Username.Equals(request.Username)); - if (user != default) { - await _passwordResetService.AddRequestAsync(user, tz.TimeZoneInfo, cancellationToken); - return Ok(); - } + var user = _database.Users.FirstOrDefault(c => c.Username.Equals(username)); + // Don't inform the caller that the user does not exist. + if (user == default) return Ok(); + + await _passwordResetService.AddRequestAsync(user, tz.TimeZoneInfo, cancellationToken); - _logger.LogInformation("User was not found, not doing request for password change"); - return Ok(); - } catch (Exception e) { - _logger.LogError(e, "_/password-reset-request/create threw an exception"); - return Ok(); - } + return Ok(); } }
\ No newline at end of file diff --git a/code/api/src/Endpoints/Internal/PasswordResetRequests/IsResetRequestValidRoute.cs b/code/api/src/Endpoints/Internal/PasswordResetRequests/IsResetRequestValidRoute.cs index 687cef6..1ad0f47 100644 --- a/code/api/src/Endpoints/Internal/PasswordResetRequests/IsResetRequestValidRoute.cs +++ b/code/api/src/Endpoints/Internal/PasswordResetRequests/IsResetRequestValidRoute.cs @@ -8,12 +8,6 @@ public class IsResetRequestValidRoute : RouteBaseAsync.WithRequest<Guid>.WithAct _passwordResetService = passwordResetService; } - /// <summary> - /// Check if a given password reset request is still valid. - /// </summary> - /// <param name="id"></param> - /// <param name="cancellationToken"></param> - /// <returns></returns> [AllowAnonymous] [HttpGet("~/_/password-reset-request/is-valid")] public override async Task<ActionResult> HandleAsync(Guid id, CancellationToken cancellationToken = default) { diff --git a/code/api/src/Utilities/SwaggerGenOptionsExtensions.cs b/code/api/src/Utilities/SwaggerGenOptionsExtensions.cs index 9b70194..a3d9036 100644 --- a/code/api/src/Utilities/SwaggerGenOptionsExtensions.cs +++ b/code/api/src/Utilities/SwaggerGenOptionsExtensions.cs @@ -8,36 +8,36 @@ namespace IOL.GreatOffice.Api.Utilities; public static class SwaggerGenOptionsExtensions { - /// <summary> - /// Updates Swagger document to support ApiEndpoints.<br/><br/> - /// For controllers inherited from <see cref="EndpointBase"/>:<br/> - /// - Replaces action Tag with <c>[namespace]</c><br/> - /// </summary> - public static void UseApiEndpoints(this SwaggerGenOptions options) { - options.TagActionsBy(EndpointNamespaceOrDefault); - } + /// <summary> + /// Updates Swagger document to support ApiEndpoints.<br/><br/> + /// For controllers inherited from <see cref="EndpointBase"/>:<br/> + /// - Replaces action Tag with <c>[namespace]</c><br/> + /// </summary> + public static void UseApiEndpoints(this SwaggerGenOptions options) { + options.TagActionsBy(EndpointNamespaceOrDefault); + } - private static IList<string?> EndpointNamespaceOrDefault(ApiDescription api) { - if (api.ActionDescriptor is not ControllerActionDescriptor actionDescriptor) { - throw new InvalidOperationException($"Unable to determine tag for endpoint: {api.ActionDescriptor.DisplayName}"); - } + private static IList<string?> EndpointNamespaceOrDefault(ApiDescription api) { + if (api.ActionDescriptor is not ControllerActionDescriptor actionDescriptor) { + throw new InvalidOperationException($"Unable to determine tag for endpoint: {api.ActionDescriptor.DisplayName}"); + } - if (actionDescriptor.ControllerTypeInfo.GetBaseTypesAndThis().Any(t => t == typeof(EndpointBase))) { - return new[] { - actionDescriptor.ControllerTypeInfo.Namespace?.Split('.').Last() - }; - } + if (actionDescriptor.ControllerTypeInfo.GetBaseTypesAndThis().Any(t => t == typeof(EndpointBase))) { + return new[] { + actionDescriptor.ControllerTypeInfo.Namespace?.Split('.').Last() + }; + } - return new[] { - actionDescriptor.ControllerName - }; - } + return new[] { + actionDescriptor.ControllerName + }; + } - public static IEnumerable<Type> GetBaseTypesAndThis(this Type type) { - var current = type; - while (current != null) { - yield return current; - current = current.BaseType; - } - } -} + private static IEnumerable<Type> GetBaseTypesAndThis(this Type type) { + var current = type; + while (current != null) { + yield return current; + current = current.BaseType; + } + } +}
\ No newline at end of file |
