public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); DbWhere dbWhere = new DbWhere(); dbWhere.sql = " 1=1"; WebHelper.AddHttpItems("DataAhthorCondition", dbWhere); }
/// <summary> /// 执行权限认证 /// </summary> /// <param name="filterContext">当前连接</param> /// <returns></returns> private bool DataAuthorize(AuthorizationContext filterContext) { var areaName = filterContext.RouteData.DataTokens["area"] + "/"; //获取当前区域 var controllerName = filterContext.RouteData.Values["controller"] + "/"; //获取控制器 var action = filterContext.RouteData.Values["Action"]; //获取当前Action string currentUrl = "/" + areaName + controllerName + action; //拼接构造完整url WebHelper.AddHttpItems("currentUrl", currentUrl); return(dataAuthorizeIBLL.SetWhereSql(currentUrl)); }
/// <summary> /// 设置查询语句 /// </summary> /// <returns></returns> public bool SetWhereSql() { UserInfo userInfo = LoginUserInfo.Get(); if (userInfo.departmentIds != null) { string whereSql = string.Format(" CHARINDEX(F_DepartmentId,'{0}') > 0", string.Join(",", userInfo.departmentIds)); DbWhere dbWhere = new DbWhere(); dbWhere.sql = whereSql; WebHelper.AddHttpItems("DataAhthorCondition", dbWhere); } return(true); }
/// <summary> /// 设置查询语句 /// </summary> /// <param name="url">接口地址</param> /// <returns></returns> public bool SetWhereSql(string url) { try { UserInfo userInfo = LoginUserInfo.Get(); if (userInfo.isSystem) { return(true); } // 判断该接口注册了 InterfaceEntity interfaceEntity = interfaceIBLL.GetEntityByUrl(url); if (interfaceEntity == null) { // 如果接口没有注册则不作过滤 return(true); } else { List <DataAuthorizeRelationEntity> relationList = (List <DataAuthorizeRelationEntity>)GetRelationList(interfaceEntity.F_Id); if (relationList.Count > 0) { relationList = relationList.FindAll(t => t.F_ObjectId.Equals(userInfo.userId) || t.F_ObjectId.Like(userInfo.roleIds)); if (relationList.Count > 0) { string whereSql = ""; DbWhere dbWhere = new DbWhere(); dbWhere.dbParameters = new List <FieldValueParam>(); int relationnum = 0; foreach (var item in relationList) { if (whereSql != "") { whereSql += " OR "; } whereSql += " ( "; string strSql = ""; List <DataAuthorizeConditionEntity> conditionList = (List <DataAuthorizeConditionEntity>)GetDataAuthorizeConditionList(item.F_Id); if (!string.IsNullOrEmpty(item.F_Formula)) { strSql = item.F_Formula; for (int i = 1; i < conditionList.Count + 1; i++) { strSql = strSql.Replace("" + i, "{@ayma" + i + "ayma@}"); } } else { for (int i = 1; i < conditionList.Count + 1; i++) { if (strSql != "") { strSql += " AND "; } strSql += " {@ayma" + i + "ayma@} "; } } int num = 1; foreach (var conditionItem in conditionList) { string strone = " " + conditionItem.F_FieldId; string value = " @" + conditionItem.F_FieldId + relationnum; FieldValueParam dbParameter = new FieldValueParam(); dbParameter.name = conditionItem.F_FieldId + relationnum; dbParameter.value = getValue(conditionItem.F_FiledValueType, conditionItem.F_FiledValue); dbParameter.type = conditionItem.F_FieldType; dbWhere.dbParameters.Add(dbParameter); //[{ value: 1, text: '等于' }, { value: 2, text: '大于' }, { value: 3, text: '大于等于' }, { value: 4, text: '小于' }, { value: 5, text: '小于等于' }, { value: 6, text: '包含' }, { value: 7, text: '包含于' }, { value: 8, text: '不等于' }, { value: 9, text: '不包含' }, { value: 10, text: '不包含于' }], switch (conditionItem.F_Symbol) { case 1: // 等于 strone += " = " + value; break; case 2: // 大于 strone += " > " + value; break; case 3: // 大于等于 strone += " >= " + value; break; case 4: // 小于 strone += " < " + value; break; case 5: // 小于等于 strone += " <= " + value; break; case 6: // 包含 strone += " like %" + value + "%"; break; case 7: // 包含于 strone += " in ( '" + value.Replace(",", "','") + "' )"; break; case 8: // 不等于 strone += " != " + value; break; case 9: // 不包含 strone += " not like %" + value + "%"; break; case 10: // 不包含于 strone += " not in ( '" + value.Replace(",", "','") + "' )"; break; default: break; } strone += " "; strSql = strSql.Replace("{@ayma" + num + "ayma@}", strone); num++; } whereSql += strSql; whereSql += " ) "; relationnum++; } dbWhere.sql = whereSql; WebHelper.AddHttpItems("DataAhthorCondition", dbWhere); } else { return(false); } } else { // 该接口没做权限过滤 return(true); } } return(true); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } }
/// <summary> /// 响应前执行登录验证,查看当前用户是否有效 /// </summary> /// <param name="filterContext"></param> public override void OnAuthorization(AuthorizationContext filterContext) { // 登录拦截是否忽略 if (_customMode == FilterMode.Ignore) { return; } var request = filterContext.HttpContext.Request; // 用户win客户端的打开 string user = request.QueryString["user"]; string pwd = request.QueryString["pwd"]; if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(pwd)) { UserEntity userEntity = userBll.CheckLogin(user, pwd); if (userEntity.LoginOk) { OperatorHelper.Instance.AddLoginUser(user, "Learun_ADMS_6.1_PC", null);//写入缓存信息 } } //免密登录示例 /*string token = request.QueryString["token"]; * string account2 = request.QueryString["account"]; * if (!string.IsNullOrEmpty(token)) * { * * OperatorHelper.Instance.AddLoginUser(account2, "Learun_ADMS_6.1_PC", null);//写入缓存信息 * } */ string account = ""; if (!request.Headers["account"].IsEmpty()) { account = request.Headers["account"].ToString(); } var areaName = filterContext.RouteData.DataTokens["area"] + "/"; //获取当前区域 var controllerName = filterContext.RouteData.Values["controller"] + "/"; //获取控制器 var action = filterContext.RouteData.Values["Action"]; //获取当前Action string currentUrl = "/" + areaName + controllerName + action; //拼接构造完整url WebHelper.AddHttpItems("currentUrl", currentUrl); var _currentUrl = WebHelper.GetHttpItems("currentUrl"); if (_currentUrl.IsEmpty()) { WebHelper.AddHttpItems("currentUrl", currentUrl); } else { WebHelper.UpdateHttpItem("currentUrl", currentUrl); } // 验证登录状态 int res = OperatorHelper.Instance.IsOnLine(account).stateCode; if (res != 1)// 登录过期或者未登录 { if (res == 2) { if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = new ContentResult { Content = new ResParameter { code = ResponseCode.nologin, info = "other" }.ToJson() }; } else { filterContext.Result = new RedirectResult("~/Login/Index?error=other"); } return; } if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = new ContentResult { Content = new ResParameter { code = ResponseCode.nologin, info = "nologin" }.ToJson() }; } else { filterContext.Result = new RedirectResult("~/Login/Index"); } return; } // IP过滤 if (!this.FilterIP()) { if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = new ContentResult { Content = new ResParameter { code = ResponseCode.nologin, info = "noip" }.ToJson() }; } else { filterContext.Result = new RedirectResult("~/Login/Index?error=ip"); } return; } // 时段过滤 if (!this.FilterTime()) { if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = new ContentResult { Content = new ResParameter { code = ResponseCode.nologin, info = "notime" }.ToJson() }; } else { filterContext.Result = new RedirectResult("~/Login/Index?error=time"); } return; } // 判断当前接口是否需要加载数据权限 if (!this.DataAuthorize(currentUrl)) { filterContext.Result = new ContentResult { Content = new ResParameter { code = ResponseCode.fail, info = "没有该数据权限" }.ToJson() }; return; } }