Skip to content

Commit ba33738

Browse files
committed
feat(auth): 优化AuthController.GetAuthInfo方法的实现 #227
1 parent 9ff4b41 commit ba33738

File tree

5 files changed

+81
-73
lines changed

5 files changed

+81
-73
lines changed

samples/web/Liuliu.Demo.Web/Controllers/AuthController.cs

+25-61
Original file line numberDiff line numberDiff line change
@@ -54,81 +54,45 @@ public bool CheckUrlAuth(string url)
5454

5555
/// <summary>
5656
/// 获取授权信息
57-
/// </summary>
57+
/// 步骤:
58+
/// 1.获取初始化时缓存的所有ModuleInfo信息,此信息已经包含最新版本的Module->Function[]信息
59+
/// 2.判断当前用户对于Function的权限
60+
/// 3.提取有效的模块代码节点
61+
/// </summary>
5862
/// <returns>权限节点</returns>
5963
[HttpGet]
6064
[ModuleInfo]
6165
[Description("获取授权信息")]
62-
public List<string> GetAuthInfo()
66+
public string[] GetAuthInfo()
6367
{
64-
Module[] modules = _functionAuthManager.Modules.ToArray();
65-
List<AuthItem> list = new List<AuthItem>();
66-
foreach (Module module in modules)
68+
IServiceProvider provider = HttpContext.RequestServices;
69+
IModuleHandler moduleHandler = provider.GetRequiredService<IModuleHandler>();
70+
IFunctionAuthorization functionAuthorization = provider.GetService<IFunctionAuthorization>();
71+
ModuleInfo[] moduleInfos = moduleHandler.ModuleInfos;
72+
73+
//先查找出所有有权限的模块
74+
List<ModuleInfo> authModules = new List<ModuleInfo>();
75+
foreach (ModuleInfo moduleInfo in moduleInfos)
6776
{
68-
if (CheckFuncAuth(module, out bool empty))
69-
{
70-
list.Add(new AuthItem { Code = GetModuleTreeCode(module, modules), HasFunc = !empty });
71-
}
72-
}
73-
List<string> codes = new List<string>();
74-
foreach (AuthItem item in list)
75-
{
76-
if (item.HasFunc)
77-
{
78-
codes.Add(item.Code);
79-
}
80-
else if (list.Any(m => m.Code.Length > item.Code.Length && m.Code.Contains(item.Code) && m.HasFunc))
77+
bool hasAuth = moduleInfo.DependOnFunctions.All(m => functionAuthorization.Authorize(m, User).IsOk);
78+
if (moduleInfo.DependOnFunctions.Length == 0 || hasAuth)
8179
{
82-
codes.Add(item.Code);
80+
authModules.Add(moduleInfo);
8381
}
8482
}
85-
return codes;
86-
}
8783

88-
/// <summary>
89-
/// 验证是否拥有指定模块的权限
90-
/// </summary>
91-
/// <param name="module">要验证的模块</param>
92-
/// <param name="empty">返回模块是否为空模块,即是否分配有功能</param>
93-
/// <returns></returns>
94-
private bool CheckFuncAuth(Module module, out bool empty)
95-
{
96-
IServiceProvider services = HttpContext.RequestServices;
97-
IFunctionAuthorization authorization = services.GetService<IFunctionAuthorization>();
98-
99-
Function[] functions = _functionAuthManager.ModuleFunctions.Where(m => m.ModuleId == module.Id).Select(m => m.Function).ToArray();
100-
empty = functions.Length == 0;
101-
if (empty)
102-
{
103-
return true;
104-
}
105-
106-
foreach (Function function in functions)
84+
List<string> codes = new List<string>();
85+
foreach (ModuleInfo moduleInfo in authModules)
10786
{
108-
if (!authorization.Authorize(function, User).IsOk)
87+
string fullCode = moduleInfo.FullCode;
88+
//模块下边有功能,或者拥有子模块
89+
if (moduleInfo.DependOnFunctions.Length > 0
90+
|| authModules.Any(m => m.FullCode.Length > fullCode.Length && m.FullCode.Contains(fullCode) && m.DependOnFunctions.Length > 0))
10991
{
110-
return false;
92+
codes.AddIfNotExist(fullCode);
11193
}
11294
}
113-
return true;
114-
}
115-
116-
/// <summary>
117-
/// 获取模块的树形路径代码串
118-
/// </summary>
119-
private static string GetModuleTreeCode(Module module, Module[] source)
120-
{
121-
var pathIds = module.TreePathIds;
122-
string[] names = pathIds.Select(m => source.First(n => n.Id == m)).Select(m => m.Code).ToArray();
123-
return names.ExpandAndToString(".");
124-
}
125-
126-
127-
private class AuthItem
128-
{
129-
public string Code { get; set; }
130-
131-
public bool HasFunc { get; set; }
95+
return codes.ToArray();
13296
}
13397
}
13498
}

src/OSharp.Authorization.Functions/ModuleHandlerBase.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,19 @@ protected ModuleHandlerBase(IServiceProvider serviceProvider)
4545
_serviceProvider = serviceProvider;
4646
_moduleInfoPicker = serviceProvider.GetService<IModuleInfoPicker>();
4747
Logger = serviceProvider.GetLogger(GetType());
48+
ModuleInfos = new ModuleInfo[0];
4849
}
4950

