using System; using Luticate2.Auth.Attributes.EntityAccessors; using Luticate2.Auth.Business; using Luticate2.Auth.DataAccess; using Luticate2.Auth.Dbo.Users; using Luticate2.Auth.Interfaces.Groups; using Luticate2.Auth.Interfaces.Permissions; using Luticate2.Auth.Interfaces.Users; using Luticate2.Auth.Middlewares; using Luticate2.Utils.Controllers; using Luticate2.Utils.Dbo.Basic; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Luticate2.Auth.Controllers { public static class LuAuthExtensions { public const string RoutePrefix = "luticate"; public const string LuticateItemsLoggedUser = "loggedUser"; public static IServiceCollection AddLuticateAuth(this IServiceCollection services, Action optionsDelegate, Action optionsAction) { services.AddLuticateUtils(optionsDelegate); services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddDbContext(options => { options.UseInternalServiceProvider(new ServiceCollection() .AddEntityFrameworkNpgsqlLuticate() .AddEntityFrameworkNpgsql() .BuildServiceProvider()); optionsAction.Invoke(options); }, ServiceLifetime.Transient); return services; } public static IMvcBuilder AddLuticateAuth(this IMvcBuilder builder) { builder.AddLuticateUtils(); builder.Services.Configure( options => { options.Filters.Add(typeof(LuLoggedUserMiddleware)); options.Filters.Add(typeof(LuPermissionMiddleware)); }); return builder; } public static IApplicationBuilder UseLuticateAuth(this IApplicationBuilder app) { app.UseLuticateUtils(); return app; } public static UsersDbo GetLuLoggedUser(this HttpContext context) { return context.GetLuItems()[LuticateItemsLoggedUser] as UsersDbo; } public static void SetLuLoggedUser(this HttpContext context, UsersDbo user) { context.GetLuItems()[LuticateItemsLoggedUser] = user; } } }