コード例 #1
0
        public IEnumerable <TreeDataView> GetRoleAndGroupTree()
        {
            IEnumerable <FapRoleGroup> roleGroups = _rbacService.GetAllRoleGroup();
            IEnumerable <FapRole>      roles      = _rbacService.GetAllRole();
            List <TreeDataView>        treeGroup  = roleGroups.Select(t => new TreeDataView {
                Id = t.Fid.ToString(), Data = new { IsRole = false }, Pid = t.Pid.ToString(), Text = t.RoleGroupName, State = new NodeState {
                    Opened = true
                }, Icon = "icon-folder purple ace-icon fa fa-users"
            }).ToList <TreeDataView>();

            if (!_applicationContext.IsAdministrator)
            {
                var roleRoles = _rbacService.GetRoleRoleList(_applicationContext.CurrentRoleUid).Select(r => r.PRoleUid);
                //自已有权限和自己创建的角色
                roles = roles.Where(r => roleRoles.Contains(r.Fid) || r.CreateBy == _applicationContext.EmpUid);
            }

            //普通用户角色加到tree根级
            FapRole commonRole = roles.FirstOrDefault(r => r.Fid == FapPlatformConstants.CommonUserRoleFid);

            if (commonRole != null)
            {
                treeGroup.Insert(0, new TreeDataView {
                    Id = commonRole.Fid, Data = new { IsRole = true }, Pid = "0", Text = commonRole.RoleName, Icon = "icon-folder orange ace-icon fa fa-users"
                });
            }

            List <TreeDataView> treeRole = roles.Select(r => new TreeDataView {
                Id = r.Fid.ToString(), Data = new { IsRole = true }, Pid = r.RoleGroupUid, Text = r.RoleName, State = new NodeState {
                    Opened = true
                }, Icon = "icon-folder orange ace-icon fa fa-users"
            }).ToList <TreeDataView>();
            List <TreeDataView> tree = new List <TreeDataView>();

            List <TreeDataView> treeRoots = treeGroup.Where(g => g.Pid == "0").ToList();

            foreach (var treeRoot in treeRoots)
            {
                TreeViewHelper.MakeTree(treeRoot.Children, treeGroup, treeRoot.Id);
            }
            tree.AddRange(treeRoots);
            List <TreeDataView> emptyGroup = new List <TreeDataView>();

            foreach (var item in tree)
            {
                var rl = treeRole.Where(r => r.Pid == item.Id);
                if (rl.Any())
                {
                    item.Children.AddRange(rl);
                }
                else
                {
                    if (!item.Id.Equals(FapPlatformConstants.CommonUserRoleFid))
                    {
                        emptyGroup.Add(item);
                    }
                }
            }
            if (emptyGroup.Any())
            {
                //移除没有角色的角色组
                emptyGroup.ForEach((d) => { tree.Remove(d); });
            }
            return(tree);
        }