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

@@ -8,9 +8,11 @@ using Volo.Abp.Domain.Repositories;
namespace Yi.Framework.Ddd.Application namespace Yi.Framework.Ddd.Application
{ {
public abstract class YiCrudAppService<TEntity, TEntityDto, TKey> : YiCrudAppService<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto> public abstract class
where TEntity : class, IEntity<TKey> YiCrudAppService<TEntity, TEntityDto, TKey> : YiCrudAppService<TEntity, TEntityDto, TKey,
where TEntityDto : IEntityDto<TKey> PagedAndSortedResultRequestDto>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{ {
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository) protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{ {
@@ -49,16 +51,53 @@ namespace Yi.Framework.Ddd.Application
} }
public abstract class YiCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput> public abstract class YiCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput,
TUpdateInput>
: CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput> : CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey> where TEntity : class, IEntity<TKey>
where TGetOutputDto : IEntityDto<TKey> where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey> where TGetListOutputDto : IEntityDto<TKey>
{ {
protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository) protected YiCrudAppService(IRepository<TEntity, TKey> repository) : base(repository)
{ {
} }
public override async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
{
await CheckUpdatePolicyAsync();
var entity = await GetEntityByIdAsync(id);
await CheckUpdateInputDtoAsync(entity,input);
await MapToEntityAsync(input, entity);
await Repository.UpdateAsync(entity, autoSave: true);
return await MapToGetOutputDtoAsync(entity);
}
protected virtual Task CheckUpdateInputDtoAsync(TEntity entity,TUpdateInput input)
{
return Task.CompletedTask;
}
public override async Task<TGetOutputDto> CreateAsync(TCreateInput input)
{
await CheckCreatePolicyAsync();
await CheckCreateInputDtoAsync(input);
var entity = await MapToEntityAsync(input);
TryToSetTenantId(entity);
await Repository.InsertAsync(entity, autoSave: true);
return await MapToGetOutputDtoAsync(entity);
}
protected virtual Task CheckCreateInputDtoAsync(TCreateInput input)
{
return Task.CompletedTask;
}
/// <summary> /// <summary>
/// 多查 /// 多查
/// </summary> /// </summary>
@@ -70,12 +109,14 @@ namespace Yi.Framework.Ddd.Application
//区分多查还是批量查 //区分多查还是批量查
if (input is IPagedResultRequest pagedInput) if (input is IPagedResultRequest pagedInput)
{ {
entites = await Repository.GetPagedListAsync(pagedInput.SkipCount, pagedInput.MaxResultCount, string.Empty); entites = await Repository.GetPagedListAsync(pagedInput.SkipCount, pagedInput.MaxResultCount,
string.Empty);
} }
else else
{ {
entites = await Repository.GetListAsync(); entites = await Repository.GetListAsync();
} }
var total = await Repository.GetCountAsync(); var total = await Repository.GetCountAsync();
var output = await MapToGetListOutputDtosAsync(entites); var output = await MapToGetListOutputDtosAsync(entites);
return new PagedResultDto<TGetListOutputDto>(total, output); return new PagedResultDto<TGetListOutputDto>(total, output);
@@ -146,4 +187,4 @@ namespace Yi.Framework.Ddd.Application
//await Repository.InsertManyAsync(entities); //await Repository.InsertManyAsync(entities);
} }
} }
} }

View File

