feat: 优化rbac结构

This commit is contained in:
chenchun
2024-09-24 11:16:19 +08:00
parent 6359696bde
commit fd0edd93ea
23 changed files with 263 additions and 256 deletions

View File

@@ -1,28 +1,31 @@
using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Dept;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Repositories;
using Yi.Framework.Rbac.Domain.Shared.Consts;
namespace Yi.Framework.Rbac.Application.Services.System
{
/// <summary>
/// Dept服务实现
/// </summary>
public class DeptService : YiCrudAppService<DeptAggregateRoot, DeptGetOutputDto, DeptGetListOutputDto, Guid, DeptGetListInputVo, DeptCreateInputVo, DeptUpdateInputVo>, IDeptService
public class DeptService : YiCrudAppService<DeptAggregateRoot, DeptGetOutputDto, DeptGetListOutputDto, Guid,
DeptGetListInputVo, DeptCreateInputVo, DeptUpdateInputVo>, IDeptService
{
private IDeptRepository _deptRepository;
public DeptService(IDeptRepository deptRepository) : base(deptRepository)
{ _deptRepository = deptRepository; }
private IDeptRepository _repository;
public DeptService(IDeptRepository repository) : base(repository)
{
_repository = repository;
}
[RemoteService(false)]
public async Task<List<Guid>> GetChildListAsync(Guid deptId)
{
return await _deptRepository.GetChildListAsync(deptId);
return await _repository.GetChildListAsync(deptId);
}
/// <summary>
@@ -32,7 +35,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
//[Route("{roleId}")]
public async Task<List<DeptGetListOutputDto>> GetRoleIdAsync(Guid roleId)
{
var entities = await _deptRepository.GetListRoleIdAsync(roleId);
var entities = await _repository.GetListRoleIdAsync(roleId);
return await MapToGetListOutputDtosAsync(entities);
}
@@ -44,16 +47,36 @@ namespace Yi.Framework.Rbac.Application.Services.System
public override async Task<PagedResultDto<DeptGetListOutputDto>> GetListAsync(DeptGetListInputVo input)
{
RefAsync<int> total = 0;
var entities = await _deptRepository._DbQueryable
.WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
.WhereIF(input.State is not null, u => u.State == input.State)
.OrderBy(u => u.OrderNum, OrderByType.Asc)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
var entities = await _repository._DbQueryable
.WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
.WhereIF(input.State is not null, u => u.State == input.State)
.OrderBy(u => u.OrderNum, OrderByType.Asc)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<DeptGetListOutputDto>
{
Items = await MapToGetListOutputDtosAsync(entities),
TotalCount = total
};
}
protected override async Task CheckCreateInputDtoAsync(DeptCreateInputVo input)
{
var isExist =
await _repository.IsAnyAsync(x => x.DeptCode == input.DeptCode);
if (isExist)
{
throw new UserFriendlyException(DeptConst.Exist);
}
}
protected override async Task CheckUpdateInputDtoAsync(DeptAggregateRoot entity, DeptUpdateInputVo input)
{
var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id)
.AnyAsync(x => x.DeptCode == input.DeptCode);
if (isExist)
{
throw new UserFriendlyException(DeptConst.Exist);
}
}
}
}
}

View File

@@ -1,10 +1,10 @@
using SqlSugar;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Menu;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services.System
@@ -23,15 +23,12 @@ namespace Yi.Framework.Rbac.Application.Services.System
public override async Task<PagedResultDto<MenuGetListOutputDto>> GetListAsync(MenuGetListInputVo input)
{
RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.MenuName), x => x.MenuName.Contains(input.MenuName!))
.WhereIF(input.State is not null, x => x.State == input.State)
.Where(x=>x.MenuSource==input.MenuSource)
.OrderByDescending(x => x.OrderNum)
.ToListAsync();
//.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<MenuGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
}
@@ -46,15 +43,5 @@ namespace Yi.Framework.Rbac.Application.Services.System
return await MapToGetListOutputDtosAsync(entities);
}
public override Task<MenuGetOutputDto> UpdateAsync(Guid id, MenuUpdateInputVo input)
{
return base.UpdateAsync(id, input);
}
public override Task<MenuGetOutputDto> CreateAsync(MenuCreateInputVo input)
{
return base.CreateAsync(input);
}
}
}

View File

