Example #1
0
        /// <summary>
        ///根据用户主键获取用户角色
        /// </summary>
        /// <param name="userGuid">用户主键</param>
        /// <returns>用户集合</returns>
        public static List <List <Role> > GetUserRoleTree(string userGuid)
        {
            API api = new API();
            List <List <Role> > roleTree = new List <List <Role> >();
            List <Role>         roleList = null;

            using (SysDbContext context = api.CreateDbContext())
            {
                var role = from r in context.Roles
                           where
                           (
                    from ru in context.RoleUsers where ru.UserGuid == userGuid select ru.RoleGuid
                           ).Contains(r.Guid)
                           select r;

                roleList = role.ToList();

                var allRole = context.Roles.ToList();


                List <Role> roleTemp = null;
                for (int i = 0; i < roleList.Count; i++)
                {
                    var topguid = roleList[i].TopGuid;
                    var rolet   = new List <Role>();
                    rolet.Add(roleList[i]);
                    while (true)
                    {
                        roleTemp = allRole.Where(r => r.Guid == topguid).ToList();
                        if (roleTemp.Count > 0)
                        {
                            rolet.Add(roleTemp[0]);
                            topguid = roleTemp[0].TopGuid;
                        }
                        else
                        {
                            break;
                        }
                    }
                    rolet.Reverse();
                    roleTree.Add(rolet);
                }

                context.Dispose();
            }
            return(roleTree);
        }
Example #2
0
        /// <summary>
        ///根据用户主键获取用户角色
        /// </summary>
        /// <param name="userGuid">用户主键</param>
        /// <returns>用户集合</returns>
        public static List <Role> GetUserRole(string userGuid)
        {
            API         api      = new API();
            List <Role> roleList = null;

            using (SysDbContext context = api.CreateDbContext())
            {
                var role = from r in context.Roles
                           where
                           (
                    from ru in context.RoleUsers where ru.UserGuid == userGuid select ru.RoleGuid
                           ).Contains(r.Guid)
                           select r;

                roleList = role.ToList();
                context.Dispose();
            }
            return(roleList);
        }