@@ -18,7 +18,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{ {
public ISqlSugarClient _Db => GetDbContextAsync().Result; public ISqlSugarClient _Db => GetDbContextAsync().Result;
public ISugarQueryable<TEntity> _DbQueryable => GetDbContextAsync().Result.Queryable<TEntity>(); public ISugarQueryable<TEntity> _DbQueryable => GetDbContextAsync().ConfigureAwait(false).GetAwaiter().GetResult().Queryable<TEntity>();
private ISugarDbContextProvider<ISqlSugarDbContext> _sugarDbContextProvider; private ISugarDbContextProvider<ISqlSugarDbContext> _sugarDbContextProvider;
public IAsyncQueryableExecuter AsyncExecuter { get; } public IAsyncQueryableExecuter AsyncExecuter { get; }

View File

@@ -5,6 +5,7 @@ using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Config; using Yi.Framework.Rbac.Application.Contracts.Dtos.Config;
using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services namespace Yi.Framework.Rbac.Application.Services
@@ -12,10 +13,12 @@ namespace Yi.Framework.Rbac.Application.Services
/// <summary> /// <summary>
/// Config服务实现 /// Config服务实现
/// </summary> /// </summary>
public class ConfigService : YiCrudAppService<ConfigAggregateRoot, ConfigGetOutputDto, ConfigGetListOutputDto, Guid, ConfigGetListInputVo, ConfigCreateInputVo, ConfigUpdateInputVo>, public class ConfigService : YiCrudAppService<ConfigAggregateRoot, ConfigGetOutputDto, ConfigGetListOutputDto, Guid,
IConfigService ConfigGetListInputVo, ConfigCreateInputVo, ConfigUpdateInputVo>,
IConfigService
{ {
private ISqlSugarRepository<ConfigAggregateRoot, Guid> _repository; private ISqlSugarRepository<ConfigAggregateRoot, Guid> _repository;
public ConfigService(ISqlSugarRepository<ConfigAggregateRoot, Guid> repository) : base(repository) public ConfigService(ISqlSugarRepository<ConfigAggregateRoot, Guid> repository) : base(repository)
{ {
_repository = repository; _repository = repository;
@@ -30,11 +33,33 @@ namespace Yi.Framework.Rbac.Application.Services
{ {
RefAsync<int> total = 0; RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.ConfigKey), x => x.ConfigKey.Contains(input.ConfigKey!)) var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.ConfigKey),
.WhereIF(!string.IsNullOrEmpty(input.ConfigName), x => x.ConfigName!.Contains(input.ConfigName!)) x => x.ConfigKey.Contains(input.ConfigKey!))
.WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime) .WhereIF(!string.IsNullOrEmpty(input.ConfigName), x => x.ConfigName!.Contains(input.ConfigName!))
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); .WhereIF(input.StartTime is not null && input.EndTime is not null,
x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<ConfigGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities)); return new PagedResultDto<ConfigGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
} }
protected override async Task CheckCreateInputDtoAsync(ConfigCreateInputVo input)
{
var isExist =
await _repository.IsAnyAsync(x => x.ConfigKey == input.ConfigKey);
if (isExist)
{
throw new UserFriendlyException(ConfigConst.Exist);
}
}
protected override async Task CheckUpdateInputDtoAsync(ConfigAggregateRoot entity, ConfigUpdateInputVo input)
{
var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id)
.AnyAsync(x => x.ConfigKey == input.ConfigKey);
if (isExist)
{
throw new UserFriendlyException(ConfigConst.Exist);
}
}
} }
} }

View File

@@ -6,6 +6,7 @@ using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Dictionary; using Yi.Framework.Rbac.Application.Contracts.Dtos.Dictionary;
using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
@@ -14,26 +15,30 @@ namespace Yi.Framework.Rbac.Application.Services
/// <summary> /// <summary>
/// Dictionary服务实现 /// Dictionary服务实现
/// </summary> /// </summary>
public class DictionaryService : YiCrudAppService<DictionaryEntity, DictionaryGetOutputDto, DictionaryGetListOutputDto, Guid, DictionaryGetListInputVo, DictionaryCreateInputVo, DictionaryUpdateInputVo>, public class DictionaryService : YiCrudAppService<DictionaryEntity, DictionaryGetOutputDto,
IDictionaryService DictionaryGetListOutputDto, Guid, DictionaryGetListInputVo, DictionaryCreateInputVo,
DictionaryUpdateInputVo>,
IDictionaryService
{ {
private ISqlSugarRepository<DictionaryEntity, Guid> _repository; private ISqlSugarRepository<DictionaryEntity, Guid> _repository;
public DictionaryService(ISqlSugarRepository<DictionaryEntity, Guid> repository) : base(repository) public DictionaryService(ISqlSugarRepository<DictionaryEntity, Guid> repository) : base(repository)
{ {
_repository= repository; _repository = repository;
} }
/// <summary> /// <summary>
/// 查询 /// 查询
/// </summary> /// </summary>
public override async Task<PagedResultDto<DictionaryGetListOutputDto>> GetListAsync(
public override async Task<PagedResultDto<DictionaryGetListOutputDto>> GetListAsync(DictionaryGetListInputVo input) DictionaryGetListInputVo input)
{ {
RefAsync<int> total = 0; RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(input.DictType is not null, x => x.DictType == input.DictType) var entities = await _repository._DbQueryable
.WhereIF(input.DictLabel is not null, x => x.DictLabel!.Contains(input.DictLabel!)) .WhereIF(input.DictType is not null, x => x.DictType == input.DictType)
.WhereIF(input.State is not null, x => x.State == input.State) .WhereIF(input.DictLabel is not null, x => x.DictLabel!.Contains(input.DictLabel!))
.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<DictionaryGetListOutputDto> return new PagedResultDto<DictionaryGetListOutputDto>
{ {
TotalCount = total, TotalCount = total,
@@ -55,4 +60,4 @@ namespace Yi.Framework.Rbac.Application.Services
return result; return result;
} }
} }
} }

