Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetAuthInfo 方法获取时间过长, 希望能够优化一下 #227

Closed
gmf520 opened this issue Apr 2, 2021 · 1 comment
Closed

GetAuthInfo 方法获取时间过长, 希望能够优化一下 #227

gmf520 opened this issue Apr 2, 2021 · 1 comment
Labels
Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Milestone

Comments

@gmf520
Copy link
Member

gmf520 commented Apr 2, 2021

GetAuthInfo 方法获取时间过长, 希望能够优化一下

Originally posted by @tegohang in #168 (comment)

@gmf520
Copy link
Member Author

gmf520 commented Apr 2, 2021

原先慢的原因,是依赖于数据库查询所有模块-模块的所有功能,还涉及模块树遍历,确实很垃圾

优化思路:

在系统初始化的时候,已经根据最新代码通过IModuleInfoPicker.Pickup()提取了ModuleInfo[]信息,此信息包含了Module树,Module->Function[]的信息,可以缓存复用,解决上面慢的原因
实现如下:

        public string[] GetAuthInfo()
        {
            IServiceProvider provider = HttpContext.RequestServices;
            IModuleHandler moduleHandler = provider.GetRequiredService<IModuleHandler>();
            IFunctionAuthorization functionAuthorization = provider.GetService<IFunctionAuthorization>();
            ModuleInfo[] moduleInfos = moduleHandler.ModuleInfos;
            
            //先查找出所有有权限的模块
            List<ModuleInfo> authModules = new List<ModuleInfo>();
            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<string> codes = new List<string>();
            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();
        }

@gmf520 gmf520 added Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工 labels Apr 2, 2021
@gmf520 gmf520 added this to the v5.0.4 milestone Apr 2, 2021
@gmf520 gmf520 closed this as completed Apr 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature 🔨 新功能,新特性 Finished ✔️ 实现并完工
Projects
None yet
Development

No branches or pull requests

1 participant