/// <summary> /// 验证用户模块。 /// </summary> public ModuleDefineCollection AuthenticateUserModules(ModuleDefineCollection modules, GUIDEx systemID, GUIDEx userID) { lock (this) { if (this.securityPermissionFacotry != null && modules != null && systemID.IsValid && userID.IsValid) { string key = string.Format("AUM_{0}_{1}_{2:yyyy-MM-dd-HH}", systemID, userID, DateTime.Now); ModuleDefineCollection result = this.cache[key] as ModuleDefineCollection; if (result == null) { result = new ModuleDefineCollection(); #region 定义表结构。 DataTable dtSource = new DataTable(); dtSource.Columns.Add("ModuleID", typeof(System.String)); dtSource.Columns.Add("ParentModuleID", typeof(System.String)); dtSource.Columns.Add("ModuleName", typeof(System.String)); dtSource.Columns.Add("ModuleUri", typeof(System.String)); dtSource.Columns.Add("OrderNo", typeof(System.Int32)); dtSource.PrimaryKey = new DataColumn[] { dtSource.Columns["ModuleID"] }; #endregion //转换为表结构。 this.AuthenticateUserModules(ref dtSource, modules); #region 校验权限。 if (dtSource != null && dtSource.Rows.Count > 0) { //获取有权限的模块ID。 List<GUIDEx> listRightModules = this.AuthenticateUserModules(dtSource, systemID, userID); if (listRightModules != null && listRightModules.Count > 0) { DataTable dtResult = dtSource.Clone(); //查找有权限的模块ID到结果数据集。 foreach (GUIDEx moduleID in listRightModules) { DataRow dr = dtSource.Rows.Find(moduleID); if (dr != null && (dtResult.Rows.Find(moduleID) == null)) dtResult.ImportRow(dr); } //整理结果数据集的结构。 DataRow[] parentRows = dtResult.Select("isnull(ParentModuleID,'') <> ''"); this.AuthenticateUserModules(ref dtResult, parentRows, dtSource.Copy()); #region 转换为对象。 DataRow[] drs = dtResult.Select("isnull(ParentModuleID,'') = ''", "OrderNo"); if (drs != null) { foreach (DataRow row in drs) { ModuleDefine moduleDefine = new ModuleDefine(Convert.ToString(row["ModuleID"]), Convert.ToString(row["ModuleName"]), Convert.ToString(row["ModuleUri"]), Convert.ToInt32(row["OrderNo"])); this.AuthenticateUserModules(moduleDefine, dtResult.Select(string.Format("isnull(ParentModuleID,'') = '{0}'", row["ModuleID"]), "OrderNo"), dtResult.Copy()); result.Add(moduleDefine); } } #endregion } } #endregion if (result.Count > 0) { this.cache.Insert(key, result, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero); } } return result; } return modules; } }
private string createNodeData(ModuleDefine data) { if (data == null) return null; StringBuilder builder = new StringBuilder(); builder.Append("{") .Append("\"").Append("id").Append("\"").Append(":").Append("\"").Append(data.ModuleID).Append("\"").Append(",") .Append("\"").Append("text").Append("\"").Append(":").Append("\"").Append(data.ModuleName).Append("\"").Append(",") .Append("\"").Append("attributes").Append("\"").Append(":") .Append("{") .Append("\"").Append("url").Append("\"").Append(":").Append("\"").Append(data.ModuleUri).Append("\"") .Append("}"); if (data.Modules != null && data.Modules.Count > 0) { builder.Append(",") .Append("\"").Append("children").Append("\"").Append(":").Append("["); int index = 0; foreach(ModuleDefine child in data.Modules) { string result = this.createNodeData(child); if (!string.IsNullOrEmpty(result)) { if (index > 0) builder.Append(","); builder.Append(result); index += 1; } } builder.Append("]"); } builder.Append("}"); if (builder.Length > 0) return builder.ToString(); return null; }
/// <summary> /// /// </summary> /// <param name="parent"></param> /// <param name="drs"></param> /// <param name="dtResult"></param> private void AuthenticateUserModules(ModuleDefine parent, DataRow[] drs, DataTable dtResult) { if (parent != null && drs != null) { foreach (DataRow row in drs) { ModuleDefine m = new ModuleDefine(Convert.ToString(row["ModuleID"]), Convert.ToString(row["ModuleName"]), Convert.ToString(row["ModuleUri"]), Convert.ToInt32(row["OrderNo"])); this.AuthenticateUserModules(m, dtResult.Select(string.Format("isnull(ParentModuleID,'') = '{0}'", row["ModuleID"]), "OrderNo"), dtResult.Copy()); parent.Modules.Add(m); } } }
private bool AddUniqueModule(Library library, int uid) { ModuleDefine module = new ModuleDefine(uid); if (library.ModuleList.Find(x => x.Uid == module.Uid) == null) { library.ModuleList.Add(module); return true; } return false; }