View File

@@ -6,6 +6,7 @@ using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.DictionaryType; using Yi.Framework.Rbac.Application.Contracts.Dtos.DictionaryType;
using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services namespace Yi.Framework.Rbac.Application.Services
@@ -22,7 +23,7 @@ namespace Yi.Framework.Rbac.Application.Services
_repository = repository; _repository = repository;
} }
public async override Task<PagedResultDto<DictionaryTypeGetListOutputDto>> GetListAsync(DictionaryTypeGetListInputVo input) public override async Task<PagedResultDto<DictionaryTypeGetListOutputDto>> GetListAsync(DictionaryTypeGetListInputVo input)
{ {
RefAsync<int> total = 0; RefAsync<int> total = 0;
@@ -38,5 +39,25 @@ namespace Yi.Framework.Rbac.Application.Services
Items = await MapToGetListOutputDtosAsync(entities) Items = await MapToGetListOutputDtosAsync(entities)
}; };
} }
protected override async Task CheckCreateInputDtoAsync(DictionaryTypeCreateInputVo input)
{
var isExist =
await _repository.IsAnyAsync(x => x.DictType == input.DictType);
if (isExist)
{
throw new UserFriendlyException(DictionaryConst.Exist);
}
}
protected override async Task CheckUpdateInputDtoAsync(DictionaryTypeAggregateRoot entity, DictionaryTypeUpdateInputVo input)
{
var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id)
.AnyAsync(x => x.DictType == input.DictType);
if (isExist)
{
throw new UserFriendlyException(DictionaryConst.Exist);
}
}
} }
} }

View File

@@ -41,8 +41,6 @@ namespace Yi.Framework.Rbac.Application.Services
return new PagedResultDto<NoticeGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities)); return new PagedResultDto<NoticeGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
} }
/// <summary> /// <summary>
/// 发送在线消息 /// 发送在线消息
/// </summary> /// </summary>

View File

