/// <summary> /// 根据用户FID获取已授权的ROLE集合 /// </summary> /// <param name="userFid"></param> /// <returns></returns> public List <UserRoleInfo> GetRolesByUser(Guid userFid, string textOrder, int pageIndex, int pageRow , out int dataCount) { dataCount = new UserRoleDAL().GetCounts("and [USER_FID] = '" + userFid + "' and [VALID_FLAG] = 1"); ///用户角色关系数据 List <UserRoleInfo> list = new UserRoleDAL().GetListByPage("and [USER_FID] = '" + userFid + "' and [VALID_FLAG] = 1", textOrder, pageIndex, pageRow); ///角色用户例外条件数据 List <UserRoleRangeAuthInfo> roleuserconditionlist = new UserRoleRangeAuthDAL().GetList("and [USER_FID] = '" + userFid + "' and [VALID_FLAG] = 1 ", string.Empty); ///例外条件基础数据 List <RangeAuthConditionInfo> userroleconditionlist = new RangeAuthConditionDAL().GetList("and [VALID_FLAG] = 1", string.Empty); foreach (var info in list) { for (int i = 1; i <= 20; i++) { if (i > userroleconditionlist.Count) { break; } List <UserRoleRangeAuthInfo> roleuserconditions = roleuserconditionlist.Where(d => d.UserFid.GetValueOrDefault() == info.Fid.GetValueOrDefault() && d.ConditionFid.GetValueOrDefault() == userroleconditionlist[i - 1].Fid.GetValueOrDefault()).ToList(); if (roleuserconditions.Count == 0) { continue; } info.GetType().GetProperty("ExtendField" + i).SetValue(info, string.Join(",", roleuserconditions.Select(d => d.ConditionContext).ToArray()), null); } } return(list); }
/// <summary> /// GetListByPage /// </summary> /// <param name="textWhere"></param> /// <param name="textOrder"></param> /// <param name="pageIndex"></param> /// <param name="pageRow"></param> /// <param name="dataCount"></param> /// <returns></returns> public List <UserRoleInfo> GetListByPage(string textWhere, string textOrder, int pageIndex, int pageRow, out int dataCount) { dataCount = dal.GetCounts(textWhere); ///用户角色关系数据 List <UserRoleInfo> list = dal.GetListByPage(textWhere, textOrder, pageIndex, pageRow); ///角色用户例外条件数据 List <UserRoleRangeAuthInfo> userRoleRangeAuthInfos = new UserRoleRangeAuthDAL().GetList(textWhere, string.Empty); ///例外条件基础数据 List <RangeAuthConditionInfo> rangeAuthConditionInfos = new RangeAuthConditionDAL().GetList(string.Empty, string.Empty); foreach (UserRoleInfo info in list) { foreach (RangeAuthConditionInfo rangeAuthConditionInfo in rangeAuthConditionInfos) { UserRoleRangeAuthInfo userRoleRangeAuthInfo = userRoleRangeAuthInfos.FirstOrDefault(d => d.UserFid.GetValueOrDefault() == info.Fid.GetValueOrDefault()); if (userRoleRangeAuthInfo == null) { continue; } PropertyInfo propertyInfo = info.GetType().GetProperty(rangeAuthConditionInfo.AttributeName); if (propertyInfo == null) { continue; } propertyInfo.SetValue(info, userRoleRangeAuthInfo.ConditionContext, null); } } return(list); }
/// <summary> /// /// </summary> /// <param name="ids"></param> /// <param name="userFid"></param> /// <param name="roleFid"></param> /// <param name="conditionFid"></param> /// <param name="loginUser"></param> /// <returns></returns> public bool SetRangeAuth(string[] ids, Guid userFid, Guid roleFid, Guid conditionFid, string loginUser) { RangeAuthConditionInfo rangeAuthConditionInfo = new RangeAuthConditionDAL().GetInfo(conditionFid); if (rangeAuthConditionInfo == null) { throw new Exception("MC:0x00000084");///数据错误 } if (ids.Length == 0) { return(CommonDAL.ExecuteNonQueryBySql("insert into dbo.[TS_SYS_USER_ROLE_RANGE_AUTH] (" + "FID, USER_FID, ROLE_FID, CONDITION_FID, CONDITION_CONTEXT, COMMENTS, VALID_FLAG, CREATE_USER, CREATE_DATE) values (" + "NEWID(), N'" + userFid + "', N'" + roleFid + "', N'" + conditionFid + "', N'1=1', N'', 1, N'" + loginUser + "', GETDATE());")); } List <UserRoleRangeAuthInfo> userRoleRangeAuthInfos = new UserRoleRangeAuthDAL().GetList("" + "[USER_FID] = N'" + userFid + "' and " + "[ROLE_FID] = N'" + roleFid + "' and " + "[CONDITION_FID] = N'" + conditionFid + "'", string.Empty); string sql = "select " + rangeAuthConditionInfo.FieldName + " from " + rangeAuthConditionInfo.TableName + " with(nolock) where [VALID_FLAG] = 1 and [ID] in (" + string.Join(",", ids) + ");"; DataTable dataTable = CommonDAL.ExecuteDataTableBySql(sql); string insertSql = string.Empty; UserRoleRangeAuthInfo userRoleRangeAuthInfo = userRoleRangeAuthInfos.FirstOrDefault(d => d.ConditionContext == "1=1"); if (userRoleRangeAuthInfo != null) { insertSql += "update dbo.[TS_SYS_USER_ROLE_RANGE_AUTH] " + "set [VALID_FLAG] = 0,[MODIFY_DATE] = GETDATE(),[MODIFY_USER] = N'" + loginUser + "' " + "where [ID] = " + userRoleRangeAuthInfo.Id + ";"; } foreach (DataRow dr in dataTable.Rows) { string conditionContext = dr[rangeAuthConditionInfo.FieldName].ToString(); userRoleRangeAuthInfo = userRoleRangeAuthInfos.FirstOrDefault(d => d.ConditionContext == conditionContext); if (userRoleRangeAuthInfo != null) { continue; } insertSql += "insert into dbo.[TS_SYS_USER_ROLE_RANGE_AUTH] (" + "FID, USER_FID, ROLE_FID, CONDITION_FID, CONDITION_CONTEXT, COMMENTS, VALID_FLAG, CREATE_USER, CREATE_DATE) values (" + "NEWID(), N'" + userFid + "', N'" + roleFid + "', N'" + conditionFid + "', N'" + conditionContext + "', N'', 1, N'" + loginUser + "', GETDATE());"; } if (insertSql.Length == 0) { return(true); } return(CommonDAL.ExecuteNonQueryBySql(insertSql)); }
/// <summary> /// GetUserRangeAuths /// </summary> /// <param name="userFid"></param> /// <param name="roleFid"></param> /// <param name="rangeAuthConditionInfos"></param> /// <returns></returns> public List <UserRoleRangeAuthInfo> GetUserRangeAuths(Guid userFid, Guid roleFid, out List <RangeAuthConditionInfo> rangeAuthConditionInfos) { rangeAuthConditionInfos = new RangeAuthConditionDAL().GetList(string.Empty, string.Empty); return(dal.GetList("[USER_FID] = N'" + userFid + "' and [ROLE_FID] = N'" + roleFid + "'", string.Empty)); }
/// <summary> /// GetRangeAuthList /// </summary> /// <param name="userFid"></param> /// <param name="roleFid"></param> /// <param name="conditionFid"></param> /// <param name="textWhere"></param> /// <param name="textOrder"></param> /// <param name="pageIndex"></param> /// <param name="pageRow"></param> /// <param name="dataCount"></param> /// <returns></returns> public List <RangeAuthInfo> GetRangeAuthList(Guid userFid, Guid roleFid, Guid conditionFid, string textWhere, string textOrder, int pageIndex, int pageRow, out int dataCount) { dataCount = 0; RangeAuthConditionInfo rangeAuthConditionInfo = new RangeAuthConditionDAL().GetInfo(conditionFid); if (rangeAuthConditionInfo == null) { return(new List <RangeAuthInfo>()); } List <UserRoleRangeAuthInfo> userRoleRangeAuthInfos = new UserRoleRangeAuthDAL().GetList("" + "[USER_FID] = N'" + userFid + "' and " + "[ROLE_FID] = N'" + roleFid + "' and " + "[CONDITION_FID] = N'" + conditionFid + "'", string.Empty); /// string whereText = string.Empty; if (!string.IsNullOrEmpty(textWhere)) { if (textWhere.Trim().StartsWith("and", StringComparison.OrdinalIgnoreCase)) { whereText += " where [VALID_FLAG] = 1 " + textWhere; } else { whereText += " where " + textWhere + " and [VALID_FLAG] = 1"; } } else { whereText += " where [VALID_FLAG] = 1 "; } /// whereText = whereText.Replace("[ConditionFieldValue]", "[" + rangeAuthConditionInfo.FieldName + "]"); whereText = whereText.Replace("[ConditionFieldDisplay]", "[" + rangeAuthConditionInfo.DisplayFieldName + "]"); if (string.IsNullOrEmpty(textOrder)) { textOrder += "[ID] desc"; } ///DATA_CNT object cnt = CommonDAL.ExecuteScalar("select count(1) from " + rangeAuthConditionInfo.TableName + " with(nolock) " + whereText + ";"); if (cnt == null || cnt == DBNull.Value) { dataCount = 0; } dataCount = Convert.ToInt32(cnt); ///DATA string sql = "select top " + pageRow + " * from " + "(select row_number() over(order by " + textOrder + ") as rownumber" + ",* from " + rangeAuthConditionInfo.TableName + " with(nolock) " + whereText + ") T " + "where rownumber > " + (pageIndex - 1) * pageRow + " "; DataTable dataTable = CommonDAL.ExecuteDataTableBySql(sql); List <RangeAuthInfo> rangeAuthInfos = new List <RangeAuthInfo>(); foreach (DataRow dr in dataTable.Rows) { RangeAuthInfo rangeAuthInfo = new RangeAuthInfo(); rangeAuthInfo.Id = Convert.ToInt64(dr["ID"]); rangeAuthInfo.RoleFid = roleFid; rangeAuthInfo.ConditionFid = conditionFid; rangeAuthInfo.ConditionFieldValue = dr[rangeAuthConditionInfo.FieldName].ToString(); rangeAuthInfo.ConditionFieldDisplay = dr[rangeAuthConditionInfo.DisplayFieldName].ToString(); UserRoleRangeAuthInfo userRoleRangeAuthInfo = userRoleRangeAuthInfos.FirstOrDefault(d => d.ConditionContext == rangeAuthInfo.ConditionFieldValue); if (userRoleRangeAuthInfo == null) { userRoleRangeAuthInfo = userRoleRangeAuthInfos.FirstOrDefault(d => d.ConditionContext == "1=1"); } rangeAuthInfo.AuthedFlag = userRoleRangeAuthInfo == null ? false : true; rangeAuthInfo.Comments = userRoleRangeAuthInfo == null ? string.Empty : userRoleRangeAuthInfo.Comments; rangeAuthInfos.Add(rangeAuthInfo); } return(rangeAuthInfos); }