123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System;
- using VCommon.VApplication.Authorization;
- using VCommon.VApplication.Dto;
- using VCommon.VApplication.Linq.Expressions;
- using VCommon.VAutoMapper;
- using VCommon.VOpenApi.VAspNetCore;
- using VCommonCoreExample.AppServices.Basic;
- using VCommonCoreExample.AppServices.System.Roles.Dto;
- using VCommonCoreExample.Authorization;
- using VCommonCoreExample.Caching;
- using VCommonCoreExample.Entity;
- namespace VCommonCoreExample.AppServices.System.Roles
- {
- [VServiceAuthorize(PermissionCodes.System.Role.EntryRole)]
- public interface IRoleService
- {
- [VServiceAuthorize(PermissionCodes.System.Role.Query)]
- IPagedResult<RoleDto> List(RoleListRequest input);
- [VServiceAuthorize(
- PermissionCodes.System.Role.Create,
- PermissionCodes.System.Role.Update)]
- RoleFormPrepOutput FormPrep(Guid? input);
- [VServiceAuthorize(PermissionCodes.System.Role.Create)]
- Guid Create(RoleCreateInput input);
- [VServiceAuthorize(PermissionCodes.System.Role.Update)]
- void Update(RoleUpdateInput input);
- [VServiceAuthorize(PermissionCodes.System.Role.Delete)]
- void Delete(DeleteInput input);
- }
- public class RoleService : DbTableAppServiceBase<Role>, IRoleService
- {
- private readonly IPermissionManager _permissionManager;
- private readonly AuthCache _authCache;
- public RoleService(IPermissionManager permissionManager,AuthCache authCache)
- {
- _permissionManager = permissionManager;
- _authCache = authCache;
- }
- public IPagedResult<RoleDto> List(RoleListRequest input)
- {
- using var repo = GetRepository();
- var exp = PredicateBuilder.SearchByName(input);
- var count = repo.Count(exp);
- var items = repo.QueryNoTracking(exp)
- .OrderByCreateTimeDescAndPageBy(input)
- .ProjectionToArray<Role, RoleDto>();
- return new RoleListOutput(count, items);
- }
- public RoleFormPrepOutput FormPrep(Guid? input)
- {
- var result = new RoleFormPrepOutput(_permissionManager.GetPermissionTreeOutput(VSession.Side));
- if (input.HasValue)
- {
- using var repo = GetRepository();
- result.Output = repo.QueryProjection<RoleDto>(input.Value);
- }
- return result;
- }
- public Guid Create(RoleCreateInput input)
- {
- foreach (var s in input.PermissionSet)
- {
- if (false == _permissionManager.ValidPermission(s)) throw new VFriendlyException($"无效的权限代码:{s}");
- }
- var repo = GetRepository();
- if (repo.CheckExist(p => p.Name == input.Name)) throw new VFriendlyException("角色名称已经存在");
- var entity = input.MapTo<Role>();
- repo.Add(entity);
- repo.SaveChanges();
- return entity.Id;
- }
- public void Update(RoleUpdateInput input)
- {
- foreach (var s in input.PermissionSet)
- {
- if (false == _permissionManager.ValidPermission(s)) throw new VFriendlyException($"无效的权限代码:{s}");
- }
- var repo = GetRepository();
- if(repo.CheckExist(p => p.Id != input.Id && p.Name == input.Name)) throw new VFriendlyException("角色名称已经存在");
- var entity = repo.GetEntityOrDefault(input.Id) ?? throw new VFriendlyException("找不到角色");
- input.MapTo(entity);
- repo.SaveChanges();
- _authCache.ClearRole(VSession.TenantId,entity.Id);
- }
- public void Delete(DeleteInput input)
- {
- var repo = GetRepository();
- var entity = repo.GetEntityOrDefault(input.Id) ?? throw new VFriendlyException("找不到角色");
- repo.Delete(entity);
- repo.SaveChanges();
- _authCache.ClearRole(VSession.TenantId, input.Id);
- }
- }
- }
|