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.

151 lines
4.7 KiB

1 year ago
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Identity;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Shentun.Peis.Permissions;
  5. using Shentun.Peis.Rooms;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Volo.Abp;
  12. using Volo.Abp.Application.Dtos;
  13. using Volo.Abp.Application.Services;
  14. using Volo.Abp.Data;
  15. using Volo.Abp.DependencyInjection;
  16. using Volo.Abp.Identity;
  17. using Volo.Abp.ObjectExtending;
  18. namespace Shentun.Peis.Roles
  19. {
  20. /// <summary>
  21. /// 角色管理
  22. /// </summary>
  23. [Authorize]
  24. public class MyRoleAppService : ApplicationService
  25. {
  26. private readonly IdentityRoleManager _roleManager;
  27. private readonly IIdentityRoleRepository _roleRepository;
  28. public MyRoleAppService(
  29. IdentityRoleManager roleManager,
  30. IIdentityRoleRepository roleRepository)
  31. {
  32. _roleManager = roleManager;
  33. _roleRepository = roleRepository;
  34. }
  35. /// <summary>
  36. /// 查询
  37. /// </summary>
  38. /// <returns></returns>
  39. [Authorize(PeisPermissions.SystemSettings.Default)]
  40. [HttpPost("api/app/MyRole/GetAllList")]
  41. public async Task<ListResultDto<IdentityRoleDto>> GetAllListAsync()
  42. {
  43. var list = await _roleRepository.GetListAsync();
  44. return new ListResultDto<IdentityRoleDto>(
  45. ObjectMapper.Map<List<IdentityRole>, List<IdentityRoleDto>>(list)
  46. );
  47. }
  48. /// <summary>
  49. /// 创建
  50. /// </summary>
  51. /// <param name="input"></param>
  52. /// <returns></returns>
  53. [Authorize(PeisPermissions.SystemSettings.Default)]
  54. [HttpPost("api/app/MyRole/Create")]
  55. public async Task<IdentityRoleDto> CreateAsync(IdentityRoleCreateDto input)
  56. {
  57. var role = new IdentityRole(
  58. GuidGenerator.Create(),
  59. input.Name,
  60. CurrentTenant.Id
  61. )
  62. {
  63. IsDefault = input.IsDefault,
  64. IsPublic = input.IsPublic
  65. };
  66. input.MapExtraPropertiesTo(role);
  67. (await _roleManager.CreateAsync(role)).CheckErrors();
  68. await CurrentUnitOfWork.SaveChangesAsync();
  69. return ObjectMapper.Map<IdentityRole, IdentityRoleDto>(role);
  70. }
  71. /// <summary>
  72. /// 修改
  73. /// </summary>
  74. /// <param name="input"></param>
  75. /// <returns></returns>
  76. [Authorize(PeisPermissions.SystemSettings.Default)]
  77. [HttpPost("api/app/MyRole/Update")]
  78. public async Task<IdentityRoleDto> UpdateAsync(UpdateRoleInputDto input)
  79. {
  80. var role = await _roleManager.GetByIdAsync(input.Id);
  81. role.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);
  82. (await _roleManager.SetRoleNameAsync(role, input.Name)).CheckErrors();
  83. role.IsDefault = input.IsDefault;
  84. role.IsPublic = input.IsPublic;
  85. input.MapExtraPropertiesTo(role);
  86. (await _roleManager.UpdateAsync(role)).CheckErrors();
  87. await CurrentUnitOfWork.SaveChangesAsync();
  88. return ObjectMapper.Map<IdentityRole, IdentityRoleDto>(role);
  89. }
  90. /// <summary>
  91. /// 删除
  92. /// </summary>
  93. /// <param name="input"></param>
  94. /// <returns></returns>
  95. [Authorize(PeisPermissions.SystemSettings.Default)]
  96. [HttpPost("api/app/MyRole/Delete")]
  97. public async Task DeleteAsync(RoleIdInputDto input)
  98. {
  99. var role = await _roleManager.FindByIdAsync(input.Id.ToString());
  100. if (role == null)
  101. {
  102. return;
  103. }
  104. (await _roleManager.DeleteAsync(role)).CheckErrors();
  105. }
  106. [Authorize(PeisPermissions.SystemSettings.Default)]
  107. [RemoteService(false)]
  108. public async Task<IdentityRoleDto> GetAsync(Guid id)
  109. {
  110. return ObjectMapper.Map<IdentityRole, IdentityRoleDto>(
  111. await _roleManager.GetByIdAsync(id)
  112. );
  113. }
  114. [Authorize(PeisPermissions.SystemSettings.Default)]
  115. [RemoteService(false)]
  116. public async Task<PagedResultDto<IdentityRoleDto>> GetListAsync(GetIdentityRolesInput input)
  117. {
  118. var list = await _roleRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter);
  119. var totalCount = await _roleRepository.GetCountAsync(input.Filter);
  120. return new PagedResultDto<IdentityRoleDto>(
  121. totalCount,
  122. ObjectMapper.Map<List<IdentityRole>, List<IdentityRoleDto>>(list)
  123. );
  124. }
  125. }
  126. }