权限修改

master
he 2024-12-06 17:01:44 +08:00
parent 655e3454a3
commit be28ea0432
5 changed files with 141 additions and 115 deletions

View File

@ -204,7 +204,8 @@ namespace IRaCIS.Core.API
// 特殊异常处理 比如 404 // 特殊异常处理 比如 404
app.UseStatusCodePagesWithReExecute("/Error/{0}"); app.UseStatusCodePagesWithReExecute("/Error/{0}");
//SwaggerSetup.Configure(app, env); // SwaggerÏÔÒþ
SwaggerSetup.Configure(app, env);

View File

@ -15,128 +15,136 @@ using System.Reflection;
namespace IRaCIS.Core.API namespace IRaCIS.Core.API
{ {
public static class SwaggerSetup public static class SwaggerSetup
{ {
public static bool useSwagger = false;
public static void AddSwaggerSetup(this IServiceCollection services) public static void AddSwaggerSetup(this IServiceCollection services)
{ {
services.AddSwaggerExamplesFromAssemblyOf<JsonPatchUserRequestExample>();
services.AddSwaggerGen(options => if (useSwagger)
{ {
//此处的Name 是控制器上分组的名称 Title是界面的大标题
//分组
options.SwaggerDoc("Reviewer", new OpenApiInfo {Title = "医生模块",Version = "Reviewer", });
options.SwaggerDoc("Trial", new OpenApiInfo { Title = "项目模块", Version = "Trial" });
options.SwaggerDoc("Enroll", new OpenApiInfo { Title = "入组模块", Version = "Enroll" });
options.SwaggerDoc("Workload", new OpenApiInfo { Title = "工作量模块", Version = "Workload" });
options.SwaggerDoc("Common", new OpenApiInfo { Title = "通用信息获取", Version = "Common" });
options.SwaggerDoc("Institution", new OpenApiInfo { Title = "机构信息模块", Version = "Institution" });
options.SwaggerDoc("Dashboard&Statistics", new OpenApiInfo { Title = "统计模块", Version = "Dashboard&Statistics" });
options.SwaggerDoc("Financial", new OpenApiInfo { Title = "财务模块", Version = "Financial" });
options.SwaggerDoc("Management", new OpenApiInfo { Title = "管理模块", Version = "Management" });
options.SwaggerDoc("Image", new OpenApiInfo { Title = "影像模块", Version = "Image" });
options.SwaggerDoc("Reading", new OpenApiInfo { Title = "读片模块", Version = "Reading" });
// 接口排序 services.AddSwaggerExamplesFromAssemblyOf<JsonPatchUserRequestExample>();
options.OrderActionsBy(o => o.GroupName);
options.DocInclusionPredicate((docName, apiDes) => services.AddSwaggerGen(options =>
{ {
if (!apiDes.TryGetMethodInfo(out MethodInfo methodInfo)) return false; //此处的Name 是控制器上分组的名称 Title是界面的大标题
var versions = methodInfo.DeclaringType.GetCustomAttributes(true) //分组
.OfType<ApiExplorerSettingsAttribute>()
.Select(attr => attr.GroupName);
return versions.Any(v => v.ToString() == docName); options.SwaggerDoc("Reviewer", new OpenApiInfo { Title = "医生模块", Version = "Reviewer", });
options.SwaggerDoc("Trial", new OpenApiInfo { Title = "项目模块", Version = "Trial" });
options.SwaggerDoc("Enroll", new OpenApiInfo { Title = "入组模块", Version = "Enroll" });
options.SwaggerDoc("Workload", new OpenApiInfo { Title = "工作量模块", Version = "Workload" });
options.SwaggerDoc("Common", new OpenApiInfo { Title = "通用信息获取", Version = "Common" });
options.SwaggerDoc("Institution", new OpenApiInfo { Title = "机构信息模块", Version = "Institution" });
options.SwaggerDoc("Dashboard&Statistics", new OpenApiInfo { Title = "统计模块", Version = "Dashboard&Statistics" });
options.SwaggerDoc("Financial", new OpenApiInfo { Title = "财务模块", Version = "Financial" });
options.SwaggerDoc("Management", new OpenApiInfo { Title = "管理模块", Version = "Management" });
options.SwaggerDoc("Image", new OpenApiInfo { Title = "影像模块", Version = "Image" });
options.SwaggerDoc("Reading", new OpenApiInfo { Title = "读片模块", Version = "Reading" });
// 接口排序
options.OrderActionsBy(o => o.GroupName);
options.DocInclusionPredicate((docName, apiDes) =>
{
if (!apiDes.TryGetMethodInfo(out MethodInfo methodInfo)) return false;
var versions = methodInfo.DeclaringType.GetCustomAttributes(true)
.OfType<ApiExplorerSettingsAttribute>()
.Select(attr => attr.GroupName);
return versions.Any(v => v.ToString() == docName);
});
var xmlPath = Path.Combine(AppContext.BaseDirectory, "IRaCIS.Core.API.xml");//这个就是刚刚配置的xml文件名
options.IncludeXmlComments(xmlPath, true);
var xmlPath2 = Path.Combine(AppContext.BaseDirectory, "IRaCIS.Core.Application.xml");//这个就是刚刚配置的xml文件名
options.IncludeXmlComments(xmlPath2, true);
//默认的第二个参数是false这个是controller的注释记得修改
// 在header中添加token传递到后台
options.OperationFilter<SecurityRequirementsOperationFilter>();
options.DocumentFilter<JsonPatchDocumentFilter>();
// 添加登录按钮
options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
//In = "header",
//Type = "apiKey"
});
//// Bearer
//options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
//{
// Description = "JWT Authorization header using the Bearer scheme.",
// Name = "Authorization",
// In = ParameterLocation.Header,
// Scheme = "bearer",
// Type = SecuritySchemeType.Http,
// BearerFormat = "JWT"
//});
}); });
}
var xmlPath = Path.Combine(AppContext.BaseDirectory, "IRaCIS.Core.API.xml");//这个就是刚刚配置的xml文件名
options.IncludeXmlComments(xmlPath, true);
var xmlPath2 = Path.Combine(AppContext.BaseDirectory, "IRaCIS.Core.Application.xml");//这个就是刚刚配置的xml文件名
options.IncludeXmlComments(xmlPath2, true);
//默认的第二个参数是false这个是controller的注释记得修改
// 在header中添加token传递到后台
options.OperationFilter<SecurityRequirementsOperationFilter>();
options.DocumentFilter<JsonPatchDocumentFilter>();
// 添加登录按钮
options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
//In = "header",
//Type = "apiKey"
});
//// Bearer
//options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
//{
// Description = "JWT Authorization header using the Bearer scheme.",
// Name = "Authorization",
// In = ParameterLocation.Header,
// Scheme = "bearer",
// Type = SecuritySchemeType.Http,
// BearerFormat = "JWT"
//});
});
} }
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
app.UseDeveloperExceptionPage(); if (useSwagger)
app.UseSwagger();
app.UseSwaggerUI(options =>
{ {
//此处的Name 是页面 选择文档下拉框 显示的名称 app.UseDeveloperExceptionPage();
options.SwaggerEndpoint($"swagger/Reviewer/swagger.json", "医生模块"); app.UseSwagger();
options.SwaggerEndpoint($"swagger/Trial/swagger.json", "项目模块"); app.UseSwaggerUI(options =>
options.SwaggerEndpoint($"swagger/Enroll/swagger.json", "入组模块"); {
options.SwaggerEndpoint($"swagger/Workload/swagger.json", "工作量模块"); //此处的Name 是页面 选择文档下拉框 显示的名称
options.SwaggerEndpoint($"swagger/Dashboard&Statistics/swagger.json", "统计模块"); options.SwaggerEndpoint($"swagger/Reviewer/swagger.json", "医生模块");
options.SwaggerEndpoint($"swagger/Common/swagger.json", "通用模块"); options.SwaggerEndpoint($"swagger/Trial/swagger.json", "项目模块");
options.SwaggerEndpoint($"swagger/Enroll/swagger.json", "入组模块");
options.SwaggerEndpoint($"swagger/Financial/swagger.json", "财务模块"); options.SwaggerEndpoint($"swagger/Workload/swagger.json", "工作量模块");
options.SwaggerEndpoint($"swagger/Institution/swagger.json", "机构信息模块"); options.SwaggerEndpoint($"swagger/Dashboard&Statistics/swagger.json", "统计模块");
options.SwaggerEndpoint($"swagger/Management/swagger.json", "管理模块"); options.SwaggerEndpoint($"swagger/Common/swagger.json", "通用模块");
options.SwaggerEndpoint($"swagger/Image/swagger.json", "影像模块");
options.SwaggerEndpoint($"swagger/Reading/swagger.json", "读片模块"); options.SwaggerEndpoint($"swagger/Financial/swagger.json", "财务模块");
options.SwaggerEndpoint($"swagger/Institution/swagger.json", "机构信息模块");
options.SwaggerEndpoint($"swagger/Management/swagger.json", "管理模块");
options.SwaggerEndpoint($"swagger/Image/swagger.json", "影像模块");
options.SwaggerEndpoint($"swagger/Reading/swagger.json", "读片模块");
//路径配置设置为空表示直接在根域名localhost:8001访问该文件, //路径配置设置为空表示直接在根域名localhost:8001访问该文件,
//注意localhost:8001/swagger是访问不到的去launchSettings.json把launchUrl去掉如果你想换一个路径直接写名字即可比如直接写c.Route = "doc"; //注意localhost:8001/swagger是访问不到的去launchSettings.json把launchUrl去掉如果你想换一个路径直接写名字即可比如直接写c.Route = "doc";
//options.RoutePrefix = string.Empty; //options.RoutePrefix = string.Empty;
options.IndexStream = () => Assembly.GetExecutingAssembly() options.IndexStream = () => Assembly.GetExecutingAssembly()
.GetManifestResourceStream("IRaCIS.Core.API.wwwroot.swagger.ui.Index.html"); .GetManifestResourceStream("IRaCIS.Core.API.wwwroot.swagger.ui.Index.html");
options.RoutePrefix = string.Empty; options.RoutePrefix = string.Empty;
//DocExpansion设置为none可折叠所有方法 //DocExpansion设置为none可折叠所有方法
options.DocExpansion(DocExpansion.None); options.DocExpansion(DocExpansion.None);
//DefaultModelsExpandDepth设置为 - 1 可不显示models //DefaultModelsExpandDepth设置为 - 1 可不显示models
options.DefaultModelsExpandDepth(-1); options.DefaultModelsExpandDepth(-1);
// 引入静态文件添加登录功能 // 引入静态文件添加登录功能
// 清除静态文件缓存 // 清除静态文件缓存
// options.IndexStream = () => null; // options.IndexStream = () => null;
});
});
}
} }
} }
} }

