Ejemplo n.º 1
0
        public void GetUserMenu()
        {
            string          userId         = "cf9d52cc-0500-4829-9611-fd0056961469";
            ISysUserService sysUserService = new SysUserService();

            //通过用户id获取权限菜单数据
            IEnumerable <SysPrivilege> sysUserPrivileges = sysUserService.GetPrivilege(userId);

            sysUserPrivileges = sysUserPrivileges.Where(x => x.PrivilegeAccess == PrivilegeAccess.Menu).ToList();

            //通过用户id获取角色,通过角色获取权限菜单数据

            IEnumerable <SysRole> sysRoles       = sysUserService.GetRoles(userId);
            ISysRoleService       sysRoleService = new SysRoleService();

            SysRole[] sysRoles1 = sysRoles.ToArray();
            //一个用户是否会有多个角色?
            for (int i = 0; i < sysRoles1.Count(); i++)
            {
                string roleId = sysRoles1[0].SysId;
                IEnumerable <SysPrivilege> sysRolePrivileges = sysRoleService.GetPrivilege(roleId);

                //排除同用户权限相同菜单数据
                SysPrivilege[] sysUserPrivileges1 = sysUserPrivileges.ToArray();
                for (int j = 0; j < sysUserPrivileges1.Length; j++)
                {
                    sysRolePrivileges =
                        sysRolePrivileges.Where(
                            x =>
                            x.PrivilegeAccessKey != sysUserPrivileges1[j].PrivilegeAccessKey &&
                            x.PrivilegeAccess == PrivilegeAccess.Menu).ToList();
                }

                sysUserPrivileges = sysUserPrivileges.Union(sysRolePrivileges);
            }
            //sysUserPrivileges结果为最终菜单权限
            List <SysMenu>  sysMenus       = new List <SysMenu>();
            ISysMenuService sysMenuService = new SysMenuService();

            foreach (SysPrivilege sysUserPrivilege in sysUserPrivileges)
            {
                string  menuId  = sysUserPrivilege.PrivilegeAccessKey;
                SysMenu sysMenu = sysMenuService.GetSysMenuById(menuId).ToArray()[0];
                sysMenus.Add(sysMenu);
            }
            sysMenus = sysMenus.Where(x => x.IsVisible == 1).OrderBy(x => x.MenuOrder).ToList();
        }