@@ -1,28 +1,31 @@
using SqlSugar; using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Yi.Framework.Ddd.Application; using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Dept; using Yi.Framework.Rbac.Application.Contracts.Dtos.Dept;
using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Repositories; using Yi.Framework.Rbac.Domain.Repositories;
using Yi.Framework.Rbac.Domain.Shared.Consts;
namespace Yi.Framework.Rbac.Application.Services.System namespace Yi.Framework.Rbac.Application.Services.System
{ {
/// <summary> /// <summary>
/// Dept服务实现 /// Dept服务实现
/// </summary> /// </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; private IDeptRepository _repository;
public DeptService(IDeptRepository deptRepository) : base(deptRepository)
{ _deptRepository = deptRepository; } public DeptService(IDeptRepository repository) : base(repository)
{
_repository = repository;
}
[RemoteService(false)] [RemoteService(false)]
public async Task<List<Guid>> GetChildListAsync(Guid deptId) public async Task<List<Guid>> GetChildListAsync(Guid deptId)
{ {
return await _deptRepository.GetChildListAsync(deptId); return await _repository.GetChildListAsync(deptId);
} }
/// <summary> /// <summary>
@@ -32,7 +35,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
//[Route("{roleId}")] //[Route("{roleId}")]
public async Task<List<DeptGetListOutputDto>> GetRoleIdAsync(Guid 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); return await MapToGetListOutputDtosAsync(entities);
} }
@@ -44,16 +47,36 @@ namespace Yi.Framework.Rbac.Application.Services.System
public override async Task<PagedResultDto<DeptGetListOutputDto>> GetListAsync(DeptGetListInputVo input) public override async Task<PagedResultDto<DeptGetListOutputDto>> GetListAsync(DeptGetListInputVo input)
{ {
RefAsync<int> total = 0; RefAsync<int> total = 0;
var entities = await _deptRepository._DbQueryable var entities = await _repository._DbQueryable
.WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!)) .WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!))
.WhereIF(input.State is not null, u => u.State == input.State) .WhereIF(input.State is not null, u => u.State == input.State)
.OrderBy(u => u.OrderNum, OrderByType.Asc) .OrderBy(u => u.OrderNum, OrderByType.Asc)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); .ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<DeptGetListOutputDto> return new PagedResultDto<DeptGetListOutputDto>
{ {
Items = await MapToGetListOutputDtosAsync(entities), Items = await MapToGetListOutputDtosAsync(entities),
TotalCount = total 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 SqlSugar;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Yi.Framework.Ddd.Application; using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Menu; using Yi.Framework.Rbac.Application.Contracts.Dtos.Menu;
using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services.System 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) public override async Task<PagedResultDto<MenuGetListOutputDto>> GetListAsync(MenuGetListInputVo input)
{ {
RefAsync<int> total = 0; RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.MenuName), x => x.MenuName.Contains(input.MenuName!)) 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) .WhereIF(input.State is not null, x => x.State == input.State)
.Where(x=>x.MenuSource==input.MenuSource) .Where(x=>x.MenuSource==input.MenuSource)
.OrderByDescending(x => x.OrderNum) .OrderByDescending(x => x.OrderNum)
.ToListAsync(); .ToListAsync();
//.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<MenuGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities)); return new PagedResultDto<MenuGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
} }
@@ -46,15 +43,5 @@ namespace Yi.Framework.Rbac.Application.Services.System
return await MapToGetListOutputDtosAsync(entities); 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 SqlSugar;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Yi.Framework.Ddd.Application; using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Post; using Yi.Framework.Rbac.Application.Contracts.Dtos.Post;
using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services.System namespace Yi.Framework.Rbac.Application.Services.System
@@ -12,10 +12,12 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <summary> /// <summary>
/// Post服务实现 /// Post服务实现
/// </summary> /// </summary>
public class PostService : YiCrudAppService<PostAggregateRoot, PostGetOutputDto, PostGetListOutputDto, Guid, PostGetListInputVo, PostCreateInputVo, PostUpdateInputVo>, public class PostService : YiCrudAppService<PostAggregateRoot, PostGetOutputDto, PostGetListOutputDto, Guid,
IPostService PostGetListInputVo, PostCreateInputVo, PostUpdateInputVo>,
IPostService
{ {
private readonly ISqlSugarRepository<PostAggregateRoot, Guid> _repository; private readonly ISqlSugarRepository<PostAggregateRoot, Guid> _repository;
public PostService(ISqlSugarRepository<PostAggregateRoot, Guid> repository) : base(repository) public PostService(ISqlSugarRepository<PostAggregateRoot, Guid> repository) : base(repository)
{ {
_repository = repository; _repository = repository;
@@ -25,10 +27,31 @@ namespace Yi.Framework.Rbac.Application.Services.System
{ {
RefAsync<int> total = 0; RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.PostName), x => x.PostName.Contains(input.PostName!)) var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.PostName),
.WhereIF(input.State is not null, x => x.State == input.State) x => x.PostName.Contains(input.PostName!))
.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<PostGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities)); 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.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Managers; using Yi.Framework.Rbac.Domain.Managers;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.Rbac.Domain.Shared.Enums; using Yi.Framework.Rbac.Domain.Shared.Enums;
using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Abstractions;
@@ -19,13 +20,16 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <summary> /// <summary>
/// Role服务实现 /// Role服务实现
/// </summary> /// </summary>
public class RoleService : YiCrudAppService<RoleAggregateRoot, RoleGetOutputDto, RoleGetListOutputDto, Guid, RoleGetListInputVo, RoleCreateInputVo, RoleUpdateInputVo>, public class RoleService : YiCrudAppService<RoleAggregateRoot, RoleGetOutputDto, RoleGetListOutputDto, Guid,
IRoleService 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); (roleManager, roleDeptRepository, userRoleRepository, repository);
} }
private ISqlSugarRepository<RoleAggregateRoot, Guid> _repository; private ISqlSugarRepository<RoleAggregateRoot, Guid> _repository;
@@ -41,23 +45,25 @@ namespace Yi.Framework.Rbac.Application.Services.System
if (input.DataScope == DataScopeEnum.CUSTOM) if (input.DataScope == DataScopeEnum.CUSTOM)
{ {
await _roleDeptRepository.DeleteAsync(x => x.RoleId == input.RoleId); 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); await _roleDeptRepository.InsertRangeAsync(insertEntities);
} }
var entity = new RoleAggregateRoot() { DataScope = input.DataScope }; var entity = new RoleAggregateRoot() { DataScope = input.DataScope };
EntityHelper.TrySetId(entity, () => input.RoleId); EntityHelper.TrySetId(entity, () => input.RoleId);
await _repository._Db.Updateable(entity).UpdateColumns(x => x.DataScope).ExecuteCommandAsync(); await _repository._Db.Updateable(entity).UpdateColumns(x => x.DataScope).ExecuteCommandAsync();
} }
public override async Task<PagedResultDto<RoleGetListOutputDto>> GetListAsync(RoleGetListInputVo input) public override async Task<PagedResultDto<RoleGetListOutputDto>> GetListAsync(RoleGetListInputVo input)
{ {
RefAsync<int> total = 0; 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(!string.IsNullOrEmpty(input.RoleName), x => x.RoleName.Contains(input.RoleName!))
.WhereIF(input.State is not null, x => x.State == input.State) .WhereIF(input.State is not null, x => x.State == input.State)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); .ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<RoleGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities)); return new PagedResultDto<RoleGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
} }
@@ -68,15 +74,16 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <returns></returns> /// <returns></returns>
public override async Task<RoleGetOutputDto> CreateAsync(RoleCreateInputVo input) public override async Task<RoleGetOutputDto> CreateAsync(RoleCreateInputVo input)
{ {
RoleGetOutputDto outputDto; var isExist =
//using (var uow = _unitOfWorkManager.CreateContext()) await _repository.IsAnyAsync(x => x.RoleCode == input.RoleCode || x.RoleName == input.RoleName);
//{ if (isExist)
{
throw new UserFriendlyException(RoleConst.Exist);
}
var entity = await MapToEntityAsync(input); var entity = await MapToEntityAsync(input);
await _repository.InsertAsync(entity); await _repository.InsertAsync(entity);
outputDto = await MapToGetOutputDtoAsync(entity); var outputDto = await MapToGetOutputDtoAsync(entity);
await _roleManager.GiveRoleSetMenuAsync(new List<Guid> { entity.Id }, input.MenuIds);
// uow.Commit();
//}
return outputDto; return outputDto;
} }
@@ -89,18 +96,20 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <returns></returns> /// <returns></returns>
public override async Task<RoleGetOutputDto> UpdateAsync(Guid id, RoleUpdateInputVo input) 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 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 MapToEntityAsync(input, entity);
await _repository.UpdateAsync(entity); await _repository.UpdateAsync(entity);
await _roleManager.GiveRoleSetMenuAsync(new List<Guid> { id }, input.MenuIds); await _roleManager.GiveRoleSetMenuAsync(new List<Guid> { id }, input.MenuIds);
dto = await MapToGetOutputDtoAsync(entity); var dto = await MapToGetOutputDtoAsync(entity);
// uow.Commit();
//}
return dto; return dto;
} }
@@ -134,7 +143,8 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <param name="isAllocated">是否在该角色下</param> /// <param name="isAllocated">是否在该角色下</param>
/// <returns></returns> /// <returns></returns>
[Route("role/auth-user/{roleId}/{isAllocated}")] [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; PagedResultDto<UserGetListOutputDto> output;
//角色下已授权用户 //角色下已授权用户
@@ -147,30 +157,34 @@ namespace Yi.Framework.Rbac.Application.Services.System
{ {
output = await GetNotAllocatedAuthUserByRoleIdAsync(roleId, input); output = await GetNotAllocatedAuthUserByRoleIdAsync(roleId, input);
} }
return output; 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; RefAsync<int> total = 0;
var output = await _userRoleRepository._DbQueryable var output = await _userRoleRepository._DbQueryable
.LeftJoin<UserAggregateRoot>((ur, u) => ur.UserId == u.Id && ur.RoleId == roleId) .LeftJoin<UserAggregateRoot>((ur, u) => ur.UserId == u.Id && ur.RoleId == roleId)
.Where((ur, u) => ur.RoleId == roleId) .Where((ur, u) => ur.RoleId == roleId)
.WhereIF(!string.IsNullOrEmpty(input.UserName), (ur, u) => u.UserName.Contains(input.UserName)) .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())) .WhereIF(input.Phone is not null, (ur, u) => u.Phone.ToString().Contains(input.Phone.ToString()))
.Select((ur, u) => new UserGetListOutputDto { Id = u.Id }, true) .Select((ur, u) => new UserGetListOutputDto { Id = u.Id }, true)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); .ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<UserGetListOutputDto>(total, output); 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; RefAsync<int> total = 0;
var entities = await _userRoleRepository._Db.Queryable<UserAggregateRoot>() var entities = await _userRoleRepository._Db.Queryable<UserAggregateRoot>()
.Where(u => SqlFunc.Subqueryable<UserRoleEntity>().Where(x => x.RoleId == roleId).Where(x => x.UserId == u.Id).NotAny()) .Where(u => SqlFunc.Subqueryable<UserRoleEntity>().Where(x => x.RoleId == roleId)
.WhereIF(!string.IsNullOrEmpty(input.UserName), u => u.UserName.Contains(input.UserName)) .Where(x => x.UserId == u.Id).NotAny())
.WhereIF(input.Phone is not null, u => u.Phone.ToString().Contains(input.Phone.ToString())) .WhereIF(!string.IsNullOrEmpty(input.UserName), u => u.UserName.Contains(input.UserName))
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); .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>>(); var output = entities.Adapt<List<UserGetListOutputDto>>();
return new PagedResultDto<UserGetListOutputDto>(total, output); return new PagedResultDto<UserGetListOutputDto>(total, output);
} }
@@ -183,7 +197,8 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// <returns></returns> /// <returns></returns>
public async Task CreateAuthUserAsync(RoleAuthUserCreateOrDeleteInput input) 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); 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) await _userRoleRepository._Db.Deleteable<UserRoleEntity>().Where(x => x.RoleId == input.RoleId)
.Where(x => input.UserIds.Contains(x.UserId)) .Where(x => input.UserIds.Contains(x.UserId))
.ExecuteCommandAsync(); ; .ExecuteCommandAsync();
;
} }
} }
} }

