Ejemplo n.º 1
0
        /// <summary>
        /// 根据模块初始化权限
        /// </summary>
        /// <param name="moduleManager">模块管理器</param>
        /// <param name="permissionManager">claim管理器</param>
        static void InitForModules(IModuleManager moduleManager, IPermissionManager permissionManager)
        {
            // 反射使用的几个类型
            var claimsAuthorizeAttrType = typeof(PermissionAuthorizeAttribute);
            var controllerType          = typeof(Microsoft.AspNetCore.Mvc.ControllerBase);
            var applicationServiceName  = nameof(Riven.Application.IApplicationService);

            // Permission 字典集合
            var permissionDict = new Dictionary <PermissionInfo, PermissionAuthorizeScope>();


            // 模块信息
            var moduleDescriptors = (moduleManager as ModuleManager)?.ModuleDescriptors;

            if (moduleDescriptors == null)
            {
                throw new Exception("not found module descriptors");
            }

            // 扫描所有模块中导出的类型
            foreach (var module in moduleDescriptors)
            {
                var types = module.ModuleType.Assembly.GetExportedTypes();
                foreach (var type in types)
                {
                    // 不符合条件的
                    if (
                        type.IsAbstract ||
                        type.IsInterface ||
                        type.IsGenericType ||
                        !type.IsClass ||
                        (type.GetInterface(applicationServiceName) == null && !type.IsSubclassOf(controllerType))
                        )
                    {
                        continue;
                    }


                    // 获取类型中所有的函数并遍历
                    var methodInfos = type.GetMethods();
                    foreach (var methodInfo in methodInfos)
                    {
                        // 获取方法上的 PermissionAuthorizeAttribute 特性
                        var attrs = methodInfo
                                    .GetCustomAttributes(claimsAuthorizeAttrType, false);
                        if (attrs.Length == 0)
                        {
                            continue;
                        }

                        // 将特性中的 Permission 数据加入字典
                        var permissionAuthorizeAttr = attrs[0] as PermissionAuthorizeAttribute;
                        var newItem = default(PermissionInfo);
                        foreach (var permission in permissionAuthorizeAttr.Permissions)
                        {
                            newItem = new PermissionInfo(permission, permissionAuthorizeAttr.Scope);
                            if (permissionDict.ContainsKey(newItem))
                            {
                                continue;
                            }
                            permissionDict[newItem] = permissionAuthorizeAttr.Scope;
                        }
                    }
                }
            }

            // 将数据进行分组
            var groupPermissions = permissionDict.GroupBy(o => o.Value)
                                   .Select(o =>
            {
                return(new
                {
                    Scope = o.Key,
                    Permissions = o.AsEnumerable().Select(p => p.Key)
                });
            })
                                   .ToList();

            // 遍历分组后的数据并生成结构添加到管理器
            var permissionItemDict = new Dictionary <PermissionItem, string>();

            permissionItemDict.Add(new PermissionItem(AppPermissions.RootNode, null, PermissionAuthorizeScope.Common), string.Empty);
            foreach (var groupPermission in groupPermissions)
            {
                foreach (var permission in groupPermission.Permissions)
                {
                    foreach (var permissionItem in PermissionToTree(permission.Name, groupPermission.Scope, AppPermissions.RootNode))
                    {
                        if (permissionItemDict.ContainsKey(permissionItem))
                        {
                            continue;
                        }

                        permissionItemDict.Add(permissionItem, string.Empty);
                    }
                }
                permissionManager.Add(permissionItemDict.Keys.ToArray());
                permissionItemDict.Clear();
            }
        }