View File

@ -49,25 +49,30 @@ namespace IRaCIS.Core.API.Middleware
private async Task<bool> IsValidRequest(HttpContext context) private async Task<bool> IsValidRequest(HttpContext context)
{ {
var apiPathList =await _userTypeMenuRepository.Where(x => x.Menu.ApiPath != null && x.Menu.ApiPath != string.Empty) if (!Jurisdiction.IsSearch)
.Select(x => new MenuApi()
{
UserTypeEnum = (int)x.UserType.UserTypeEnum,
ApiPath = x.Menu.ApiPath,
}).ToListAsync();
apiPathList = apiPathList.Select(x => new MenuApi()
{ {
UserTypeEnum = x.UserTypeEnum, var apiPathList = await _userTypeMenuRepository.Where(x => x.Menu.ApiPath != null && x.Menu.ApiPath != string.Empty)
ApiPath = x.ApiPath.ToLower(), .Select(x => new MenuApi()
}).ToList(); {
UserTypeEnum = (int)x.UserType.UserTypeEnum,
ApiPath = x.Menu.ApiPath,
}).ToListAsync();
Jurisdiction.MenuActionList = apiPathList.Select(x => new MenuApi()
{
UserTypeEnum = x.UserTypeEnum,
ApiPath = x.ApiPath.ToLower(),
}).ToList();
Jurisdiction.IsSearch = true;
}
if (_userInfo.UserTypeEnumInt != 0) if (_userInfo.UserTypeEnumInt != 0)
{ {
var url = _userInfo.RequestUrl.ToLower(); ; var url = _userInfo.RequestUrl.ToLower(); ;
if (apiPathList.Any(x => x.ApiPath.Contains(url)) && !apiPathList.Any(x => x.ApiPath.Contains(url) && x.UserTypeEnum == _userInfo.UserTypeEnumInt)) if (Jurisdiction.MenuActionList.Any(x => x.ApiPath.Contains(url)) && !Jurisdiction.MenuActionList.Any(x => x.ApiPath.Contains(url) && x.UserTypeEnum == _userInfo.UserTypeEnumInt))
{ {
return false; return false;
} }

View File

@ -102,11 +102,6 @@ namespace IRaCIS.Application.Contracts
} }
public class MenuApi
{
public int UserTypeEnum { get; set; }
public string ApiPath { get; set; }
}
} }

View File

@ -1,9 +1,25 @@
using IRaCIS.Core.Domain.Models; using IRaCIS.Core.Domain.Models;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json; using Microsoft.Extensions.Configuration.Json;
using System.Collections.Generic;
namespace IRaCIS.Core.Domain.Share namespace IRaCIS.Core.Domain.Share
{ {
public static class Jurisdiction
{
public static List<MenuApi> MenuActionList { get; set; }=new List<MenuApi> ();
public static bool IsSearch { get; set; } = false;
}
public class MenuApi
{
public int UserTypeEnum { get; set; }
public string ApiPath { get; set; }
}
/// <summary> /// <summary>
/// 多环境 配置环境实体 /// 多环境 配置环境实体
/// </summary> /// </summary>
@ -42,6 +58,7 @@ namespace IRaCIS.Core.Domain.Share
public static string UserCodePrefix { get; set; } public static string UserCodePrefix { get; set; }
public static string QCChallengeCodePrefix { get; set; } public static string QCChallengeCodePrefix { get; set; }
public static string DicomStudyCodePrefix { get; set; } public static string DicomStudyCodePrefix { get; set; }