@@ -1,10 +1,10 @@
using SqlSugar;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Post;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services.System
@@ -12,10 +12,12 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <summary>
/// Post服务实现
/// </summary>
public class PostService : YiCrudAppService<PostAggregateRoot, PostGetOutputDto, PostGetListOutputDto, Guid, PostGetListInputVo, PostCreateInputVo, PostUpdateInputVo>,
IPostService
public class PostService : YiCrudAppService<PostAggregateRoot, PostGetOutputDto, PostGetListOutputDto, Guid,
PostGetListInputVo, PostCreateInputVo, PostUpdateInputVo>,
IPostService
{
private readonly ISqlSugarRepository<PostAggregateRoot, Guid> _repository;
public PostService(ISqlSugarRepository<PostAggregateRoot, Guid> repository) : base(repository)
{
_repository = repository;
@@ -25,10 +27,31 @@ namespace Yi.Framework.Rbac.Application.Services.System
{
RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.PostName), x => x.PostName.Contains(input.PostName!))
.WhereIF(input.State is not null, x => x.State == input.State)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.PostName),
x => x.PostName.Contains(input.PostName!))
.WhereIF(input.State is not null, x => x.State == input.State)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<PostGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
}
protected override async Task CheckCreateInputDtoAsync(PostCreateInputVo input)
{
var isExist =
await _repository.IsAnyAsync(x => x.PostCode == input.PostCode);
if (isExist)
{
throw new UserFriendlyException(PostConst.Exist);
}
}
protected override async Task CheckUpdateInputDtoAsync(PostAggregateRoot entity, PostUpdateInputVo input)
{
var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id)
.AnyAsync(x => x.PostCode == input.PostCode);
if (isExist)
{
throw new UserFriendlyException(RoleConst.Exist);
}
}
}
}
}

View File

