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.

LuUsersController.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.ComponentModel.DataAnnotations;
  2. using Luticate2.Auth.Attributes;
  3. using Luticate2.Auth.Business;
  4. using Luticate2.Auth.Dbo.Basic;
  5. using Luticate2.Auth.Dbo.Permissions;
  6. using Luticate2.Auth.Dbo.Users;
  7. using Luticate2.Auth.Interfaces.Users;
  8. using Luticate2.Utils.Controllers;
  9. using Luticate2.Utils.Dbo.Basic;
  10. using Luticate2.Utils.Dbo.PaginatedRequest;
  11. using Luticate2.Utils.Utils;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.Extensions.Options;
  15. namespace Luticate2.Auth.Controllers
  16. {
  17. [Route(LuAuthExtensions.RoutePrefix + "/[controller]")]
  18. public class LuUsersController : LuController
  19. {
  20. private const string EntityType = LuEntityTypes.LuUsers;
  21. private const string ReadPermission = LuPermissions.LuGroupsRead;
  22. private const string WritePermission = LuPermissions.LuGroupsWrite;
  23. private readonly ILuUsersBusiness _busines;
  24. private readonly LuAuthOptionsDbo _luAuthOptions;
  25. public LuUsersController(ILuUsersBusiness busines, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo,
  26. IOptions<LuAuthOptionsDbo> luAuthOptions) : base(luUtilsOptionsDbo)
  27. {
  28. _busines = busines;
  29. _luAuthOptions = luAuthOptions.Value;
  30. }
  31. [HttpGet("{id}")]
  32. [LuPermission(ReadPermission, EntityType)]
  33. public LuApiWrapperDbo<LuUsersDbo> GetSingleById([LuPermissionArg][Required]string id)
  34. {
  35. return Handle(_busines.GetSingleById(id).ToLite());
  36. }
  37. [HttpGet]
  38. [LuPermission(ReadPermission, EntityType)]
  39. public LuApiWrapperDbo<LuPaginatedDbo<LuUsersDbo>> GetMultiple([Required]LuPaginatedRequestDbo request)
  40. {
  41. return Handle(_busines.GetMultiple(request).ToLite());
  42. }
  43. [HttpPost]
  44. [LuPermission(WritePermission, EntityType)]
  45. public LuApiWrapperDbo<LuUsersDbo> Register([Required]LuUsersAddDbo data)
  46. {
  47. return Handle(_busines.Register(data).ToLite());
  48. }
  49. [HttpPost("{id}")]
  50. [LuPermission(WritePermission, EntityType)]
  51. public LuApiWrapperDbo<LuUsersDbo> Edit([LuPermissionArg][Required]string id, [Required]LuUsersEditDbo data)
  52. {
  53. return Handle(_busines.Edit(id, data).ToLite());
  54. }
  55. [HttpDelete("{id}")]
  56. [LuPermission(WritePermission, EntityType)]
  57. public LuApiWrapperDbo<LuUsersDbo> Delete([LuPermissionArg][Required]string id)
  58. {
  59. return Handle(_busines.DeleteSingleByIdDbo(id).ToLite());
  60. }
  61. [HttpPost("login")]
  62. public LuApiWrapperDbo<LuUsersLoginResultDbo> Login([Required]string username, [Required]string password)
  63. {
  64. var loginRes = _busines.Login(username, password);
  65. if (loginRes)
  66. {
  67. Response.Cookies.Append(LuAuthExtensions.TokenCookieName, loginRes.Data.Token, new CookieOptions
  68. {
  69. HttpOnly = true,
  70. Secure = _luAuthOptions.SecureCookies
  71. });
  72. }
  73. return Handle(loginRes);
  74. }
  75. [HttpPost("logout")]
  76. public LuApiWrapperDbo<bool> Logout(/*string token*/)
  77. {
  78. // var t = token;
  79. // if (t == null)
  80. // {
  81. // t = HttpContext.GetLuUserToken();
  82. // }
  83. var t = HttpContext.GetLuUserToken();
  84. var logoutRes = _busines.Logout(t);
  85. if (logoutRes/* && (token == null || t == token)*/)
  86. {
  87. Response.Cookies.Delete(LuAuthExtensions.TokenCookieName);
  88. }
  89. return Handle(logoutRes);
  90. }
  91. [HttpGet("me")]
  92. public LuApiWrapperDbo<LuUsersDbo> Me()
  93. {
  94. return Handle(_busines.Me().ToLite());
  95. }
  96. [HttpGet("sessions")]
  97. public LuApiWrapperDbo<LuPaginatedDbo<LuUsersTokenDbo>> Sessions(LuPaginatedRequestDbo paginatedRequestDbo)
  98. {
  99. return Handle(_busines.Sessions(paginatedRequestDbo));
  100. }
  101. }
  102. }