Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LuAuthExtensions.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.Tokens;
  9. using Luticate2.Auth.Interfaces.Users;
  10. using Luticate2.Auth.Middlewares;
  11. using Luticate2.Utils.Controllers;
  12. using Luticate2.Utils.Dbo.Basic;
  13. using Microsoft.AspNetCore.Builder;
  14. using Microsoft.AspNetCore.Http;
  15. using Microsoft.AspNetCore.Mvc;
  16. using Microsoft.EntityFrameworkCore;
  17. using Microsoft.Extensions.DependencyInjection;
  18. using Microsoft.Extensions.DependencyInjection.Extensions;
  19. namespace Luticate2.Auth.Controllers
  20. {
  21. public static class LuAuthExtensions
  22. {
  23. public const string RoutePrefix = "luticate";
  24. public const string LuticateItemsLoggedUser = "loggedUser";
  25. public const string TokenCookieName = "luticate2-token";
  26. public static IServiceCollection AddLuticateAuth(this IServiceCollection services,
  27. Action<LuUtilsOptionsDbo> optionsDelegate, Action<DbContextOptionsBuilder> optionsAction)
  28. {
  29. services.AddLuticateUtils(optionsDelegate);
  30. services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  31. services.TryAddScoped<ILuLoggedUserAccessor, LuLoggedUserAccessor>();
  32. services.TryAddScoped<LuAttrLoggedUserAccessor>();
  33. services.TryAddScoped<LuAttrArgumentAccessor>();
  34. services.TryAddTransient<LuGroupsController>();
  35. services.TryAddTransient<ILuGroupsBusiness, LuGroupsBusiness>();
  36. services.TryAddTransient<LuGroupsDataAccess>();
  37. services.TryAddTransient<LuUsersController>();
  38. services.TryAddTransient<ILuUsersBusiness, LuUsersBusiness>();
  39. services.TryAddTransient<LuUsersDataAccess>();
  40. services.TryAddTransient<ILuTokensBusiness, LuTokensBusiness>();
  41. services.TryAddTransient<LuTokensDataAccess>();
  42. services.TryAddTransient<ILuPermissionsBusiness, LuPermissionsBusiness>();
  43. services.AddDbContext<LuAuthDatabaseContext>(options =>
  44. {
  45. options.UseInternalServiceProvider(new ServiceCollection()
  46. .AddEntityFrameworkNpgsqlLuticate()
  47. .AddEntityFrameworkNpgsql()
  48. .BuildServiceProvider());
  49. optionsAction.Invoke(options);
  50. }, ServiceLifetime.Transient);
  51. return services;
  52. }
  53. public static IMvcBuilder AddLuticateAuth(this IMvcBuilder builder)
  54. {
  55. builder.AddLuticateUtils();
  56. builder.Services.Configure<MvcOptions>(
  57. options =>
  58. {
  59. options.Filters.Add(typeof(LuLoggedUserMiddleware));
  60. options.Filters.Add(typeof(LuPermissionMiddleware));
  61. });
  62. return builder;
  63. }
  64. public static IApplicationBuilder UseLuticateAuth(this IApplicationBuilder app)
  65. {
  66. app.UseLuticateUtils();
  67. return app;
  68. }
  69. public static LuUsersFullDbo GetLuLoggedUser(this HttpContext context)
  70. {
  71. return context.GetLuItems()[LuticateItemsLoggedUser] as LuUsersFullDbo;
  72. }
  73. public static void SetLuLoggedUser(this HttpContext context, LuUsersFullDbo user)
  74. {
  75. context.GetLuItems()[LuticateItemsLoggedUser] = user;
  76. }
  77. public static string GetLuUserToken(this HttpContext context)
  78. {
  79. var token = context.Request.Cookies[TokenCookieName];
  80. return string.IsNullOrWhiteSpace(token) ? null : token;
  81. }
  82. }
  83. }