@@ -11,6 +11,7 @@ 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;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.Rbac.Domain.Shared.Enums;
using Yi.Framework.SqlSugarCore.Abstractions;
@@ -19,13 +20,16 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <summary>
/// Role服务实现
/// </summary>
public class RoleService : YiCrudAppService<RoleAggregateRoot, RoleGetOutputDto, RoleGetListOutputDto, Guid, RoleGetListInputVo, RoleCreateInputVo, RoleUpdateInputVo>,
IRoleService
public class RoleService : YiCrudAppService<RoleAggregateRoot, RoleGetOutputDto, RoleGetListOutputDto, Guid,
RoleGetListInputVo, RoleCreateInputVo, RoleUpdateInputVo>,
IRoleService
{
public RoleService(RoleManager roleManager, ISqlSugarRepository<RoleDeptEntity> roleDeptRepository, ISqlSugarRepository<UserRoleEntity> userRoleRepository, ISqlSugarRepository<RoleAggregateRoot, Guid> repository) : base(repository)
public RoleService(RoleManager roleManager, ISqlSugarRepository<RoleDeptEntity> roleDeptRepository,
ISqlSugarRepository<UserRoleEntity> userRoleRepository,
ISqlSugarRepository<RoleAggregateRoot, Guid> repository) : base(repository)
{
(_roleManager, _roleDeptRepository, _userRoleRepository, _repository) =
(roleManager, roleDeptRepository, userRoleRepository, repository);
(roleManager, roleDeptRepository, userRoleRepository, repository);
}
private ISqlSugarRepository<RoleAggregateRoot, Guid> _repository;
@@ -41,23 +45,25 @@ namespace Yi.Framework.Rbac.Application.Services.System
if (input.DataScope == DataScopeEnum.CUSTOM)
{
await _roleDeptRepository.DeleteAsync(x => x.RoleId == input.RoleId);
var insertEntities = input.DeptIds.Select(x => new RoleDeptEntity { DeptId = x, RoleId = input.RoleId }).ToList();
var insertEntities = input.DeptIds.Select(x => new RoleDeptEntity { DeptId = x, RoleId = input.RoleId })
.ToList();
await _roleDeptRepository.InsertRangeAsync(insertEntities);
}
var entity = new RoleAggregateRoot() { DataScope = input.DataScope };
EntityHelper.TrySetId(entity, () => input.RoleId);
await _repository._Db.Updateable(entity).UpdateColumns(x => x.DataScope).ExecuteCommandAsync();
}
public override async Task<PagedResultDto<RoleGetListOutputDto>> GetListAsync(RoleGetListInputVo input)
{
RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.RoleCode), x => x.RoleCode.Contains(input.RoleCode!))
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.RoleCode),
x => x.RoleCode.Contains(input.RoleCode!))
.WhereIF(!string.IsNullOrEmpty(input.RoleName), x => x.RoleName.Contains(input.RoleName!))
.WhereIF(input.State is not null, x => x.State == input.State)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
.WhereIF(input.State is not null, x => x.State == input.State)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<RoleGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
}
@@ -68,15 +74,16 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <returns></returns>
public override async Task<RoleGetOutputDto> CreateAsync(RoleCreateInputVo input)
{
RoleGetOutputDto outputDto;
//using (var uow = _unitOfWorkManager.CreateContext())
//{
var isExist =
await _repository.IsAnyAsync(x => x.RoleCode == input.RoleCode || x.RoleName == input.RoleName);
if (isExist)
{
throw new UserFriendlyException(RoleConst.Exist);
}
var entity = await MapToEntityAsync(input);
await _repository.InsertAsync(entity);
outputDto = await MapToGetOutputDtoAsync(entity);
await _roleManager.GiveRoleSetMenuAsync(new List<Guid> { entity.Id }, input.MenuIds);
// uow.Commit();
//}
var outputDto = await MapToGetOutputDtoAsync(entity);
return outputDto;
}
@@ -89,18 +96,20 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <returns></returns>
public override async Task<RoleGetOutputDto> UpdateAsync(Guid id, RoleUpdateInputVo input)
{
var dto = new RoleGetOutputDto();
//using (var uow = _unitOfWorkManager.CreateContext())
//{
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);
if (isExist)
{
throw new UserFriendlyException(RoleConst.Exist);
}
await MapToEntityAsync(input, entity);
await _repository.UpdateAsync(entity);
await _roleManager.GiveRoleSetMenuAsync(new List<Guid> { id }, input.MenuIds);
dto = await MapToGetOutputDtoAsync(entity);
// uow.Commit();
//}
var dto = await MapToGetOutputDtoAsync(entity);
return dto;
}
@@ -134,7 +143,8 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <param name="isAllocated">是否在该角色下</param>
/// <returns></returns>
[Route("role/auth-user/{roleId}/{isAllocated}")]
public async Task<PagedResultDto<UserGetListOutputDto>> GetAuthUserByRoleIdAsync([FromRoute] Guid roleId, [FromRoute] bool isAllocated, [FromQuery] RoleAuthUserGetListInput input)
public async Task<PagedResultDto<UserGetListOutputDto>> GetAuthUserByRoleIdAsync([FromRoute] Guid roleId,
[FromRoute] bool isAllocated, [FromQuery] RoleAuthUserGetListInput input)
{
PagedResultDto<UserGetListOutputDto> output;
//角色下已授权用户
@@ -147,30 +157,34 @@ namespace Yi.Framework.Rbac.Application.Services.System
{
output = await GetNotAllocatedAuthUserByRoleIdAsync(roleId, input);
}
return output;
}
private async Task<PagedResultDto<UserGetListOutputDto>> GetAllocatedAuthUserByRoleIdAsync(Guid roleId, RoleAuthUserGetListInput input)
private async Task<PagedResultDto<UserGetListOutputDto>> GetAllocatedAuthUserByRoleIdAsync(Guid roleId,
RoleAuthUserGetListInput input)
{
RefAsync<int> total = 0;
var output = await _userRoleRepository._DbQueryable
.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);
.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);
return new PagedResultDto<UserGetListOutputDto>(total, output);
}
private async Task<PagedResultDto<UserGetListOutputDto>> GetNotAllocatedAuthUserByRoleIdAsync(Guid roleId, RoleAuthUserGetListInput input)
private async Task<PagedResultDto<UserGetListOutputDto>> GetNotAllocatedAuthUserByRoleIdAsync(Guid roleId,
RoleAuthUserGetListInput input)
{
RefAsync<int> total = 0;
var entities = await _userRoleRepository._Db.Queryable<UserAggregateRoot>()
.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);
.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);
var output = entities.Adapt<List<UserGetListOutputDto>>();
return new PagedResultDto<UserGetListOutputDto>(total, output);
}
@@ -183,7 +197,8 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <returns></returns>
public async Task CreateAuthUserAsync(RoleAuthUserCreateOrDeleteInput input)
{
var userRoleEntities = input.UserIds.Select(u => new UserRoleEntity { RoleId = input.RoleId, UserId = u }).ToList();
var userRoleEntities = input.UserIds.Select(u => new UserRoleEntity { RoleId = input.RoleId, UserId = u })
.ToList();
await _userRoleRepository.InsertRangeAsync(userRoleEntities);
}
@@ -197,7 +212,8 @@ namespace Yi.Framework.Rbac.Application.Services.System
{
await _userRoleRepository._Db.Deleteable<UserRoleEntity>().Where(x => x.RoleId == input.RoleId)
.Where(x => input.UserIds.Contains(x.UserId))
.ExecuteCommandAsync(); ;
.ExecuteCommandAsync();
;
}
}
}
}

View File

@@ -127,6 +127,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[Permission("system:user:list")]
public override async Task<UserGetOutputDto> GetAsync(Guid id)
{
//使用导航树形查询
@@ -153,7 +154,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
if (await _repository.IsAnyAsync(u => input.UserName!.Equals(u.UserName) && !id.Equals(u.Id)))
{
throw new UserFriendlyException("用户已经存在,更新失败");
throw new UserFriendlyException(UserConst.Exist);
}
var entity = await _repository.GetByIdAsync(id);