5051
/// <summary>
5152
/// 获取 日志记录对象
5253
/// </summary>
5354
protected ILogger Logger { get; }
5455

56+
/// <summary>
57+
/// 获取 所有模块信息
58+
/// </summary>
59+
public ModuleInfo[] ModuleInfos { get; private set; }
60+
5561
/// <summary>
5662
/// 从程序集中获取模块信息
5763
/// </summary>
@@ -67,8 +73,9 @@ public void Initialize()
6773
{
6874
SyncToDatabase(provider, moduleInfos);
6975
});
76+
ModuleInfos = moduleInfos.OrderBy(m => $"{m.Position}.{m.Code}").ToArray();
7077
}
71-
78+
7279
/// <summary>
7380
/// 重写以实现将提取到的模块信息同步到数据库中
7481
/// </summary>

src/OSharp.Hosting.Apis/Controllers/AuthController.cs

+38-11
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
using System;
1111
using System.Collections.Generic;
1212
using System.ComponentModel;
13+
using System.Linq;
1314

1415
using Microsoft.AspNetCore.Mvc;
16+
using Microsoft.Extensions.DependencyInjection;
1517

1618
using OSharp.AspNetCore.Mvc;
1719
using OSharp.AspNetCore.Mvc.Filters;
20+
using OSharp.Authorization;
1821
using OSharp.Authorization.Modules;
19-
using OSharp.Hosting.Authorization;
22+
using OSharp.Collections;
2023

2124

2225
namespace OSharp.Hosting.Apis.Controllers
@@ -25,13 +28,6 @@ namespace OSharp.Hosting.Apis.Controllers
2528
[ModuleInfo(Order = 2)]
2629
public class AuthController : SiteApiControllerBase
2730
{
28-
private readonly FunctionAuthManager _functionAuthManager;
29-
30-
public AuthController(FunctionAuthManager functionAuthManager)
31-
{
32-
_functionAuthManager = functionAuthManager;
33-
}
34-
3531
/// <summary>
3632
/// 检查URL授权
3733
/// </summary>
@@ -48,14 +44,45 @@ public bool CheckUrlAuth(string url)
4844

4945
/// <summary>
5046
/// 获取授权信息
51-
/// </summary>
47+
/// 步骤:
48+
/// 1.获取初始化时缓存的所有ModuleInfo信息,此信息已经包含最新版本的Module->Function[]信息
49+
/// 2.判断当前用户对于Function的权限
50+
/// 3.提取有效的模块代码节点
51+
/// </summary>
5252
/// <returns>权限节点</returns>
5353
[HttpGet]
5454
[ModuleInfo]
5555
[Description("获取授权信息")]
56-
public List<string> GetAuthInfo()
56+
public string[] GetAuthInfo()
5757
{
58-
throw new NotImplementedException();
58+
IServiceProvider provider = HttpContext.RequestServices;
59+
IModuleHandler moduleHandler = provider.GetRequiredService<IModuleHandler>();
60+
IFunctionAuthorization functionAuthorization = provider.GetService<IFunctionAuthorization>();
61+
ModuleInfo[] moduleInfos = moduleHandler.ModuleInfos;
62+
63+
//先查找出所有有权限的模块
64+
List<ModuleInfo> authModules = new List<ModuleInfo>();
65+
foreach (ModuleInfo moduleInfo in moduleInfos)
66+
{
67+
bool hasAuth = moduleInfo.DependOnFunctions.All(m => functionAuthorization.Authorize(m, User).IsOk);
68+
if (moduleInfo.DependOnFunctions.Length == 0 || hasAuth)
69+
{
70+
authModules.Add(moduleInfo);
71+
}
72+
}
73+
74+
List<string> codes = new List<string>();
75+
foreach (ModuleInfo moduleInfo in authModules)
76+
{
77+
string fullCode = moduleInfo.FullCode;
78+
//模块下边有功能,或者拥有子模块
79+
if (moduleInfo.DependOnFunctions.Length > 0
80+
|| authModules.Any(m => m.FullCode.Length > fullCode.Length && m.FullCode.Contains(fullCode) && m.DependOnFunctions.Length > 0))
81+
{
82+
codes.AddIfNotExist(fullCode);
83+
}
84+
}
85+
return codes.ToArray();
5986
}
6087
}
6188
}

src/OSharp/Authorization/Modules/IModuleHandler.cs

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ namespace OSharp.Authorization.Modules
1414
/// </summary>
1515
public interface IModuleHandler
1616
{
17+
/// <summary>
18+
/// 获取 所有模块信息
19+
/// </summary>
20+
ModuleInfo[] ModuleInfos { get; }
21+
1722
/// <summary>
1823
/// 从程序集中获取模块信息
1924
/// </summary>

src/OSharp/Authorization/Modules/ModuleInfo.cs

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public class ModuleInfo : IEntityHash
4949
/// </summary>
5050
public string PositionName { get; set; }
5151

52+
/// <summary>
53+
/// 获取 位置全名
54+
/// </summary>
55+
public string FullCode => $"{Position}.{Code}";
56+
5257
/// <summary>
5358
/// 获取或设置 依赖功能
5459
/// </summary>

0 commit comments

Comments
 (0)