Files
Yi.Admin/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/RoleService.cs

229 lines
9.5 KiB
C#
Raw Normal View History

2023-12-11 09:55:12 +08:00
using Mapster;
using Microsoft.AspNetCore.Mvc;
2023-04-13 21:12:06 +08:00
using SqlSugar;
2023-12-11 09:55:12 +08:00
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Entities;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Role;
using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Managers;
2024-09-24 11:16:19 +08:00
using Yi.Framework.Rbac.Domain.Shared.Consts;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Rbac.Domain.Shared.Enums;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services.System
2023-04-13 21:12:06 +08:00
{
/// <summary>
/// Role服务实现
/// </summary>
2024-09-24 11:16:19 +08:00
public class RoleService : YiCrudAppService<RoleAggregateRoot, RoleGetOutputDto, RoleGetListOutputDto, Guid,
RoleGetListInputVo, RoleCreateInputVo, RoleUpdateInputVo>,
IRoleService
2023-04-13 21:12:06 +08:00
{
2024-09-24 11:16:19 +08:00
public RoleService(RoleManager roleManager, ISqlSugarRepository<RoleDeptEntity> roleDeptRepository,
ISqlSugarRepository<UserRoleEntity> userRoleRepository,
ISqlSugarRepository<RoleAggregateRoot, Guid> repository) : base(repository)
2023-12-11 09:55:12 +08:00
{
(_roleManager, _roleDeptRepository, _userRoleRepository, _repository) =
2024-09-24 11:16:19 +08:00
(roleManager, roleDeptRepository, userRoleRepository, repository);
2023-12-11 09:55:12 +08:00
}
2024-05-22 14:35:08 +08:00
private ISqlSugarRepository<RoleAggregateRoot, Guid> _repository;
2023-04-13 21:12:06 +08:00
private RoleManager _roleManager { get; set; }
2023-12-11 09:55:12 +08:00
private ISqlSugarRepository<RoleDeptEntity> _roleDeptRepository;
2023-04-15 12:19:02 +08:00
2023-12-11 09:55:12 +08:00
private ISqlSugarRepository<UserRoleEntity> _userRoleRepository;
2024-01-02 23:26:05 +08:00
2024-12-23 12:15:16 +08:00
public async Task UpdateDataScopeAsync(UpdateDataScopeInput input)
{
//只有自定义的需要特殊处理
if (input.DataScope == DataScopeEnum.CUSTOM)
{
await _roleDeptRepository.DeleteAsync(x => x.RoleId == input.RoleId);
2024-09-24 11:16:19 +08:00
var insertEntities = input.DeptIds.Select(x => new RoleDeptEntity { DeptId = x, RoleId = input.RoleId })
.ToList();
await _roleDeptRepository.InsertRangeAsync(insertEntities);
}
2024-09-24 11:16:19 +08:00
2024-05-22 14:35:08 +08:00
var entity = new RoleAggregateRoot() { DataScope = input.DataScope };
2023-12-11 09:55:12 +08:00
EntityHelper.TrySetId(entity, () => input.RoleId);
await _repository._Db.Updateable(entity).UpdateColumns(x => x.DataScope).ExecuteCommandAsync();
}
2023-04-13 21:12:06 +08:00
public override async Task<PagedResultDto<RoleGetListOutputDto>> GetListAsync(RoleGetListInputVo input)
{
RefAsync<int> total = 0;
2024-09-24 11:16:19 +08:00
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.RoleCode),
x => x.RoleCode.Contains(input.RoleCode!))
2023-04-13 21:12:06 +08:00
.WhereIF(!string.IsNullOrEmpty(input.RoleName), x => x.RoleName.Contains(input.RoleName!))
2024-09-24 11:16:19 +08:00
.WhereIF(input.State is not null, x => x.State == input.State)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
2023-04-13 21:12:06 +08:00
return new PagedResultDto<RoleGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
}
/// <summary>
/// 添加角色
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<RoleGetOutputDto> CreateAsync(RoleCreateInputVo input)
{
2024-09-24 11:16:19 +08:00
var isExist =
await _repository.IsAnyAsync(x => x.RoleCode == input.RoleCode || x.RoleName == input.RoleName);
if (isExist)
{
throw new UserFriendlyException(RoleConst.Exist);
}
2023-04-15 17:33:42 +08:00
var entity = await MapToEntityAsync(input);
await _repository.InsertAsync(entity);
2024-09-24 11:16:19 +08:00
var outputDto = await MapToGetOutputDtoAsync(entity);
2023-04-13 21:12:06 +08:00
return outputDto;
}
/// <summary>
/// 修改角色
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
2023-12-11 09:55:12 +08:00
public override async Task<RoleGetOutputDto> UpdateAsync(Guid id, RoleUpdateInputVo input)
2023-04-13 21:12:06 +08:00
{
2023-04-15 17:33:42 +08:00
var entity = await _repository.GetByIdAsync(id);
var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id)
.AnyAsync(x => x.RoleCode == input.RoleCode || x.RoleName == input.RoleName);
2024-09-24 11:16:19 +08:00
if (isExist)
{
throw new UserFriendlyException(RoleConst.Exist);
}
2023-04-15 17:33:42 +08:00
await MapToEntityAsync(input, entity);
await _repository.UpdateAsync(entity);
2023-04-13 21:12:06 +08:00
2023-12-11 09:55:12 +08:00
await _roleManager.GiveRoleSetMenuAsync(new List<Guid> { id }, input.MenuIds);
2023-04-13 21:12:06 +08:00
2024-09-24 11:16:19 +08:00
var dto = await MapToGetOutputDtoAsync(entity);
2023-04-13 21:12:06 +08:00
return dto;
}
/// <summary>
/// 更新状态
/// </summary>
/// <param name="id"></param>
/// <param name="state"></param>
/// <returns></returns>
2023-12-11 09:55:12 +08:00
[Route("role/{id}/{state}")]
public async Task<RoleGetOutputDto> UpdateStateAsync([FromRoute] Guid id, [FromRoute] bool state)
2023-04-13 21:12:06 +08:00
{
var entity = await _repository.GetByIdAsync(id);
if (entity is null)
{
throw new ApplicationException("角色未存在");
}
entity.State = state;
await _repository.UpdateAsync(entity);
return await MapToGetOutputDtoAsync(entity);
}
2023-05-23 19:29:56 +08:00
/// <summary>
/// 获取角色下的用户
/// </summary>
/// <param name="roleId"></param>
/// <param name="input"></param>
/// <param name="isAllocated">是否在该角色下</param>
/// <returns></returns>
2023-12-11 09:55:12 +08:00
[Route("role/auth-user/{roleId}/{isAllocated}")]
2024-09-24 11:16:19 +08:00
public async Task<PagedResultDto<UserGetListOutputDto>> GetAuthUserByRoleIdAsync([FromRoute] Guid roleId,
[FromRoute] bool isAllocated, [FromQuery] RoleAuthUserGetListInput input)
2023-05-23 19:29:56 +08:00
{
2023-05-24 20:54:26 +08:00
PagedResultDto<UserGetListOutputDto> output;
2023-05-23 19:29:56 +08:00
//角色下已授权用户
if (isAllocated == true)
{
2023-05-24 20:54:26 +08:00
output = await GetAllocatedAuthUserByRoleIdAsync(roleId, input);
2023-05-23 19:29:56 +08:00
}
//角色下未授权用户
else
{
2023-05-24 20:54:26 +08:00
output = await GetNotAllocatedAuthUserByRoleIdAsync(roleId, input);
2023-05-23 19:29:56 +08:00
}
2024-09-24 11:16:19 +08:00
2023-05-24 20:54:26 +08:00
return output;
2023-05-23 19:29:56 +08:00
}
2024-09-24 11:16:19 +08:00
private async Task<PagedResultDto<UserGetListOutputDto>> GetAllocatedAuthUserByRoleIdAsync(Guid roleId,
RoleAuthUserGetListInput input)
2023-05-24 20:54:26 +08:00
{
RefAsync<int> total = 0;
var output = await _userRoleRepository._DbQueryable
2024-09-24 11:16:19 +08:00
.LeftJoin<UserAggregateRoot>((ur, u) => ur.UserId == u.Id && ur.RoleId == roleId)
.Where((ur, u) => ur.RoleId == roleId)
.WhereIF(!string.IsNullOrEmpty(input.UserName), (ur, u) => u.UserName.Contains(input.UserName))
.WhereIF(input.Phone is not null, (ur, u) => u.Phone.ToString().Contains(input.Phone.ToString()))
.Select((ur, u) => new UserGetListOutputDto { Id = u.Id }, true)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
2023-05-24 20:54:26 +08:00
return new PagedResultDto<UserGetListOutputDto>(total, output);
}
2023-05-23 19:29:56 +08:00
2024-09-24 11:16:19 +08:00
private async Task<PagedResultDto<UserGetListOutputDto>> GetNotAllocatedAuthUserByRoleIdAsync(Guid roleId,
RoleAuthUserGetListInput input)
2023-05-24 20:54:26 +08:00
{
RefAsync<int> total = 0;
2024-05-22 14:35:08 +08:00
var entities = await _userRoleRepository._Db.Queryable<UserAggregateRoot>()
2024-09-24 11:16:19 +08:00
.Where(u => SqlFunc.Subqueryable<UserRoleEntity>().Where(x => x.RoleId == roleId)
.Where(x => x.UserId == u.Id).NotAny())
.WhereIF(!string.IsNullOrEmpty(input.UserName), u => u.UserName.Contains(input.UserName))
.WhereIF(input.Phone is not null, u => u.Phone.ToString().Contains(input.Phone.ToString()))
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
2023-05-24 20:54:26 +08:00
var output = entities.Adapt<List<UserGetListOutputDto>>();
return new PagedResultDto<UserGetListOutputDto>(total, output);
}
2023-05-23 19:29:56 +08:00
/// <summary>
/// 批量给用户授权
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task CreateAuthUserAsync([FromBody] RoleAuthUserCreateOrDeleteInput input)
2023-05-23 19:29:56 +08:00
{
2024-09-24 11:16:19 +08:00
var userRoleEntities = input.UserIds.Select(u => new UserRoleEntity { RoleId = input.RoleId, UserId = u })
.ToList();
2023-05-23 19:29:56 +08:00
await _userRoleRepository.InsertRangeAsync(userRoleEntities);
}
/// <summary>
/// 批量取消授权
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task DeleteAuthUserAsync([FromBody] RoleAuthUserCreateOrDeleteInput input)
2023-05-23 19:29:56 +08:00
{
await _userRoleRepository._Db.Deleteable<UserRoleEntity>().Where(x => x.RoleId == input.RoleId)
.Where(x => input.UserIds.Contains(x.UserId))
2024-09-24 11:16:19 +08:00
.ExecuteCommandAsync();
}
/// <summary>
/// 根据角色名称移除指定用户的角色
/// </summary>
/// <param name="userIds"></param>
/// <param name="roleCode"></param>
/// <returns></returns>
[RemoteService(isEnabled: false)]
public Task<int> RemoveUserRoleByRoleCodeAsync(List<Guid> userIds, string roleCode)
{
return _roleManager.RemoveUserRoleByRoleCodeAsync(userIds, roleCode);
2023-05-23 19:29:56 +08:00
}
2023-04-13 21:12:06 +08:00
}
2024-09-24 11:16:19 +08:00
}