IdentityService.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Security.Claims;
  2. namespace Admin.NET.Core.Service;
  3. public class IdentityService : ITransient
  4. {
  5. private readonly IHttpContextAccessor _context;
  6. private readonly List<APIJSON_Role> _roles;
  7. public IdentityService(IHttpContextAccessor context, IOptions<APIJSONOptions> roles)
  8. {
  9. _context = context ?? throw new ArgumentNullException(nameof(context));
  10. _roles = roles.Value.Roles;
  11. }
  12. /// <summary>
  13. /// 获取当前用户Id
  14. /// </summary>
  15. /// <returns></returns>
  16. public string GetUserIdentity()
  17. {
  18. return _context.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
  19. }
  20. /// <summary>
  21. /// 获取当前用户权限名称
  22. /// </summary>
  23. /// <returns></returns>
  24. public string GetUserRoleName()
  25. {
  26. return _context.HttpContext.User.FindFirstValue(ClaimTypes.Role);
  27. }
  28. /// <summary>
  29. /// 获取当前用户权限
  30. /// </summary>
  31. /// <returns></returns>
  32. public APIJSON_Role GetRole()
  33. {
  34. var role = new APIJSON_Role();
  35. if (string.IsNullOrEmpty(GetUserRoleName())) // 若没登录默认取第一个
  36. {
  37. role = _roles.FirstOrDefault();
  38. }
  39. else
  40. {
  41. role = _roles.FirstOrDefault(it => it.RoleName.Equals(GetUserRoleName(), StringComparison.CurrentCultureIgnoreCase));
  42. }
  43. return role;
  44. }
  45. /// <summary>
  46. /// 获取当前表的可查询字段
  47. /// </summary>
  48. /// <param name="table"></param>
  49. /// <returns></returns>
  50. public (bool, string) GetSelectRole(string table)
  51. {
  52. var role = GetRole();
  53. if (role == null || role.Select == null || role.Select.Table == null)
  54. {
  55. return (false, $"appsettings.json权限配置不正确!");
  56. }
  57. string tablerole = role.Select.Table.FirstOrDefault(it => it == "*" || it.Equals(table, StringComparison.CurrentCultureIgnoreCase));
  58. if (string.IsNullOrEmpty(tablerole))
  59. {
  60. return (false, $"表名{table}没权限查询!");
  61. }
  62. int index = Array.IndexOf(role.Select.Table, tablerole);
  63. string selectrole = role.Select.Column[index];
  64. return (true, selectrole);
  65. }
  66. /// <summary>
  67. /// 当前列是否在角色里面
  68. /// </summary>
  69. /// <param name="col"></param>
  70. /// <param name="selectrole"></param>
  71. /// <returns></returns>
  72. public bool ColIsRole(string col, string[] selectrole)
  73. {
  74. if (selectrole.Contains("*"))
  75. {
  76. return true;
  77. }
  78. else
  79. {
  80. if (col.Contains("(") && col.Contains(")"))
  81. {
  82. Regex reg = new Regex(@"\(([^)]*)\)");
  83. Match m = reg.Match(col);
  84. return selectrole.Contains(m.Result("$1"), StringComparer.CurrentCultureIgnoreCase);
  85. }
  86. else
  87. {
  88. return selectrole.Contains(col, StringComparer.CurrentCultureIgnoreCase);
  89. }
  90. }
  91. }
  92. }