RoleController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using IdentityModel;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.Model.Core.Models;
  10. using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
  11. using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
  12. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  13. using TEAMModelOS.Service.Core.Interfaces;
  14. namespace TEAMModelOS.Controllers.Core
  15. {
  16. /// <summary>
  17. /// 获得学校信息
  18. /// </summary>
  19. [Route("api/[controller]")]
  20. [ApiController]
  21. [Authorize]
  22. public class RoleController : BaseController
  23. {
  24. private readonly IRoleService _roleSeservice;
  25. public RoleController(IRoleService roleService)
  26. {
  27. _roleSeservice = roleService;
  28. }
  29. [HttpPost("FindRoleByDict")]
  30. public async Task<BaseJosnRPCResponse> FindRoleByDict(JosnRPCRequest<Dictionary<string, object>> request)
  31. {
  32. // request.@params.TryAdd("PartitionKey", request.lang);
  33. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  34. List<Role> roles = await _roleSeservice.FindListByDictHasAll<Role>(request.@params);
  35. return builder.Data(roles).build();
  36. }
  37. [HttpPost("GetLoginRoles")]
  38. public async Task<BaseJosnRPCResponse> GetLoginRoles(JosnRPCRequest<Dictionary<string, object>> request)
  39. {
  40. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  41. List<string> rolecodes = GetLoginUser(JwtClaimTypes.Role);
  42. List<Role> roles = new List<Role>();
  43. if (rolecodes.IsNotEmpty()) {
  44. foreach (string code in rolecodes)
  45. {
  46. Role role = await _roleSeservice.FindByRowKey<Role>(code);
  47. if (role != null && !string.IsNullOrEmpty(role.RowKey))
  48. {
  49. List<RoleIdentityClaim> identityClaims = await _roleSeservice.FindListByDict<RoleIdentityClaim>(new Dictionary<string, object> {
  50. // {"PartitionKey",request.lang },
  51. { "RoleCode",role.RowKey },{"RoleLevel",role.Level }
  52. });
  53. roles.Add(role);
  54. }
  55. }
  56. }
  57. return builder.Data(roles).build();
  58. }
  59. [HttpPost("GetLoginClaim")]
  60. public async Task<BaseJosnRPCResponse> GetLoginClaim(JosnRPCRequest<Dictionary<string, object>> request)
  61. {
  62. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  63. List<string> rolecodes = GetLoginUser(JwtClaimTypes.Role);
  64. List<string> Id = GetLoginUser(JwtClaimTypes.Id);
  65. List<Role> roles = new List<Role>();
  66. List<Dictionary<string, object>> roleClaims = new List<Dictionary<string, object>>();
  67. if (rolecodes.IsNotEmpty())
  68. {
  69. foreach (string code in rolecodes)
  70. {
  71. List<RoleIdentityClaimValue> claimValues= await _roleSeservice.FindListByDict<RoleIdentityClaimValue>(
  72. new Dictionary<string, object> { { "RoleCode", code },{ "TeamModelId", Id[0]}
  73. // , { "PartitionKey", request.lang}
  74. }
  75. );
  76. List<string> keys = new List<string>();
  77. List<Dictionary<string, object>> claims = new List<Dictionary<string, object>>();
  78. foreach (IGrouping<string, RoleIdentityClaimValue> group in claimValues.GroupBy(c =>c.GroupKey))
  79. {
  80. List<RoleIdentityClaimValue> claimValue = claimValues.Where(x => x.GroupKey.Equals(group.Key)).ToList();
  81. claimValue= claimValue.OrderBy(s => s.ClaimOrder).ToList() ;
  82. Dictionary<string, object> claim = new Dictionary<string, object>
  83. {
  84. { "claim", claimValue },
  85. { "group", group.Key }
  86. };
  87. claims.Add(claim);
  88. }
  89. Dictionary<string, object> roleClaim = new Dictionary<string, object>
  90. {
  91. { "roleClaim", claims },
  92. { "role", await _roleSeservice.FindOneByDict<Role>(new Dictionary<string, object> { { "RowKey", code },
  93. //{ "PartitionKey", request.lang }
  94. }) }
  95. };
  96. roleClaims.Add(roleClaim);
  97. }
  98. }
  99. return builder.Data(roleClaims).build();
  100. }
  101. }
  102. }