123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System;
- using Luticate2.Auth.Attributes.EntityAccessors;
- using Luticate2.Auth.Business;
- using Luticate2.Auth.DataAccess;
- using Luticate2.Auth.Dbo.Basic;
- using Luticate2.Auth.Dbo.Users;
- using Luticate2.Auth.Interfaces.Groups;
- using Luticate2.Auth.Interfaces.Permissions;
- using Luticate2.Auth.Interfaces.Tokens;
- 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;
- using Microsoft.Extensions.DependencyInjection.Extensions;
-
- namespace Luticate2.Auth.Controllers
- {
- public static class LuAuthExtensions
- {
- public const string RoutePrefix = "luticate";
-
- public const string LuticateItemsLoggedUser = "loggedUser";
-
- public const string TokenCookieName = "luticate2-token";
-
- public static IServiceCollection AddLuticateAuth(this IServiceCollection services,
- Action<LuUtilsOptionsDbo> utilsOptionsDelegate, Action<DbContextOptionsBuilder> optionsAction,
- Action<LuAuthOptionsDbo> authOptionsDelegate)
- {
- services.AddLuticateUtils(utilsOptionsDelegate);
-
- services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
-
- services.TryAddScoped<ILuLoggedUserAccessor, LuLoggedUserAccessor>();
- services.TryAddScoped<LuAttrLoggedUserAccessor>();
- services.TryAddScoped<LuAttrArgumentAccessor>();
-
- services.TryAddTransient<LuGroupsController>();
- services.TryAddTransient<ILuGroupsBusiness, LuGroupsBusiness>();
- services.TryAddTransient<LuGroupsDataAccess>();
-
- services.TryAddTransient<LuUsersController>();
- services.TryAddTransient<ILuUsersBusiness, LuUsersBusiness>();
- services.TryAddTransient<LuUsersDataAccess>();
-
- services.TryAddTransient<ILuTokensBusiness, LuTokensBusiness>();
- services.TryAddTransient<LuTokensDataAccess>();
-
- services.TryAddTransient<ILuPermissionsBusiness, LuPermissionsBusiness>();
-
- services.AddDbContext<LuAuthDatabaseContext>(options =>
- {
- options.UseInternalServiceProvider(new ServiceCollection()
- .AddEntityFrameworkNpgsqlLuticate()
- .AddEntityFrameworkNpgsql()
- .BuildServiceProvider());
- optionsAction.Invoke(options);
- }, ServiceLifetime.Transient);
-
- services.Configure(authOptionsDelegate);
-
- 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 LuUsersFullDbo GetLuLoggedUser(this HttpContext context)
- {
- return context.GetLuItems()[LuticateItemsLoggedUser] as LuUsersFullDbo;
- }
-
- public static void SetLuLoggedUser(this HttpContext context, LuUsersFullDbo user)
- {
- context.GetLuItems()[LuticateItemsLoggedUser] = user;
- }
-
- public static string GetLuUserToken(this HttpContext context)
- {
- var token = context.Request.Cookies[TokenCookieName];
- return string.IsNullOrWhiteSpace(token) ? null : token;
- }
- }
- }
|