Beispiel #1
0
        /// <summary>
        /// 获取到用户可用module
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="orgId">当前组织机构</param>
        /// <param name="moduleEname">模块英文名称,如果不为空则返回该模块的所有子集,如果为空则返回所有</param>
        /// <returns>json</returns>
        public object GetUserModules(string userId, string orgId, string moduleEname)
        {
            try
            {
                List <Base_ModuleInfo> moduleList;
                //缓存Key值
                string cacheKey = userId + orgId + "_UserAuthModule";
                //判断缓存是否存在
                #region 缓存存在数据,取出缓存数据
                //缓存中存在菜单字典取出数据,如果字典中不存在该用户键值,取出数据存入
                object info = CacheHelper.GetLocalCache(cacheKey);
                if (info != null)
                {
                    moduleList = (List <Base_ModuleInfo>)info;
                }
                #endregion

                #region 缓存不存在,取出数据存入创建缓存
                else
                {
                    // 缓存中不存在菜单字典,创建字典存入缓存
                    moduleList = _authBll.GetAllowModuleByUser(userId, orgId);
                    if (moduleList.Count > 0)
                    {
                        CacheHelper.SaveLocalCache(null, TimeSpan.FromHours(_authBll.CacheHour), moduleList, cacheKey);
                    }
                    else
                    {
                        throw new PermissionLimittedException("noAuth");
                    }
                }
                #endregion

                #region 过滤结果集合
                if (moduleList != null & moduleList.Count > 0)
                {
                    //转化成json串
                    if (!string.IsNullOrEmpty(moduleEname))//如果moduleEname不为空值,代表请求某菜单下的按钮
                    {
                        Base_ModuleInfo parentObj      = moduleList.Where(op => op.EName == moduleEname).FirstOrDefault();
                        string          parentModuleId = Guid.NewGuid().ToString().ToUpper();//默认给一个新创建的Guid
                        if (parentObj != null)
                        {
                            parentModuleId = parentObj.OID; //更改Guid值
                        }
                        moduleList = moduleList.Where(op => op.Flag == 1 || op.Flag == 3 || op.Flag == 4).Where(op => op.ParentOID == parentModuleId.ToString()).OrderBy(op => op.LT).ToList();
                    }
                    else//如果moduleEname为空值,代表请求整个权限菜单
                    {
                        moduleList = moduleList.Where(op => op.Flag == 0).OrderBy(op => op.LT).ToList();
                    }
                }
                #endregion

                #region 过滤结果字段
                object        delresult;
                List <string> removeCols = new List <string>();
                removeCols.Add("Buttons");
                removeCols.Add("Orgs");
                removeCols.Add("Permissions");
                delresult = Common.NewtonJsonHelper.Deserialize <object>(Common.NewtonJsonHelper.Serialize(moduleList, removeCols), null);
                #endregion

                return(delresult);
            }
            catch (Exception err)
            {
                throw new PermissionLimittedException("noAuth", err);
            }
        }