View File

@@ -127,6 +127,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
[Permission("system:user:list")]
public override async Task<UserGetOutputDto> GetAsync(Guid id) 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))) 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); var entity = await _repository.GetByIdAsync(id);

View File

@@ -1,120 +0,0 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Shared.Options;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services
{
/// <summary>
/// 测试文档控制器
/// </summary>
public class TestServcie : ApplicationService
{
private IRepository<StudentEntity> _repository;
private IUnitOfWorkManager _unitOfWork;
private ISqlSugarRepository<StudentEntity> _sqlsugarRepository;
public IOptions<JwtOptions> options { get; set; }
public TestServcie(IRepository<StudentEntity> repository, IUnitOfWorkManager unitOfWork, ISqlSugarRepository<StudentEntity> sqlsugarRepository, IRepository<StudentEntity, Guid> repository2)
{
_unitOfWork = unitOfWork;
_repository = repository;
_sqlsugarRepository = sqlsugarRepository;
}
/// <summary>
/// 你好,多线程
/// </summary>
/// <returns></returns>
public async Task<string> GetTaskTest()
{
var tasks = Enumerable.Range(0, 2).Select(x =>
{
return Task.Run(async () =>
{
using (var uow = _unitOfWork.Begin(true))
{
// await _repository.GetListAsync();
await _sqlsugarRepository._DbQueryable.ToListAsync();
await uow.CompleteAsync();
}
});
}).ToList();
await Task.WhenAll(tasks);
return "你哈";
}
[Authorize]
public async Task<List<StudentEntity>> GetTest()
{
//using (var uow = _unitOfWork.Begin(true))
//{
var data = await _repository.GetListAsync();
var data2 = await _repository.GetListAsync();
//await uow.CompleteAsync();
return data;
//}
}
//[UnitOfWork]
public async Task<StudentEntity> PostTest()
{
//using (var uow = _unitOfWork.Begin())
//{
var stu = new StudentEntity() { Name = $"{DateTime.Now.ToString()}你好" };
var data = await _repository.InsertAsync(stu);
//await uow.CompleteAsync();
return data;
//}
}
public async Task<StudentEntity> PostError()
{
throw new ApplicationException();
}
public async Task<StudentEntity> PostUserError()
{
throw new UserFriendlyException("直接爆炸");
}
public string Login()
{
var data = options.Value;
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(data.SecurityKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var claims = new List<Claim>()
{
new Claim("name","admin")
};
var token = new JwtSecurityToken(
issuer: data.Issuer,
audience: data.Audience,
claims: claims,
expires: DateTime.Now.AddSeconds(60 * 60 * 2),
notBefore: DateTime.Now,
signingCredentials: creds);
string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
return returnToken;
}
}
}

View File

@@ -0,0 +1,6 @@
namespace Yi.Framework.Rbac.Domain.Shared.Consts;
public class ConfigConst
{
public const string Exist = "该配置已经存在";
}

View File

@@ -12,5 +12,6 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts
public class DeptConst public class DeptConst
{ {
public const string Exist = "该部门已经存在";
} }
} }

