diff --git a/samples/web/Liuliu.Demo.Web/Controllers/AuthController.cs b/samples/web/Liuliu.Demo.Web/Controllers/AuthController.cs index 60419e8d..ca86f1a8 100644 --- a/samples/web/Liuliu.Demo.Web/Controllers/AuthController.cs +++ b/samples/web/Liuliu.Demo.Web/Controllers/AuthController.cs @@ -54,81 +54,45 @@ public bool CheckUrlAuth(string url) /// /// 获取授权信息 - /// + /// 步骤: + /// 1.获取初始化时缓存的所有ModuleInfo信息,此信息已经包含最新版本的Module->Function[]信息 + /// 2.判断当前用户对于Function的权限 + /// 3.提取有效的模块代码节点 + /// /// 权限节点 [HttpGet] [ModuleInfo] [Description("获取授权信息")] - public List GetAuthInfo() + public string[] GetAuthInfo() { - Module[] modules = _functionAuthManager.Modules.ToArray(); - List list = new List(); - foreach (Module module in modules) + IServiceProvider provider = HttpContext.RequestServices; + IModuleHandler moduleHandler = provider.GetRequiredService(); + IFunctionAuthorization functionAuthorization = provider.GetService(); + ModuleInfo[] moduleInfos = moduleHandler.ModuleInfos; + + //先查找出所有有权限的模块 + List authModules = new List(); + foreach (ModuleInfo moduleInfo in moduleInfos) { - if (CheckFuncAuth(module, out bool empty)) - { - list.Add(new AuthItem { Code = GetModuleTreeCode(module, modules), HasFunc = !empty }); - } - } - List codes = new List(); - foreach (AuthItem item in list) - { - if (item.HasFunc) - { - codes.Add(item.Code); - } - else if (list.Any(m => m.Code.Length > item.Code.Length && m.Code.Contains(item.Code) && m.HasFunc)) + bool hasAuth = moduleInfo.DependOnFunctions.All(m => functionAuthorization.Authorize(m, User).IsOk); + if (moduleInfo.DependOnFunctions.Length == 0 || hasAuth) { - codes.Add(item.Code); + authModules.Add(moduleInfo); } } - return codes; - } - /// - /// 验证是否拥有指定模块的权限 - /// - /// 要验证的模块 - /// 返回模块是否为空模块,即是否分配有功能 - /// - private bool CheckFuncAuth(Module module, out bool empty) - { - IServiceProvider services = HttpContext.RequestServices; - IFunctionAuthorization authorization = services.GetService(); - - Function[] functions = _functionAuthManager.ModuleFunctions.Where(m => m.ModuleId == module.Id).Select(m => m.Function).ToArray(); - empty = functions.Length == 0; - if (empty) - { - return true; - } - - foreach (Function function in functions) + List codes = new List(); + foreach (ModuleInfo moduleInfo in authModules) { - if (!authorization.Authorize(function, User).IsOk) + string fullCode = moduleInfo.FullCode; + //模块下边有功能,或者拥有子模块 + if (moduleInfo.DependOnFunctions.Length > 0 + || authModules.Any(m => m.FullCode.Length > fullCode.Length && m.FullCode.Contains(fullCode) && m.DependOnFunctions.Length > 0)) { - return false; + codes.AddIfNotExist(fullCode); } } - return true; - } - - /// - /// 获取模块的树形路径代码串 - /// - private static string GetModuleTreeCode(Module module, Module[] source) - { - var pathIds = module.TreePathIds; - string[] names = pathIds.Select(m => source.First(n => n.Id == m)).Select(m => m.Code).ToArray(); - return names.ExpandAndToString("."); - } - - - private class AuthItem - { - public string Code { get; set; } - - public bool HasFunc { get; set; } + return codes.ToArray(); } } } diff --git a/src/OSharp.Authorization.Functions/ModuleHandlerBase.cs b/src/OSharp.Authorization.Functions/ModuleHandlerBase.cs index 5f435dd8..4a439455 100644 --- a/src/OSharp.Authorization.Functions/ModuleHandlerBase.cs +++ b/src/OSharp.Authorization.Functions/ModuleHandlerBase.cs @@ -45,6 +45,7 @@ protected ModuleHandlerBase(IServiceProvider serviceProvider) _serviceProvider = serviceProvider; _moduleInfoPicker = serviceProvider.GetService(); Logger = serviceProvider.GetLogger(GetType()); + ModuleInfos = new ModuleInfo[0]; } /// @@ -52,6 +53,11 @@ protected ModuleHandlerBase(IServiceProvider serviceProvider) /// protected ILogger Logger { get; } + /// + /// 获取 所有模块信息 + /// + public ModuleInfo[] ModuleInfos { get; private set; } + /// /// 从程序集中获取模块信息 /// @@ -67,8 +73,9 @@ public void Initialize() { SyncToDatabase(provider, moduleInfos); }); + ModuleInfos = moduleInfos.OrderBy(m => $"{m.Position}.{m.Code}").ToArray(); } - + /// /// 重写以实现将提取到的模块信息同步到数据库中 /// diff --git a/src/OSharp.Hosting.Apis/Controllers/AuthController.cs b/src/OSharp.Hosting.Apis/Controllers/AuthController.cs index 924b11a4..01b235ba 100644 --- a/src/OSharp.Hosting.Apis/Controllers/AuthController.cs +++ b/src/OSharp.Hosting.Apis/Controllers/AuthController.cs @@ -10,13 +10,16 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Linq; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; using OSharp.AspNetCore.Mvc; using OSharp.AspNetCore.Mvc.Filters; +using OSharp.Authorization; using OSharp.Authorization.Modules; -using OSharp.Hosting.Authorization; +using OSharp.Collections; namespace OSharp.Hosting.Apis.Controllers @@ -25,13 +28,6 @@ namespace OSharp.Hosting.Apis.Controllers [ModuleInfo(Order = 2)] public class AuthController : SiteApiControllerBase { - private readonly FunctionAuthManager _functionAuthManager; - - public AuthController(FunctionAuthManager functionAuthManager) - { - _functionAuthManager = functionAuthManager; - } - /// /// 检查URL授权 /// @@ -48,14 +44,45 @@ public bool CheckUrlAuth(string url) /// /// 获取授权信息 - /// + /// 步骤: + /// 1.获取初始化时缓存的所有ModuleInfo信息,此信息已经包含最新版本的Module->Function[]信息 + /// 2.判断当前用户对于Function的权限 + /// 3.提取有效的模块代码节点 + /// /// 权限节点 [HttpGet] [ModuleInfo] [Description("获取授权信息")] - public List GetAuthInfo() + public string[] GetAuthInfo() { - throw new NotImplementedException(); + IServiceProvider provider = HttpContext.RequestServices; + IModuleHandler moduleHandler = provider.GetRequiredService(); + IFunctionAuthorization functionAuthorization = provider.GetService(); + ModuleInfo[] moduleInfos = moduleHandler.ModuleInfos; + + //先查找出所有有权限的模块 + List authModules = new List(); + foreach (ModuleInfo moduleInfo in moduleInfos) + { + bool hasAuth = moduleInfo.DependOnFunctions.All(m => functionAuthorization.Authorize(m, User).IsOk); + if (moduleInfo.DependOnFunctions.Length == 0 || hasAuth) + { + authModules.Add(moduleInfo); + } + } + + List codes = new List(); + foreach (ModuleInfo moduleInfo in authModules) + { + string fullCode = moduleInfo.FullCode; + //模块下边有功能,或者拥有子模块 + if (moduleInfo.DependOnFunctions.Length > 0 + || authModules.Any(m => m.FullCode.Length > fullCode.Length && m.FullCode.Contains(fullCode) && m.DependOnFunctions.Length > 0)) + { + codes.AddIfNotExist(fullCode); + } + } + return codes.ToArray(); } } } diff --git a/src/OSharp/Authorization/Modules/IModuleHandler.cs b/src/OSharp/Authorization/Modules/IModuleHandler.cs index 45134abf..bd0ab6d0 100644 --- a/src/OSharp/Authorization/Modules/IModuleHandler.cs +++ b/src/OSharp/Authorization/Modules/IModuleHandler.cs @@ -14,6 +14,11 @@ namespace OSharp.Authorization.Modules /// public interface IModuleHandler { + /// + /// 获取 所有模块信息 + /// + ModuleInfo[] ModuleInfos { get; } + /// /// 从程序集中获取模块信息 /// diff --git a/src/OSharp/Authorization/Modules/ModuleInfo.cs b/src/OSharp/Authorization/Modules/ModuleInfo.cs index 673ad1fd..a14a5406 100644 --- a/src/OSharp/Authorization/Modules/ModuleInfo.cs +++ b/src/OSharp/Authorization/Modules/ModuleInfo.cs @@ -49,6 +49,11 @@ public class ModuleInfo : IEntityHash /// public string PositionName { get; set; } + /// + /// 获取 位置全名 + /// + public string FullCode => $"{Position}.{Code}"; + /// /// 获取或设置 依赖功能 ///