PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms 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
  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
 22using System;
 23using System.Configuration;
 24using System.IO;
 25using System.Xml;
 26using System.Xml.Linq;
 27using System.Linq;
 28using System.Xml.XPath;
 29using System.Reflection;
 30using System.Web.Configuration;
 31
 32using DotNetNuke.Services.Installer;
 33
 34namespace DotNetNuke.Tests.UI.WatiN.Utilities
 35{
 36    public static class WebConfigManager
 37    {
 38        public static string GetWebPath()
 39        {
 40            var webPath = Directory.GetCurrentDirectory().Replace("\\Tests\\Fixtures", "\\Website");
 41
 42            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["DefaultPhysicalAppPath"]))
 43            {
 44                webPath = ConfigurationManager.AppSettings["DefaultPhysicalAppPath"];
 45            }
 46
 47            return webPath;
 48        }
 49
 50        /// <summary>
 51        /// Updates the web.config so that the site will run in full trust.
 52        /// </summary>
 53        public static void UpdateConfigForFullTrust()
 54        {
 55            var physicalPath = GetWebPath();
 56            var webConfig = XDocument.Load(Path.Combine(physicalPath, "web.config"));
 57            var trustLevel = webConfig.XPathSelectElement("configuration/system.web/trust");
 58            using (var outfile = new StreamWriter(Directory.GetCurrentDirectory() + @"\log.txt"))
 59            {
 60                outfile.Write(physicalPath);
 61                outfile.Write(trustLevel.ToString());
 62            }
 63            trustLevel.Attribute("level").Value = "Full";
 64            webConfig.Save(Path.Combine(physicalPath, "web.config"));
 65        }
 66
 67        /// <summary>
 68        /// Updates the web.config so that the site will run in full trust.
 69        /// </summary>
 70        public static void UpdateConfigForMediumTrust()
 71        {
 72            var physicalPath = GetWebPath();
 73            var webConfig = XDocument.Load(Path.Combine(physicalPath, "web.config"));
 74            var trustLevel = webConfig.XPathSelectElement("configuration/system.web/trust");
 75            using (var outfile = new StreamWriter(Directory.GetCurrentDirectory() + @"\log.txt"))
 76            {
 77                outfile.Write(physicalPath);
 78                outfile.Write(trustLevel.ToString());
 79            }
 80            trustLevel.Attribute("level").Value = "Medium";
 81            webConfig.Save(Path.Combine(physicalPath, "web.config"));
 82        }
 83
 84        /// <summary>
 85        /// Updates the web.config file to drop emails to a local folder.
 86        /// </summary>
 87        /// <param name="mailDropPath">The path to the mailDrop.xml file that contains the xml for the mail dump.</param>
 88        /// <param name="emailPath">The path that emails will be sent to.</param>
 89        public static void UpdateConfigForMailDrop(string mailDropPath, string emailPath)
 90        {
 91            var physicalPath = GetWebPath(); 
 92            var mailDropFragment = XDocument.Load(Path.Combine(mailDropPath, "mailDrop.xml"));
 93            var specifiedPickupDirectory = mailDropFragment.XPathSelectElement("configuration/nodes/node/system.net/mailSettings/smtp/specifiedPickupDirectory");
 94            specifiedPickupDirectory.Attribute("pickupDirectoryLocation").Value = emailPath;
 95            mailDropFragment.Save(Path.Combine(physicalPath, "UpdatedMailDrop.xml"));
 96            var mailDrop = new FileStream(Path.Combine(physicalPath, "UpdatedMailDrop.xml"), FileMode.Open, FileAccess.Read);
 97            try
 98            {
 99                var fileName = string.Format("{0}\\web.config", physicalPath);
100                var targetDocument = new XmlDocument();
101                targetDocument.Load(fileName);
102                var mailNodes = (from mail in targetDocument.DocumentElement.ChildNodes.Cast<XmlNode>()
103                                 where mail.Name == "system.net"
104                                 select mail).ToList();
105                if (mailNodes.Count == 0)
106                {
107                    var merge = new XmlMerge(mailDrop, String.Empty, String.Empty);
108                    merge.UpdateConfig(targetDocument);
109                    targetDocument.Save(fileName);
110                }
111            }
112            finally
113            {
114                mailDrop.Close();
115            }
116        }
117
118        /// <summary>
119        /// Updates the web.config so that the site will run in full trust.
120        /// </summary>
121        /// <param name="physicalPath">The path for the folder containing the web.config.</param>
122        public static void UpdateConfigForFullTrust(string physicalPath)
123        {
124
125            var webConfig = XDocument.Load(Path.Combine(physicalPath, "web.config"));
126            var trustLevel = webConfig.XPathSelectElement("configuration/system.web/trust");
127            using (var outfile = new StreamWriter(Directory.GetCurrentDirectory() + @"\log.txt"))
128            {
129                outfile.Write(physicalPath);
130                outfile.Write(trustLevel.ToString());
131            }
132            trustLevel.Attribute("level").Value = "Full";
133            webConfig.Save(Path.Combine(physicalPath, "web.config"));
134        }
135
136        public static void SyncConfig(string sitePath)
137        {
138            var configFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
139            var webConfigPath = Path.Combine(sitePath, "web.config");
140
141            var dllConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
142            var webConfig = XDocument.Load(webConfigPath);
143
144            var keySection = dllConfig.GetSection("system.web/machineKey") as MachineKeySection;
145            var webNode = webConfig.XPathSelectElement("configuration/system.web/machineKey");
146
147            if (keySection.ValidationKey != webNode.Attribute("validationKey").Value)
148            {
149                keySection.ValidationKey = webNode.Attribute("validationKey").Value;
150                keySection.DecryptionKey = webNode.Attribute("decryptionKey").Value;
151
152                dllConfig.Save();
153
154                Type type = Assembly.GetAssembly(typeof(System.Web.TraceContext)).GetType("System.Web.Configuration.RuntimeConfig");
155                var fieldInfo = type.GetField("s_clientRuntimeConfig", BindingFlags.NonPublic | BindingFlags.Static);
156                fieldInfo.SetValue(null, null);
157                ConfigurationManager.RefreshSection("system.web/machineKey");
158            }
159        }
160
161        public static void TouchConfig(string sitePath)
162        {
163            var webConfigPath = Path.Combine(sitePath, "web.config");
164            File.SetLastWriteTime(webConfigPath, DateTime.Now);
165        }
166    }
167}