View File

@@ -0,0 +1,6 @@
namespace Yi.Framework.Rbac.Domain.Shared.Consts;
public class DictionaryConst
{
public const string Exist = "该字典已经存在";
}

View File

@@ -12,5 +12,6 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts
public class MenuConst public class MenuConst
{ {
public const string Exist = "该菜单已经存在";
} }
} }

View File

@@ -12,5 +12,6 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts
public class PostConst public class PostConst
{ {
public const string Exist = "该岗位已经存在";
} }
} }

View File

@@ -12,5 +12,6 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts
public class RoleConst public class RoleConst
{ {
public const string Exist = "该角色已经存在";
} }
} }

View File

@@ -16,7 +16,7 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts
public const string Login_User_No_Exist = "登录失败!用户名不存在!"; public const string Login_User_No_Exist = "登录失败!用户名不存在!";
public const string Login_Passworld_Error = "密码为空,添加失败!"; public const string Login_Passworld_Error = "密码为空,添加失败!";
public const string Create_Passworld_Error = "密码格式错误长度需大于等于6位"; public const string Create_Passworld_Error = "密码格式错误长度需大于等于6位";
public const string User_Exist = "用户已经存在,创建失败!"; public const string Exist = "用户已经存在,创建失败!";
public const string State_Is_State = "该用户已被禁用,请联系管理员进行恢复"; public const string State_Is_State = "该用户已被禁用,请联系管理员进行恢复";
public const string No_Permission = "登录禁用!该用户分配无任何权限,无意义登录!"; public const string No_Permission = "登录禁用!该用户分配无任何权限,无意义登录!";
public const string No_Role = "登录禁用!该用户分配无任何角色,无意义登录!"; public const string No_Role = "登录禁用!该用户分配无任何角色,无意义登录!";

