OpenAuth.Net/OpenAuth.App/Resources/ResourceApp.cs

165 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Threading.Tasks;
using Infrastructure;
using NUnit.Framework;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using SqlSugar;
namespace OpenAuth.App
{
/// <summary>
/// 分类管理
/// </summary>
public class ResourceApp:SqlSugarBaseApp<SysResource>
{
private RevelanceManagerApp _revelanceApp;
private ApiService _apiService;
private IAuth _auth;
public ResourceApp(ISqlSugarClient client, IAuth auth, RevelanceManagerApp revelanceApp, ApiService apiService) : base(client, auth)
{
_revelanceApp = revelanceApp;
_apiService = apiService;
_auth = auth;
}
public void Add(AddOrUpdateResReq resource)
{
var obj = resource.MapTo<SysResource>();
CaculateCascade(obj);
obj.CreateTime = DateTime.Now;
var user = _auth.GetCurrentUser().User;
obj.CreateUserId = user.Id;
obj.CreateUserName = user.Name;
Repository.Insert(obj);
}
public void Update(AddOrUpdateResReq obj)
{
var user = _auth.GetCurrentUser().User;
Repository.Update(u => new SysResource
{
Name = obj.Name,
Disable = obj.Disable,
CascadeId = obj.CascadeId,
AppId = obj.AppId,
AppName = obj.AppName,
ParentId = obj.ParentId,
ParentName = obj.ParentName,
TypeId = obj.TypeId,
TypeName = obj.TypeName,
Description = obj.Description,
UpdateTime = DateTime.Now,
UpdateUserId = user.Id,
UpdateUserName = user.Name
//todo:要修改的字段赋值
},u => u.Id == obj.Id);
}
public IEnumerable<SysResource> LoadForRole(string appId, string roleId)
{
var elementIds = _revelanceApp.Get(Define.ROLERESOURCE, true, roleId);
return SugarClient.Queryable<SysResource>().Where(u => elementIds.Contains(u.Id) && (appId == null || appId =="" || u.AppId == appId)).ToArray();
}
public async Task<TableData> Load(QueryResourcesReq request)
{
var loginContext = _auth.GetCurrentUser();
if (loginContext == null)
{
throw new CommonException("登录已过期", Define.INVALID_TOKEN);
}
var columnFields = loginContext.GetTableColumns("Resource");
if (columnFields == null || columnFields.Count == 0)
{
throw new Exception("请在代码生成界面配置Resource表的字段属性");
}
var result = new TableData();
var resources = GetDataPrivilege("u");
if (!string.IsNullOrEmpty(request.key))
{
resources = resources.Where(u => u.Name.Contains(request.key) || u.Id.Contains(request.key));
}
if (!string.IsNullOrEmpty(request.appId))
{
resources = resources.Where(u => u.AppId == request.appId);
}
var columnnames = columnFields.Select(u => u.ColumnName);
var propertyStr = string.Join(',', columnnames);
result.columnFields = columnFields;
result.data = resources.OrderBy(u => u.TypeId)
.Skip((request.page - 1) * request.limit)
.Take(request.limit).Select($"{propertyStr}").ToList();
result.count = await resources.CountAsync();
return result;
}
/// <summary>
/// 获取资源类型
/// </summary>
/// <returns></returns>
public async Task<List<SysResourceApp>> GetResourceApps()
{
var types = await SugarClient.Queryable<SysResource>()
.Distinct()
.Select(u => new {u.AppId,u.AppName})
.ToListAsync();
return types.Select(u => new SysResourceApp(u.AppId, u.AppName)).ToList();
}
/// <summary>
/// 同步站点API到资源列表
/// <para>读取站点API信息如果资源列表中不存在则添加</para>
/// </summary>
public async Task Sync()
{
var apis = await _apiService.GetSwaggerEndpoints();
var user = _auth.GetCurrentUser().User;
foreach (var api in apis)
{
//检查资源是否存在
var resource = Repository.GetFirst(u => u.Name == api.Path && u.TypeId == Define.API);
if (resource != null)
{
continue;
}
resource = new SysResource
{
Id = api.Path,
Name = api.Path,
Disable = true,
SortNo = 0,
AppId = $"{Define.API}_{api.Tag}",
AppName = $"API接口-{api.Tag}",
TypeId = Define.API,
TypeName = "API接口",
Description = api.Summary??"",
CreateTime = DateTime.Now,
CreateUserId = user.Id,
CreateUserName = user.Name
};
CaculateCascade(resource);
Repository.Insert(resource);
}
}
}
/// <summary>
/// 资源类型
/// </summary>
public record SysResourceApp(string Id, string Name);
}