1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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<LuUtilsOptionsDbo> optionsDelegate, Action<DbContextOptionsBuilder> optionsAction)
- {
- services.AddLuticateUtils(optionsDelegate);
-
- services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
-
- services.AddScoped<ILuLoggedUserAccessor, LuLoggedUserAccessor>();
- services.AddScoped<LuAttrLoggedUserAccessor>();
- services.AddScoped<LuAttrArgumentAccessor>();
-
- services.AddTransient<LuGroupsController>();
- services.AddTransient<ILuGroupsBusiness, LuGroupsBusiness>();
- services.AddTransient<LuGroupsDataAccess>();
-
- services.AddTransient<LuUsersController>();
- services.AddTransient<ILuUsersBusiness, LuUsersBusiness>();
-
- services.AddTransient<ILuPermissionsBusiness, LuPermissionsBusiness>();
-
- services.AddDbContext<LuAuthDatabaseContext>(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<MvcOptions>(
- 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;
- }
- }
- }
|