Przeglądaj źródła

限制登录、api访问时间段有效设置-2025-01-10 15:30:00

LEIYI 3 miesięcy temu
rodzic
commit
df14367e98

+ 15 - 0
OASystem/OASystem.Api/Controllers/AuthController.cs

@@ -88,8 +88,23 @@ namespace OASystem.API.Controllers
             #region 校验用户信息 
             var userData = _loginRep.Login(dto).Result;
             if (userData.Code != 0) return Ok(JsonView(false, userData.Msg));
+            #endregion
+
+            #region 限制销售部门 除gyy外可登录
+
+            var userInfo = userData.Data as UserLoginInfoView;
+            if (userInfo == null) return Ok(JsonView(false, userData.Msg));
+
+            if (!userInfo.CnName.Contains("张海麟"))
+            {
+                if (!userInfo.DepName.Contains("市场部")) return Ok(JsonView(false, "无登录权限!"));
+
+                var noLoginAuth = new List<string>() { "高媛媛", "符志超", "杨千莹", "喻思霖", "张倩" }; //成都销售不可登录
+                if (noLoginAuth.Contains(userInfo.CnName)) return Ok(JsonView(false, "无登录权限!"));
+            }
 
             #endregion
+
             Result authData = null;
             string uName = string.Empty,
                    role = string.Empty,

+ 59 - 0
OASystem/OASystem.Api/Middlewares/TimeRestrictionMiddleware.cs

@@ -0,0 +1,59 @@
+using Microsoft.AspNetCore.Http;
+using NPOI.SS.Formula.Functions;
+using System.Text.Encodings.Web;
+using System.Text.Json;
+using System.Text.Unicode;
+
+namespace OASystem.API.Middlewares
+{
+    /// <summary>
+    /// 设置时间端访问All接口
+    /// </summary>
+    public class TimeRestrictionMiddleware
+    {
+        private readonly RequestDelegate _next; 
+        private readonly DateTime _startDateTime;
+        private readonly DateTime _endDateTime;
+
+        public TimeRestrictionMiddleware(RequestDelegate next, DateTime startDateTime, DateTime endDateTime)
+        {
+            _next = next;
+            _startDateTime = startDateTime;
+            _endDateTime = endDateTime;
+        }
+
+        public async Task InvokeAsync(HttpContext context)
+        {
+            var currentDateTime = DateTime.Now;
+
+            if (currentDateTime >= _startDateTime && currentDateTime <= _endDateTime)
+            {
+                await _next(context);
+            }
+            else
+            {
+                if (context.Request.Method == "OPTIONS")
+                {
+                    context.Response.Headers.Add("Access-Control-Allow-Origin", "http://example.com");
+                    context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
+                    context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
+                    
+                }
+
+                context.Response.ContentType = "application/json";
+                context.Response.StatusCode = 201;
+                var response = context.Response;
+                var errorResponse = new 
+                {
+                    code = 201,
+                    msg = "NO ACCESS!",
+                    data = "",
+                    count = 0
+                };
+
+                await context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(errorResponse));
+
+            }
+        }
+    }
+}

+ 10 - 1
OASystem/OASystem.Api/Program.cs

@@ -374,9 +374,11 @@ builder.Services.TryAddSingleton(typeof(CommonService));
 var app = builder.Build();
 
 
-
 AutofacIocManager.Instance.Container = app.UseHostFiltering().ApplicationServices.GetAutofacRoot();//AutofacIocManager
 
+
+
+
 // Configure the HTTP request pipeline.
 if (!app.Environment.IsDevelopment())
 {
@@ -385,6 +387,7 @@ if (!app.Environment.IsDevelopment())
 
 app.UseStaticFiles();
 
+
 //app.UseMiddleware<FixedPromptMiddleware>();
 app.UseMiddleware<ExceptionHandlingMiddleware>();
 
@@ -392,6 +395,12 @@ app.UseRouting();
 
 app.UseCors("Cors");  //Cors
 
+// 定义允许的访问时间段
+var startTime = DateTime.Parse(_config["ApiAccessTime:StartTime"]);
+var endTime = DateTime.Parse(_config["ApiAccessTime:EndTime"]);
+app.UseMiddleware<TimeRestrictionMiddleware>(startTime, endTime);
+
+
 app.UseAuthentication(); // 认证
 app.UseAuthorization();  // 授权
 

+ 4 - 0
OASystem/OASystem.Api/appsettings.json

@@ -3,6 +3,10 @@
     "OA2023DB": "server=132.232.92.186;uid=sa;pwd=Yjx@158291;database=OA2023DB;MultipleActiveResultSets=True;",
     "OA2014DB": "server=132.232.92.186;uid=sa;pwd=Yjx@158291;database=OA2014;MultipleActiveResultSets=True;"
   },
+  "ApiAccessTime": {
+    "StartTime": "2025-01-09 15:00:00",
+    "EndTime": "2025-01-10 15:30:00"
+  },
   "JwtSecurityKey": "48d3f4fe770940a1068052f581536b81", //jwt密钥
   "UseSwagger": "true", //启用Swagger
   "GroupsConfig": {