mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-15 23:13:40 +08:00
增加同步API资源到资源列表接口
This commit is contained in:
parent
a5bbfad2ff
commit
50769b94c6
@ -38,5 +38,7 @@
|
|||||||
public const string INSTANCE_NOTICE_USER = "INSTANCE_NOTICE_USER";
|
public const string INSTANCE_NOTICE_USER = "INSTANCE_NOTICE_USER";
|
||||||
//流程实例知会角色
|
//流程实例知会角色
|
||||||
public const string INSTANCE_NOTICE_ROLE = "INSTANCE_NOTICE_ROLE";
|
public const string INSTANCE_NOTICE_ROLE = "INSTANCE_NOTICE_ROLE";
|
||||||
|
|
||||||
|
public const string API = "API_RESOURCE";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -27,6 +27,7 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.1.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.1.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||||
|
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.23" />
|
||||||
<PackageReference Include="Moq" Version="4.13.1" />
|
<PackageReference Include="Moq" Version="4.13.1" />
|
||||||
<PackageReference Include="NUnit" Version="3.13.1" />
|
<PackageReference Include="NUnit" Version="3.13.1" />
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||||
|
@ -17,6 +17,16 @@ namespace OpenAuth.App
|
|||||||
public class ResourceApp:SqlSugarBaseApp<SysResource>
|
public class ResourceApp:SqlSugarBaseApp<SysResource>
|
||||||
{
|
{
|
||||||
private RevelanceManagerApp _revelanceApp;
|
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)
|
public void Add(AddOrUpdateResReq resource)
|
||||||
{
|
{
|
||||||
@ -95,9 +105,38 @@ namespace OpenAuth.App
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResourceApp(ISqlSugarClient client, IAuth auth, RevelanceManagerApp revelanceApp) : base(client, auth)
|
/// <summary>
|
||||||
|
/// 同步站点API到资源列表
|
||||||
|
/// <para>读取站点API信息,如果资源列表中不存在,则添加</para>
|
||||||
|
/// </summary>
|
||||||
|
public async Task Sync()
|
||||||
{
|
{
|
||||||
_revelanceApp = revelanceApp;
|
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
|
||||||
|
{
|
||||||
|
Name = api.Path,
|
||||||
|
Disable = true,
|
||||||
|
SortNo = 0,
|
||||||
|
TypeId = Define.API,
|
||||||
|
TypeName = "API接口",
|
||||||
|
Description = api.Summary??"",
|
||||||
|
CreateTime = DateTime.Now,
|
||||||
|
CreateUserId = user.Id,
|
||||||
|
CreateUserName = user.Name
|
||||||
|
};
|
||||||
|
CaculateCascade(resource);
|
||||||
|
Repository.Insert(resource);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,22 +4,16 @@ using System.Linq;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Infrastructure;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.OpenApi.Readers;
|
using Microsoft.OpenApi.Readers;
|
||||||
|
|
||||||
[ApiController]
|
public class ApiService
|
||||||
[Route("api/[controller]")]
|
|
||||||
[ApiExplorerSettings(GroupName = "系统管理_System")]
|
|
||||||
public class SystemController : ControllerBase
|
|
||||||
{
|
{
|
||||||
private readonly IHttpClientFactory _httpClientFactory;
|
private readonly IHttpClientFactory _httpClientFactory;
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
public SystemController(IHttpClientFactory httpClientFactory
|
public ApiService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
|
||||||
, IConfiguration configuration)
|
|
||||||
{
|
{
|
||||||
_httpClientFactory = httpClientFactory;
|
_httpClientFactory = httpClientFactory;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
@ -27,58 +21,43 @@ public class SystemController : ControllerBase
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取所有API接口信息
|
/// 获取所有API接口信息
|
||||||
|
/// <para>这个方法单元测试必须启动WebApi站点</para>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
public async Task<List<SwaggerEndpointInfo>> GetSwaggerEndpoints()
|
||||||
[HttpGet]
|
|
||||||
[AllowAnonymous]
|
|
||||||
public async Task<Response<List<SwaggerEndpointInfo>>> Get()
|
|
||||||
{
|
{
|
||||||
var result = new Response<List<SwaggerEndpointInfo>>();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var apis = await GetSwaggerEndpoints();
|
|
||||||
|
|
||||||
result.Result = apis;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
result.Code = 500;
|
|
||||||
result.Message = ex.InnerException?.Message ?? ex.Message;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取所有API接口信息
|
|
||||||
/// </summary>
|
|
||||||
private async Task<List<SwaggerEndpointInfo>> GetSwaggerEndpoints()
|
|
||||||
{
|
|
||||||
var reader = new OpenApiStringReader();
|
var reader = new OpenApiStringReader();
|
||||||
var client = _httpClientFactory.CreateClient();
|
var client = _httpClientFactory.CreateClient();
|
||||||
|
|
||||||
var baseUrl = _configuration["AppSetting:HttpHost"]?.Replace("*", "localhost");
|
var baseUrl = _configuration["AppSetting:HttpHost"]?.Replace("*", "localhost");
|
||||||
|
|
||||||
var apis = new List<SwaggerEndpointInfo>();
|
var apis = new List<SwaggerEndpointInfo>();
|
||||||
foreach (var controller in GetControllers())
|
var controllers = GetControllers();
|
||||||
|
|
||||||
|
foreach (var controller in controllers)
|
||||||
{
|
{
|
||||||
var groupname = GetSwaggerGroupName(controller);
|
var groupname = GetSwaggerGroupName(controller);
|
||||||
|
|
||||||
var swaggerJsonUrl = $"{baseUrl}/swagger/{groupname}/swagger.json";
|
var swaggerJsonUrl = $"{baseUrl}/swagger/{groupname}/swagger.json";
|
||||||
var response = await client.GetAsync(swaggerJsonUrl);
|
|
||||||
var content = await response.Content.ReadAsStringAsync();
|
var response = await client.GetAsync(swaggerJsonUrl).ConfigureAwait(false);
|
||||||
|
|
||||||
|
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||||
var document = reader.Read(content, out var diagnostic);
|
var document = reader.Read(content, out var diagnostic);
|
||||||
//获取所有api
|
//获取所有api
|
||||||
apis.AddRange(document.Paths
|
var controllerApis = document.Paths
|
||||||
.SelectMany(path => path.Value.Operations
|
.SelectMany(path => path.Value.Operations
|
||||||
.Select(op => new SwaggerEndpointInfo(
|
.Select(op => new SwaggerEndpointInfo(
|
||||||
path.Key,
|
path.Key,
|
||||||
op.Key.ToString(),
|
op.Key.ToString(),
|
||||||
op.Value.Summary,
|
op.Value.Summary,
|
||||||
op.Value.Description,
|
op.Value.Description,
|
||||||
op.Value.Tags.FirstOrDefault()?.Name))));
|
op.Value.Tags.FirstOrDefault()?.Name)));
|
||||||
|
|
||||||
|
apis.AddRange(controllerApis);
|
||||||
}
|
}
|
||||||
|
|
||||||
return apis;
|
return apis;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -101,13 +80,16 @@ public class SystemController : ControllerBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private List<Type> GetControllers()
|
private List<Type> GetControllers()
|
||||||
{
|
{
|
||||||
Assembly asm = Assembly.GetExecutingAssembly();
|
var webApiAssembly = AppDomain.CurrentDomain.GetAssemblies()
|
||||||
|
.FirstOrDefault(a => a.GetName().Name.Contains("OpenAuth.WebApi"));
|
||||||
|
|
||||||
|
var controlleractionlist = webApiAssembly.GetTypes()
|
||||||
|
.Where(type => typeof(Microsoft.AspNetCore.Mvc.ControllerBase).IsAssignableFrom(type))
|
||||||
|
.ToList();
|
||||||
|
|
||||||
var controlleractionlist = asm.GetTypes()
|
|
||||||
.Where(type => typeof(ControllerBase).IsAssignableFrom(type))
|
|
||||||
.OrderBy(x => x.Name).ToList();
|
|
||||||
return controlleractionlist;
|
return controlleractionlist;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public record SwaggerEndpointInfo(
|
public record SwaggerEndpointInfo(
|
||||||
string Path,
|
string Path,
|
||||||
@ -115,4 +97,3 @@ public class SystemController : ControllerBase
|
|||||||
string Summary,
|
string Summary,
|
||||||
string Description,
|
string Description,
|
||||||
string Tag);
|
string Tag);
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -22,7 +22,7 @@ namespace OpenAuth.WebApi.Controllers
|
|||||||
{
|
{
|
||||||
private readonly ResourceApp _app;
|
private readonly ResourceApp _app;
|
||||||
|
|
||||||
public ResourcesController(IAuth authUtil, ResourceApp app)
|
public ResourcesController(ResourceApp app)
|
||||||
{
|
{
|
||||||
_app = app;
|
_app = app;
|
||||||
}
|
}
|
||||||
@ -32,6 +32,28 @@ namespace OpenAuth.WebApi.Controllers
|
|||||||
return await _app.Load(request);
|
return await _app.Load(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 同步站点API到资源列表
|
||||||
|
/// <para>读取站点API信息,如果资源列表中不存在,则添加</para>
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<Response> Sync()
|
||||||
|
{
|
||||||
|
var result = new Response();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _app.Sync();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
result.Code = 500;
|
||||||
|
result.Message = ex.InnerException?.Message ?? ex.Message;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Response Delete([FromBody]string[] ids)
|
public Response Delete([FromBody]string[] ids)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user