From a18443c563d2dff035a7dd20b88b633a0a3bbd2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E6=98=8E=E9=94=8B?= Date: Fri, 6 Oct 2023 11:19:38 +0800 Subject: [PATCH] =?UTF-8?q?wip:=20(auth)=20=E4=BC=98=E5=8C=96=E5=8A=9F?= =?UTF-8?q?=E8=83=BD-=E8=A7=92=E8=89=B2=E7=9B=B8=E5=85=B3=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=EF=BC=8C=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=90=8D=E4=BB=A3=E6=9B=BFId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/web/Liuliu.Demo.WebApi/Startup.cs | 2 +- .../appsettings.Development.json | 6 +++--- .../FunctionAuthCacheBase.cs | 8 ++++++-- .../ModuleHandlerBase.cs | 2 +- .../Functions/FunctionHandlerBase.cs | 17 ++++++++++++++++- .../Authorization/Functions/IFunctionHandler.cs | 9 ++++++++- 6 files changed, 35 insertions(+), 9 deletions(-) diff --git a/samples/web/Liuliu.Demo.WebApi/Startup.cs b/samples/web/Liuliu.Demo.WebApi/Startup.cs index 11354354..15349e29 100644 --- a/samples/web/Liuliu.Demo.WebApi/Startup.cs +++ b/samples/web/Liuliu.Demo.WebApi/Startup.cs @@ -42,7 +42,7 @@ public void ConfigureServices(IServiceCollection services) .AddPack() .AddPack() .AddPack() - .AddPack() + //.AddPack() .AddPack() .AddPack(); } diff --git a/samples/web/Liuliu.Demo.WebApi/appsettings.Development.json b/samples/web/Liuliu.Demo.WebApi/appsettings.Development.json index 4c514902..ac509c32 100644 --- a/samples/web/Liuliu.Demo.WebApi/appsettings.Development.json +++ b/samples/web/Liuliu.Demo.WebApi/appsettings.Development.json @@ -1,9 +1,9 @@ { "Logging": { "LogLevel": { - "Default": "Debug", - "OSharp": "Debug", - "Liuliu": "Debug", + "Default": "Information", + "OSharp": "Information", + "Liuliu": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Warning" } diff --git a/src/OSharp.Authorization.Functions/FunctionAuthCacheBase.cs b/src/OSharp.Authorization.Functions/FunctionAuthCacheBase.cs index a6b17e47..8f03c908 100644 --- a/src/OSharp.Authorization.Functions/FunctionAuthCacheBase.cs +++ b/src/OSharp.Authorization.Functions/FunctionAuthCacheBase.cs @@ -7,6 +7,8 @@ // 2018-05-11 0:59 // ----------------------------------------------------------------------- +using System.Diagnostics.CodeAnalysis; + namespace OSharp.Authorization; @@ -99,13 +101,15 @@ public virtual void RemoveUserCaches(params string[] userNames) public string[] GetFunctionRoles(Guid functionId, IServiceProvider scopeProvider = null, bool forceRefresh = false) { string key = GetFunctionRolesKey(functionId); + IFunctionHandler functionHandler = _serviceProvider.GetRequiredService(); + var function = functionHandler.GetFunction(functionId); string[] roleNames; if (!forceRefresh) { roleNames = _cache.Get(key); if (roleNames != null) { - _logger.LogDebug($"从缓存中获取到功能“{functionId}”的“Function-Roles[]”缓存,角色数:{roleNames.Length}"); + _logger.LogDebug($"从缓存中获取到功能“{function.Name}”的“Function-Roles[]”缓存,角色数:{roleNames.Length}"); return roleNames; } } @@ -139,7 +143,7 @@ public string[] GetFunctionRoles(Guid functionId, IServiceProvider scopeProvider // 有效期为 7 ± 1 天 int seconds = 7 * 24 * 3600 + _random.Next(-24 * 3600, 24 * 3600); _cache.Set(key, roleNames, seconds); - _logger.LogDebug($"添加功能“{functionId}”的“Function-Roles[]”缓存,角色数:{roleNames.Length}"); + _logger.LogDebug($"添加功能“{function.Name}”的“Function-Roles[]”缓存,角色数:{roleNames.Length}"); serviceScope?.Dispose(); return roleNames; diff --git a/src/OSharp.Authorization.Functions/ModuleHandlerBase.cs b/src/OSharp.Authorization.Functions/ModuleHandlerBase.cs index 6c0bac77..458d843b 100644 --- a/src/OSharp.Authorization.Functions/ModuleHandlerBase.cs +++ b/src/OSharp.Authorization.Functions/ModuleHandlerBase.cs @@ -30,7 +30,7 @@ protected ModuleHandlerBase(IServiceProvider serviceProvider) _serviceProvider = serviceProvider; _moduleInfoPicker = serviceProvider.GetService(); Logger = serviceProvider.GetLogger(GetType()); - ModuleInfos = new ModuleInfo[0]; + ModuleInfos = Array.Empty(); } /// diff --git a/src/OSharp/Authorization/Functions/FunctionHandlerBase.cs b/src/OSharp/Authorization/Functions/FunctionHandlerBase.cs index 380f7d54..ce799df7 100644 --- a/src/OSharp/Authorization/Functions/FunctionHandlerBase.cs +++ b/src/OSharp/Authorization/Functions/FunctionHandlerBase.cs @@ -62,6 +62,21 @@ public void Initialize() /// public abstract MethodInfo[] GetMethodInfos(Type functionType); + /// + /// 查找指定编号的功能信息 + /// + /// 功能编号 + /// + public IFunction GetFunction(Guid functionId) + { + if (_functions.Count == 0) + { + RefreshCache(); + } + + return _functions.FirstOrDefault(m => m.Id.Equals(functionId)); + } + /// /// 查找指定条件的功能信息 /// @@ -362,4 +377,4 @@ protected virtual TFunction[] GetFromDatabase(IServiceProvider scopedProvider) TFunction[] functions = repository.QueryAsNoTracking(null, false).ToArray(); return functions; } -} \ No newline at end of file +} diff --git a/src/OSharp/Authorization/Functions/IFunctionHandler.cs b/src/OSharp/Authorization/Functions/IFunctionHandler.cs index e6e19fee..a5e9c4da 100644 --- a/src/OSharp/Authorization/Functions/IFunctionHandler.cs +++ b/src/OSharp/Authorization/Functions/IFunctionHandler.cs @@ -32,6 +32,13 @@ public interface IFunctionHandler /// MethodInfo[] GetMethodInfos(Type functionType); + /// + /// 查找指定编号的功能信息 + /// + /// 功能编号 + /// + IFunction GetFunction(Guid functionId); + /// /// 查找指定条件的功能信息 /// @@ -50,4 +57,4 @@ public interface IFunctionHandler /// 清空功能信息缓存 /// void ClearCache(); -} \ No newline at end of file +}