暂存字段权限更改

This commit is contained in:
2023-09-28 16:26:03 +08:00
parent 38eda262d6
commit c592925096
9 changed files with 186 additions and 124 deletions
+31 -19
View File
@@ -3,8 +3,12 @@ using Infrastructure;
using Infrastructure.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using NLog;
using SqlSugar.IOC;
using ZR.Infrastructure.Resolver;
using ZR.ServiceCore.Model;
namespace ZR.ServiceCore.Filters;
public class DataFieldFilter : ActionFilterAttribute
@@ -15,42 +19,50 @@ public class DataFieldFilter : ActionFilterAttribute
{
ResultType = resultType;
}
public DataFieldFilter()
{
}
public Type ResultType { get; set; }
public string RulePath { get; set; }
public override void OnResultExecuting(ResultExecutingContext context)
{
// var list = DbScoped.SugarScope.Queryable<PoRequirement>().ToList();
// var list = DbScoped.SugarScope.Queryable<S>()
var httpContext = context.HttpContext;
var url = httpContext.Request.Path;
// var url = httpContext.Request.Path;
var info = JwtUtil.GetLoginUser(context.HttpContext);
var roleIds = info.Roles.Select(it => it.RoleId).ToList();
// var list = DbScoped.SugarScope.Queryable<PoRequirement>().ToList();
var list = DbScoped.SugarScope.Queryable<SysField>()
.LeftJoin<SysRoleField>((sf, srf) => sf.Id == srf.FieldId)
.Where((sf, srf) => roleIds.Contains(srf.RoleId))
.ToList();
// 获取原始的结果
var originalResult = context.Result as ContentResult;
var apiResult = (JsonConvert.DeserializeObject<ApiResult>(originalResult?.Content!)) as ApiResult;
// 创建泛型类型
// var resObj = typeof(JObject).GetMethod("ToObject", Type.EmptyTypes)?
// .MakeGenericMethod(ResultType).Invoke(apiResult?.Data, null)!;
// SetPropertiesToNull(resObj, new List<string>
// {
// "Reqno"
// });
var resObj = typeof(JObject).GetMethod("ToObject", Type.EmptyTypes)?
.MakeGenericMethod(ResultType).Invoke(apiResult?["data"], null)!;
SetPropertiesToNull(resObj, list.Select(it => it.FieldName).ToList());
// var json = JsonConvert.SerializeObject(resObj, new JsonSerializerSettings
// {
// ContractResolver = new CamelCasePropertyNamesContractResolver()
// });
// var jObject = JObject.Parse(json);
// jObject.Add(new JProperty("props", new List<string>
apiResult["data"] = resObj;
var jsonRes = JsonConvert.SerializeObject(apiResult, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
// JsonConvert.SerializeObject(apiResult, new JsonSerializerSettings
// {
// "Reqno"
// }.Select(ToLowerFirstLetter).ToList()));
// apiResult.Data = jObject;
context.Result = new ContentResult{ Content = JsonConvert.SerializeObject(apiResult, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}),
// ContractResolver = new JsonPropertyContractResolver(
// list.Select(it => ToLowerFirstLetter(it.FieldName)).ToList()
// )
// })
context.Result = new ContentResult{ Content = jsonRes,
ContentType = "application/json"} ;
base.OnResultExecuting(context);
}
+2
View File
@@ -79,5 +79,7 @@ namespace ZR.ServiceCore.Model
[Navigate(NavigateType.OneToOne, nameof(CategoryId), nameof(ArticleCategory.CategoryId))] //自定义关系映射
public ArticleCategory ArticleCategoryNav { get; set; }
[SugarColumn(IsIgnore = true)]
public string IsPaa { get; set; }
}
}
@@ -1,14 +1,11 @@
using ZR.Service;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Model.Dto;
namespace ZR.ServiceCore.Services.IService;
public interface ISysFieldService : IBaseService<SysField>
{
}
public interface ISysRoleFieldService : IBaseService<SysRoleField>
{
Task<List<SysFieldDto>> GetFields(string fullName, long roleId);
Task<bool> InitFields();
}
@@ -1,6 +1,10 @@
namespace ZR.ServiceCore.Services.IService;
using ZR.Service;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Model.Dto;
public class ISysRoleFieldService
namespace ZR.ServiceCore.Services.IService;
public interface ISysRoleFieldService : IBaseService<SysRoleField>
{
Task<bool> InsertOrUpdateSysRoleField(List<SysFieldDto> sysFieldDtos, long roleId);
}
+82 -1
View File
@@ -1,12 +1,93 @@
using Infrastructure.Attribute;
using ZR.Service;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Model.Dto;
using ZR.ServiceCore.Services.IService;
namespace ZR.ServiceCore.Services;
[AppService(ServiceType = typeof(ISysFieldService), ServiceLifetime = LifeTime.Transient)]
public class SysFieldService : BaseService<SysField>, ISysFieldService
{
private readonly ISysRoleFieldService _sysRoleFieldService;
public SysFieldService(ISysRoleFieldService sysRoleFieldService)
{
_sysRoleFieldService = sysRoleFieldService;
}
public async Task<List<SysFieldDto>> GetFields(string fullName, long roleId)
{
var fields = await Queryable()
.Where(it => it.FullName == fullName)
.ToListAsync();
var roleFields = await _sysRoleFieldService
.Queryable()
.Where(it => it.RoleId == roleId)
.Select(it => it.FieldId)
.ToListAsync();
var list = fields.Select(it => new SysFieldDto
{
FieldName = it.FieldName,
FieldType = it.FieldType,
FullName = it.FullName,
Id = it.Id,
IsClass = it.IsClass,
IsPermission = !roleFields.Contains(it.Id)
}).ToList();
return list;
}
public async Task<bool> InitFields()
{
var serviceCoreModels = AppDomain.CurrentDomain
.GetAssemblies()
.First(it => it.FullName.Contains("ZR.ServiceCore"))
.ExportedTypes
.Where(p => p.FullName.StartsWith("ZR.ServiceCore.Model"))
.Select(it => new
{
it.FullName,
Properties = it.GetProperties().Select(pt => new
{
FieldName = pt.Name,
FieldType = pt.PropertyType.FullName,
IsClass = pt.PropertyType.IsClass,
IsArray = pt.PropertyType.IsArray,
// IsList = pt.PropertyType.IsClass ? pt.DeclaringType.FullName : string.Empty
}).ToList()
// Properties = it.GetProperties()
})
.ToList();
foreach (var serviceCoreModel in serviceCoreModels)
{
var sysFields = serviceCoreModel.Properties
.Select(property => new SysField
{
FieldName = property.FieldName,
FullName = serviceCoreModel.FullName,
FieldType = property.FieldType,
IsClass = property.IsClass ? "1" : "0"
}).ToList();
await Deleteable()
.Where(it =>
!sysFields.Select(sf => sf.FieldName)
.ToArray().Contains(it.FieldName))
.Where(it => it.FullName == serviceCoreModel.FullName)
.ExecuteCommandAsync();
var sysFieldStore = await Storageable(sysFields)
.WhereColumns(it => new { it.FieldName, it.FullName })
.ToStorageAsync();
await sysFieldStore
.AsInsertable
.ExecuteReturnSnowflakeIdListAsync();
await sysFieldStore
.AsUpdateable
.IgnoreColumns(it => it.Id)
.ExecuteCommandAsync();
}
return true;
}
}
+47 -1
View File
@@ -1,11 +1,57 @@
using Infrastructure.Attribute;
using ZR.Service;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Model.Dto;
using ZR.ServiceCore.Services.IService;
namespace ZR.ServiceCore.Services;
[AppService(ServiceType = typeof(ISysRoleFieldService), ServiceLifetime = LifeTime.Transient)]
public class SysRoleFieldService : BaseService<SysRoleField>, ISysRoleFieldService
{
public async Task<bool> InsertOrUpdateSysRoleField(List<SysFieldDto> sysFieldDtos, long roleId)
{
try
{
await Context.Ado.BeginTranAsync();
await Deleteable()
.Where(it => it.RoleId == roleId)
.Where(it => sysFieldDtos.Select(sf => sf.Id).ToArray()
.Contains(it.FieldId))
.ExecuteCommandAsync();
var ids = sysFieldDtos
.Where(it => it.IsPermission != true)
.Select(it => new SysRoleField
{
FieldId = it.Id,
RoleId = roleId
})
.ToList();
await Insertable(ids).ExecuteCommandAsync();
await Context.Ado.CommitTranAsync();
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
await Context.Ado.RollbackTranAsync();
throw;
}
// var list = await
// Queryable()
// .LeftJoin<SysField>((rf, f) => rf.FieldId == f.Id)
// .Where((rf, f) => rf.RoleId == roleId && f.FullName == sysFieldDtos[0].FullName)
// .ToListAsync();
//
//
// list.AddRange(ids);
// list = list.Distinct().ToList();
// var sysRoleFieldStore = await
// Storageable(ids)
// .WhereColumns(it => new { it.FieldId, it.RoleId })
// .ToStorageAsync();
// await sysRoleFieldStore.AsInsertable.ExecuteCommandAsync();
// await sysRoleFieldStore.AsUpdateable.ExecuteCommandAsync();
// await sysRoleFieldStore.AsDeleteable.ExecuteCommandAsync();
// return true;
}
}