@@ -54,81 +54,45 @@ public bool CheckUrlAuth(string url)
54
54
55
55
/// <summary>
56
56
/// 获取授权信息
57
- /// </summary>
57
+ /// 步骤:
58
+ /// 1.获取初始化时缓存的所有ModuleInfo信息,此信息已经包含最新版本的Module->Function[]信息
59
+ /// 2.判断当前用户对于Function的权限
60
+ /// 3.提取有效的模块代码节点
61
+ /// </summary>
58
62
/// <returns>权限节点</returns>
59
63
[ HttpGet ]
60
64
[ ModuleInfo ]
61
65
[ Description ( "获取授权信息" ) ]
62
- public List < string > GetAuthInfo ( )
66
+ public string [ ] GetAuthInfo ( )
63
67
{
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 )
67
76
{
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 )
81
79
{
82
- codes . Add ( item . Code ) ;
80
+ authModules . Add ( moduleInfo ) ;
83
81
}
84
82
}
85
- return codes ;
86
- }
87
83
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 )
107
86
{
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 ) )
109
91
{
110
- return false ;
92
+ codes . AddIfNotExist ( fullCode ) ;
111
93
}
112
94
}
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 ( ) ;
132
96
}
133
97
}
134
98
}
0 commit comments