示例#1
0
        public async Task <IResultModel> GetTree()
        {
            //先取缓存
            if (_cacheHandler.TryGetValue(CacheKeys.PermissionTree, out TreeResultModel <int, PermissionTreeResultModel> root))
            {
                return(ResultModel.Success(root));
            }

            var id = 0;

            root = new TreeResultModel <int, PermissionTreeResultModel>
            {
                Id    = id,
                Label = _systemConfig.Base.Title,
                Item  = new PermissionTreeResultModel()
            };
            root.Path.Add(root.Label);

            var modules     = _moduleRepository.GetAllAsync();
            var permissions = await _repository.GetAllAsync();

            //模块
            foreach (var module in await modules)
            {
                var moduleNode = new TreeResultModel <int, PermissionTreeResultModel>
                {
                    Id    = ++id,
                    Label = module.Name,
                    Item  = new PermissionTreeResultModel
                    {
                        Label = module.Name,
                        Code  = module.Code
                    }
                };

                moduleNode.Path.AddRange(root.Path);
                moduleNode.Path.Add(module.Name);

                var controllers = permissions.Where(m => m.ModuleCode.EqualsIgnoreCase(module.Code)).DistinctBy(m => m.Controller);
                //控制器
                foreach (var controller in controllers)
                {
                    var controllerName = controller.Name.Split('_')[0];
                    var controllerNode = new TreeResultModel <int, PermissionTreeResultModel>
                    {
                        Id    = ++id,
                        Label = controllerName,
                        Item  = new PermissionTreeResultModel
                        {
                            Label = controllerName,
                            Code  = controller.Controller
                        }
                    };

                    controllerNode.Path.AddRange(moduleNode.Path);
                    controllerNode.Path.Add(controllerName);

                    var permissionList = permissions.Where(m => m.ModuleCode.EqualsIgnoreCase(module.Code) && m.Controller.EqualsIgnoreCase(controller.Controller));
                    //权限
                    foreach (var permission in permissionList)
                    {
                        var permissionName = permission.Name.Contains("_") ? permission.Name.Split('_')[1] : permission.Name;
                        var permissionNode = new TreeResultModel <int, PermissionTreeResultModel>
                        {
                            Id    = ++id,
                            Label = permissionName,
                            Item  = new PermissionTreeResultModel
                            {
                                Label        = permissionName,
                                Code         = permission.Code,
                                IsPermission = true
                            }
                        };

                        permissionNode.Path.AddRange(controllerNode.Path);
                        permissionNode.Path.Add(permissionName);

                        controllerNode.Children.Add(permissionNode);
                    }

                    moduleNode.Children.Add(controllerNode);
                }
                root.Children.Add(moduleNode);
            }

            await _cacheHandler.SetAsync(CacheKeys.PermissionTree, root);

            return(ResultModel.Success(root));
        }
示例#2
0
        public async Task <IResultModel> Query()
        {
            var list = await _repository.GetAllAsync();

            return(ResultModel.Success(list));
        }