View File

@@ -1,13 +0,0 @@
using Volo.Abp.Domain.Entities;
namespace Yi.Framework.Rbac.Domain.Entities
{
public class StudentEntity : Entity<Guid>
{
[SqlSugar.SugarColumn(IsPrimaryKey = true)]
public override Guid Id { get; protected set; }
public string Name { get; set; }
}
}

View File

@@ -1,16 +0,0 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
using Yi.Framework.Rbac.Domain.Entities;
namespace Yi.Framework.Rbac.Domain.EventHandlers
{
public class StudentEventHandler : ILocalEventHandler<EntityCreatedEventData<StudentEntity>>, ITransientDependency
{
public Task HandleEventAsync(EntityCreatedEventData<StudentEntity> eventData)
{
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(eventData.Entity));
return Task.CompletedTask;
}
}
}

View File

@@ -118,7 +118,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
var isExist = await _repository.IsAnyAsync(x => x.UserName == userEntity.UserName); var isExist = await _repository.IsAnyAsync(x => x.UserName == userEntity.UserName);
if (isExist) if (isExist)
{ {
throw new UserFriendlyException(UserConst.User_Exist); throw new UserFriendlyException(UserConst.Exist);
} }
var entity = await _repository.InsertReturnEntityAsync(userEntity); var entity = await _repository.InsertReturnEntityAsync(userEntity);

View File

@@ -55,7 +55,7 @@ namespace Yi.Framework.Rbac.Test.System
} }
catch (UserFriendlyException ex) catch (UserFriendlyException ex)
{ {
ex.Message.ShouldBe(UserConst.User_Exist); ex.Message.ShouldBe(UserConst.Exist);
} }
} }