PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/DNN Platform/Tests/DotNetNuke.Tests.Utilities/WebConfigManager.cs

https://bitbucket.org/Jamie_Clayton/dnn-community-codeplex-mirror
C# | 167 lines | 117 code | 17 blank | 33 comment | 6 complexity | 7623836b2bdeba6227439b6d4ef8c223 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #region Copyright
  2. //
  3. // DotNetNuke® - http://www.dotnetnuke.com
  4. // Copyright (c) 2002-2013
  5. // by DotNetNuke Corporation
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  8. // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  9. // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  10. // to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all copies or substantial portions
  13. // of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  18. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. // DEALINGS IN THE SOFTWARE.
  20. #endregion
  21. using System;
  22. using System.Configuration;
  23. using System.IO;
  24. using System.Xml;
  25. using System.Xml.Linq;
  26. using System.Linq;
  27. using System.Xml.XPath;
  28. using System.Reflection;
  29. using System.Web.Configuration;
  30. using DotNetNuke.Services.Installer;
  31. namespace DotNetNuke.Tests.UI.WatiN.Utilities
  32. {
  33. public static class WebConfigManager
  34. {
  35. public static string GetWebPath()
  36. {
  37. var webPath = Directory.GetCurrentDirectory().Replace("\\Tests\\Fixtures", "\\Website");
  38. if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["DefaultPhysicalAppPath"]))
  39. {
  40. webPath = ConfigurationManager.AppSettings["DefaultPhysicalAppPath"];
  41. }
  42. return webPath;
  43. }
  44. /// <summary>
  45. /// Updates the web.config so that the site will run in full trust.
  46. /// </summary>
  47. public static void UpdateConfigForFullTrust()
  48. {
  49. var physicalPath = GetWebPath();
  50. var webConfig = XDocument.Load(Path.Combine(physicalPath, "web.config"));
  51. var trustLevel = webConfig.XPathSelectElement("configuration/system.web/trust");
  52. using (var outfile = new StreamWriter(Directory.GetCurrentDirectory() + @"\log.txt"))
  53. {
  54. outfile.Write(physicalPath);
  55. outfile.Write(trustLevel.ToString());
  56. }
  57. trustLevel.Attribute("level").Value = "Full";
  58. webConfig.Save(Path.Combine(physicalPath, "web.config"));
  59. }
  60. /// <summary>
  61. /// Updates the web.config so that the site will run in full trust.
  62. /// </summary>
  63. public static void UpdateConfigForMediumTrust()
  64. {
  65. var physicalPath = GetWebPath();
  66. var webConfig = XDocument.Load(Path.Combine(physicalPath, "web.config"));
  67. var trustLevel = webConfig.XPathSelectElement("configuration/system.web/trust");
  68. using (var outfile = new StreamWriter(Directory.GetCurrentDirectory() + @"\log.txt"))
  69. {
  70. outfile.Write(physicalPath);
  71. outfile.Write(trustLevel.ToString());
  72. }
  73. trustLevel.Attribute("level").Value = "Medium";
  74. webConfig.Save(Path.Combine(physicalPath, "web.config"));
  75. }
  76. /// <summary>
  77. /// Updates the web.config file to drop emails to a local folder.
  78. /// </summary>
  79. /// <param name="mailDropPath">The path to the mailDrop.xml file that contains the xml for the mail dump.</param>
  80. /// <param name="emailPath">The path that emails will be sent to.</param>
  81. public static void UpdateConfigForMailDrop(string mailDropPath, string emailPath)
  82. {
  83. var physicalPath = GetWebPath();
  84. var mailDropFragment = XDocument.Load(Path.Combine(mailDropPath, "mailDrop.xml"));
  85. var specifiedPickupDirectory = mailDropFragment.XPathSelectElement("configuration/nodes/node/system.net/mailSettings/smtp/specifiedPickupDirectory");
  86. specifiedPickupDirectory.Attribute("pickupDirectoryLocation").Value = emailPath;
  87. mailDropFragment.Save(Path.Combine(physicalPath, "UpdatedMailDrop.xml"));
  88. var mailDrop = new FileStream(Path.Combine(physicalPath, "UpdatedMailDrop.xml"), FileMode.Open, FileAccess.Read);
  89. try
  90. {
  91. var fileName = string.Format("{0}\\web.config", physicalPath);
  92. var targetDocument = new XmlDocument();
  93. targetDocument.Load(fileName);
  94. var mailNodes = (from mail in targetDocument.DocumentElement.ChildNodes.Cast<XmlNode>()
  95. where mail.Name == "system.net"
  96. select mail).ToList();
  97. if (mailNodes.Count == 0)
  98. {
  99. var merge = new XmlMerge(mailDrop, String.Empty, String.Empty);
  100. merge.UpdateConfig(targetDocument);
  101. targetDocument.Save(fileName);
  102. }
  103. }
  104. finally
  105. {
  106. mailDrop.Close();
  107. }
  108. }
  109. /// <summary>
  110. /// Updates the web.config so that the site will run in full trust.
  111. /// </summary>
  112. /// <param name="physicalPath">The path for the folder containing the web.config.</param>
  113. public static void UpdateConfigForFullTrust(string physicalPath)
  114. {
  115. var webConfig = XDocument.Load(Path.Combine(physicalPath, "web.config"));
  116. var trustLevel = webConfig.XPathSelectElement("configuration/system.web/trust");
  117. using (var outfile = new StreamWriter(Directory.GetCurrentDirectory() + @"\log.txt"))
  118. {
  119. outfile.Write(physicalPath);
  120. outfile.Write(trustLevel.ToString());
  121. }
  122. trustLevel.Attribute("level").Value = "Full";
  123. webConfig.Save(Path.Combine(physicalPath, "web.config"));
  124. }
  125. public static void SyncConfig(string sitePath)
  126. {
  127. var configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
  128. var webConfigPath = Path.Combine(sitePath, "web.config");
  129. var dllConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  130. var webConfig = XDocument.Load(webConfigPath);
  131. var keySection = dllConfig.GetSection("system.web/machineKey") as MachineKeySection;
  132. var webNode = webConfig.XPathSelectElement("configuration/system.web/machineKey");
  133. if (keySection.ValidationKey != webNode.Attribute("validationKey").Value)
  134. {
  135. keySection.ValidationKey = webNode.Attribute("validationKey").Value;
  136. keySection.DecryptionKey = webNode.Attribute("decryptionKey").Value;
  137. dllConfig.Save();
  138. Type type = Assembly.GetAssembly(typeof(System.Web.TraceContext)).GetType("System.Web.Configuration.RuntimeConfig");
  139. var fieldInfo = type.GetField("s_clientRuntimeConfig", BindingFlags.NonPublic | BindingFlags.Static);
  140. fieldInfo.SetValue(null, null);
  141. ConfigurationManager.RefreshSection("system.web/machineKey");
  142. }
  143. }
  144. public static void TouchConfig(string sitePath)
  145. {
  146. var webConfigPath = Path.Combine(sitePath, "web.config");
  147. File.SetLastWriteTime(webConfigPath, DateTime.Now);
  148. }
  149. }
  150. }