using BusinessERP.Data; using BusinessERP.Models; using BusinessERP.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; namespace BusinessERP.Helpers { public static class ProgramTaskExtension { public static void SeedingData(WebApplication app) { try { using (var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService(); var userManager = services.GetRequiredService>(); var roleManager = services.GetRequiredService>(); var functional = services.GetRequiredService(); DbInitializer.Initialize(context, functional).Wait(); } catch (Exception ex) { var logger = services.GetRequiredService>(); logger.LogError(ex, "An error occurred while seeding the database."); } } } catch (Exception) { throw; } } } internal class AuthResponsesOperationFilter : IOperationFilter { public void Apply(OpenApiOperation operation, OperationFilterContext context) { var attributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true) .Union(context.MethodInfo.GetCustomAttributes(true)); if (attributes.OfType().Any()) { return; } var authAttributes = attributes.OfType(); if (authAttributes.Any()) { operation.Responses["401"] = new OpenApiResponse { Description = "Unauthorized" }; if (authAttributes.Any(att => !String.IsNullOrWhiteSpace(att.Roles) || !String.IsNullOrWhiteSpace(att.Policy))) { operation.Responses["403"] = new OpenApiResponse { Description = "Forbidden" }; } operation.Security = new List { new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Id = "BearerAuth", Type = ReferenceType.SecurityScheme } }, Array.Empty() } } }; } } } }