You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuAuthExtensions.cs 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using Luticate2.Auth.Attributes.EntityAccessors;
  3. using Luticate2.Auth.Business;
  4. using Luticate2.Auth.DataAccess;
  5. using Luticate2.Auth.Dbo.Users;
  6. using Luticate2.Auth.Interfaces.Groups;
  7. using Luticate2.Auth.Interfaces.Permissions;
  8. using Luticate2.Auth.Interfaces.Users;
  9. using Luticate2.Auth.Middlewares;
  10. using Luticate2.Utils.Controllers;
  11. using Luticate2.Utils.Dbo.Basic;
  12. using Microsoft.AspNetCore.Builder;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.AspNetCore.Mvc;
  15. using Microsoft.EntityFrameworkCore;
  16. using Microsoft.Extensions.DependencyInjection;
  17. namespace Luticate2.Auth.Controllers
  18. {
  19. public static class LuAuthExtensions
  20. {
  21. public const string RoutePrefix = "luticate";
  22. public const string LuticateItemsLoggedUser = "loggedUser";
  23. public static IServiceCollection AddLuticateAuth(this IServiceCollection services,
  24. Action<LuUtilsOptionsDbo> optionsDelegate, Action<DbContextOptionsBuilder> optionsAction)
  25. {
  26. services.AddLuticateUtils(optionsDelegate);
  27. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  28. services.AddScoped<ILuLoggedUserAccessor, LuLoggedUserAccessor>();
  29. services.AddScoped<LuAttrLoggedUserAccessor>();
  30. services.AddScoped<LuAttrArgumentAccessor>();
  31. services.AddTransient<LuGroupsController>();
  32. services.AddTransient<ILuGroupsBusiness, LuGroupsBusiness>();
  33. services.AddTransient<LuGroupsDataAccess>();
  34. services.AddTransient<LuUsersController>();
  35. services.AddTransient<ILuUsersBusiness, LuUsersBusiness>();
  36. services.AddTransient<ILuPermissionsBusiness, LuPermissionsBusiness>();
  37. services.AddDbContext<LuAuthDatabaseContext>(options =>
  38. {
  39. options.UseInternalServiceProvider(new ServiceCollection()
  40. .AddEntityFrameworkNpgsqlLuticate()
  41. .AddEntityFrameworkNpgsql()
  42. .BuildServiceProvider());
  43. optionsAction.Invoke(options);
  44. }, ServiceLifetime.Transient);
  45. return services;
  46. }
  47. public static IMvcBuilder AddLuticateAuth(this IMvcBuilder builder)
  48. {
  49. builder.AddLuticateUtils();
  50. builder.Services.Configure<MvcOptions>(
  51. options =>
  52. {
  53. options.Filters.Add(typeof(LuLoggedUserMiddleware));
  54. options.Filters.Add(typeof(LuPermissionMiddleware));
  55. });
  56. return builder;
  57. }
  58. public static IApplicationBuilder UseLuticateAuth(this IApplicationBuilder app)
  59. {
  60. app.UseLuticateUtils();
  61. return app;
  62. }
  63. public static UsersDbo GetLuLoggedUser(this HttpContext context)
  64. {
  65. return context.GetLuItems()[LuticateItemsLoggedUser] as UsersDbo;
  66. }
  67. public static void SetLuLoggedUser(this HttpContext context, UsersDbo user)
  68. {
  69. context.GetLuItems()[LuticateItemsLoggedUser] = user;
  70. }
  71. }
  72. }