diff --git a/PostFundManagement.Api/Program.cs b/PostFundManagement.Api/Program.cs index c44f968..dbac407 100644 --- a/PostFundManagement.Api/Program.cs +++ b/PostFundManagement.Api/Program.cs @@ -29,7 +29,7 @@ builder.Services.AddApiVersioning(options => builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddTransient(); builder.Services.AddEndpoints(Assembly.GetExecutingAssembly()); builder.Services.AddApiServices(builder.Configuration); diff --git a/PostFundManagement.Application/Extensions/General.cs b/PostFundManagement.Application/Extensions/General.cs index c5bab52..ec14c53 100644 --- a/PostFundManagement.Application/Extensions/General.cs +++ b/PostFundManagement.Application/Extensions/General.cs @@ -1,19 +1,50 @@ -using PostFundManagement.Application.Abstractions; -using PostFundManagement.Domain.Abstractions; - -namespace PostFundManagement.Application.Extensions; +namespace PostFundManagement.Application.Extensions; public static class General { - public static IServiceCollection AddRegisterServices(this IServiceCollection services) + public static string Clean(this string input) => Patterns.SpaceRegex().Replace(input, string.Empty); + + public static string ToJson(this TValue obj) where TValue : class => JsonSerializer.Serialize(obj); + + public static TValue FromJson(this string json) where TValue : class => JsonSerializer.Deserialize(json)!; + + public static Guid GetUserId(this HttpContext context) => (Guid)context.Items[Domain.Extensions.Constants.UserIdItemKey]!; + + public static string GetUserEmail(this HttpContext context) => (string)context.Items[Domain.Extensions.Constants.EmailtemKey]!; + + public static string GetUserRole(this HttpContext context) => (string)context.Items[Domain.Extensions.Constants.UserRoleItemKey]!; + + public static (bool Decoded, Guid userId) GetUserId(this ClaimsPrincipal principal) { - var serviceType = typeof(IService); + var subClaim = principal.FindFirst(JwtRegisteredClaimNames.Sub)?.Value ?? principal.FindFirst(ClaimTypes.NameIdentifier)?.Value; - var sharedImplementations = typeof(IApplication).Assembly.GetTypes() - .Where(t => serviceType.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract); + var decoded = Guid.TryParse(subClaim, out var userId); - foreach (var sharedImplementation in sharedImplementations) services.AddScoped(sharedImplementation); - - return services; + return (decoded, userId); } + + public static (bool Decoded, string email) GetUserEmail(this ClaimsPrincipal principal) + { + var emailClaim = principal.FindFirst("email")?.Value ?? principal.FindFirst(ClaimTypes.Email)?.Value; + + var decoded = !string.IsNullOrWhiteSpace(emailClaim); + + return (decoded, emailClaim!); + } + + public static (bool Decoded, string role) GetUserRole(this ClaimsPrincipal principal) + { + var roleClaim = principal.FindFirst("role")?.Value ?? principal.FindFirst(ClaimTypes.Role)?.Value; + + var decoded = !string.IsNullOrWhiteSpace(roleClaim); + + return (decoded, roleClaim!); + } + + public static Microsoft.AspNetCore.Mvc.ProblemDetails ToProblems(this IReadOnlyList errors, int statusCode = StatusCodes.Status400BadRequest) => new() + { + Detail = string.Join(',', errors.Select(e => e.Message)), + Status = statusCode, + Title = "Error", + }; }