// 从注册表和 library.xml 文件中获得实例信息 // parameters: // // return: // -1 出错 // 0 实例没有找到 // 1 成功 public static int GetLibraryInstanceInfo( string strInstanceNameParam, out LibraryInstanceInfo info, out string strError) { strError = ""; info = new LibraryInstanceInfo(); string strInstanceName = ""; string strDataDir = ""; string strCertificatSN = ""; string[] existing_urls = null; for (int i = 0; ; i++) { bool bRet = InstallHelper.GetInstanceInfo("dp2Library", i, out strInstanceName, out strDataDir, out existing_urls, out strCertificatSN); if (bRet == false) { strError = "实例 '" + strInstanceNameParam + "' 不存在"; return(0); } if (strInstanceName == strInstanceNameParam) { info.InstanceName = strInstanceName; info.Urls = existing_urls; info.DataDir = strDataDir; break; } } string strFileName = Path.Combine(strDataDir, "library.xml"); if (File.Exists(strFileName) == false) { strError = "实例 '" + strInstanceNameParam + "' 的 library.xml 文件不存在"; return(0); } XmlDocument dom = new XmlDocument(); try { dom.Load(strFileName); } catch (Exception ex) { strError = "文件 '" + strFileName + "' 装载到 XMLDOM 时出错: " + ex.Message; return(-1); } if (dom.DocumentElement == null) { strError = "文件 '" + strFileName + "' 格式不正确,缺乏根元素"; return(-1); } info.Version = LibraryServerUtil.GetLibraryXmlVersion(dom); // supervisor // XmlElement nodeSupervisor = dom.DocumentElement.SelectSingleNode("accounts/account[@type='']") as XmlElement; XmlElement nodeSupervisor = null; // 找到第一个具备 managedatabase 权限用户 XmlNodeList nodes = dom.DocumentElement.SelectNodes("accounts/account[@type='']"); if (nodes.Count > 0) { foreach (XmlElement account in nodes) { string strRights = account.GetAttribute("rights"); if (StringUtil.IsInList("managedatabase", strRights) == true) { nodeSupervisor = account; break; } } } if (nodeSupervisor != null) { info.SupervisorUserName = nodeSupervisor.GetAttribute("name"); info.SupervisorPassword = nodeSupervisor.GetAttribute("password"); if (info.Version <= 2.0) { // library.xml 2.00 及以前的做法 try { info.SupervisorPassword = Cryptography.Decrypt(info.SupervisorPassword, "dp2circulationpassword"); } catch { strError = "<account password='******' /> 中的密码不正确"; return(-1); } // 得到 supervisor 密码的明文 } } string strValue = dom.DocumentElement.GetAttribute("_initialDatabase"); if (DomUtil.IsBooleanTrue(strValue, false) == true) { info.InitialDatabase = true; } else { info.InitialDatabase = false; } return(1); }
// 从注册表和 library.xml 文件中获得实例信息 // parameters: // // return: // -1 出错 // 0 实例没有找到 // 1 成功 public static int GetLibraryInstanceInfo( string strInstanceNameParam, out LibraryInstanceInfo info, out string strError) { strError = ""; info = new LibraryInstanceInfo(); string strInstanceName = ""; string strDataDir = ""; string strCertificatSN = ""; string[] existing_urls = null; for (int i = 0; ; i++) { bool bRet = InstallHelper.GetInstanceInfo("dp2Library", i, out strInstanceName, out strDataDir, out existing_urls, out strCertificatSN); if (bRet == false) { strError = "实例 '" + strInstanceNameParam + "' 不存在"; return 0; } if (strInstanceName == strInstanceNameParam) { info.InstanceName = strInstanceName; info.Urls = existing_urls; info.DataDir = strDataDir; break; } } string strFileName = Path.Combine(strDataDir, "library.xml"); if (File.Exists(strFileName) == false) { strError = "实例 '" + strInstanceNameParam + "' 的 library.xml 文件不存在"; return 0; } XmlDocument dom = new XmlDocument(); try { dom.Load(strFileName); } catch (Exception ex) { strError = "文件 '" + strFileName + "' 装载到 XMLDOM 时出错: " + ex.Message; return -1; } if (dom.DocumentElement == null) { strError = "文件 '" + strFileName + "' 格式不正确,缺乏根元素"; return -1; } info.Version = LibraryServerUtil.GetLibraryXmlVersion(dom); // supervisor // XmlElement nodeSupervisor = dom.DocumentElement.SelectSingleNode("accounts/account[@type='']") as XmlElement; XmlElement nodeSupervisor = null; // 找到第一个具备 managedatabase 权限用户 XmlNodeList nodes = dom.DocumentElement.SelectNodes("accounts/account[@type='']"); if (nodes.Count > 0) { foreach (XmlElement account in nodes) { string strRights = account.GetAttribute("rights"); if (StringUtil.IsInList("managedatabase", strRights) == true) { nodeSupervisor = account; break; } } } if (nodeSupervisor != null) { info.SupervisorUserName = nodeSupervisor.GetAttribute("name"); info.SupervisorPassword = nodeSupervisor.GetAttribute("password"); if (info.Version <= 2.0) { // library.xml 2.00 及以前的做法 try { info.SupervisorPassword = Cryptography.Decrypt(info.SupervisorPassword, "dp2circulationpassword"); } catch { strError = "<account password='******' /> 中的密码不正确"; return -1; } // 得到 supervisor 密码的明文 } } string strValue = dom.DocumentElement.GetAttribute("_initialDatabase"); if (DomUtil.IsBooleanTrue(strValue, false) == true) info.InitialDatabase = true; else info.InitialDatabase = false; return 1; }
// 要求操作者用 supervisor 账号登录一次。以便后续进行各种重要操作。 // 只需要 library.xml 即可,不需要 dp2library 在运行中。 // return: // -2 实例没有找到 // -1 出错 // 0 放弃验证 // 1 成功 public static int LibrarySupervisorLogin(IWin32Window owner, string strInstanceName, string strComment, out string strError) { strError = ""; LibraryInstanceInfo info = null; // return: // -1 出错 // 0 实例没有找到 // 1 成功 int nRet = GetLibraryInstanceInfo( strInstanceName, out info, out strError); if (nRet == -1) { return(-1); } if (nRet == 0) { strError = "实例 '" + strInstanceName + "' 没有找到"; return(-2); } if (string.IsNullOrEmpty(info.SupervisorUserName) == true) { // TODO: 此时是否可以不用验证了呢? strError = "实例 '" + strInstanceName + "' 的账户中,没有找到具有 managedatabase 权限的管理员账户,因此无法验证操作者身份"; return(-1); } ConfirmSupervisorDialog dlg = new ConfirmSupervisorDialog(); GuiUtil.AutoSetDefaultFont(dlg); dlg.Comment = strComment; dlg.ServerUrl = "实例 '" + strInstanceName + "'"; dlg.UserName = info.SupervisorUserName; dlg.StartPosition = FormStartPosition.CenterScreen; REDO_LOGIN: dlg.ShowDialog(owner); if (dlg.DialogResult == DialogResult.Cancel) { return(0); } if (info.Version <= 2.0) { // 以前的做法 if (dlg.Password != info.SupervisorPassword) { MessageBox.Show(owner, "密码不正确。请重新输入密码"); goto REDO_LOGIN; } } else { // 新的做法 nRet = LibraryServerUtil.MatchUserPassword(dlg.Password, info.SupervisorPassword, out strError); if (nRet == -1) { strError = "MatchUserPassword() error: " + strError; return(-1); } Debug.Assert(nRet == 0 || nRet == 1, ""); if (nRet == 1) { MessageBox.Show(owner, "密码不正确。请重新输入密码"); goto REDO_LOGIN; } } return(1); }