PageRenderTime 40ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/WebsitePanel.Installer/Releases/1.1.0/Sources/WebsitePanel.Setup/Wizard/DatabasePage.cs

https://github.com/ShaneMcC/websitepanel
C# | 244 lines | 192 code | 18 blank | 34 comment | 26 complexity | c5717822869f0103a33c344116e4a78c MD5 | raw file
  1. // Copyright (c) 2011, SMB SAAS Systems Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without modification,
  5. // are permitted provided that the following conditions are met:
  6. //
  7. // - Redistributions of source code must retain the above copyright notice, this
  8. // list of conditions and the following disclaimer.
  9. //
  10. // - Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. //
  14. // - Neither the name of the SMB SAAS Systems Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from this
  16. // software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  22. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. using System;
  29. using System.IO;
  30. using System.Collections.Generic;
  31. using System.ComponentModel;
  32. using System.Drawing;
  33. using System.Data;
  34. using System.Text;
  35. using System.Threading;
  36. using System.Windows.Forms;
  37. namespace WebsitePanel.Setup
  38. {
  39. public partial class DatabasePage : BannerWizardPage
  40. {
  41. public DatabasePage()
  42. {
  43. InitializeComponent();
  44. }
  45. protected override void InitializePageInternal()
  46. {
  47. base.InitializePageInternal();
  48. this.Text = "Database Settings";
  49. string component = SetupVariables.ComponentFullName;
  50. this.Description = string.Format("Enter the connection information for the {0} database.", component);
  51. this.lblIntro.Text = "The connection information will be used by the Setup Wizard to install the database objects only. Click Next to continue.";
  52. this.txtDatabase.Text = SetupVariables.Database;
  53. this.txtSqlServer.Text = SetupVariables.DatabaseServer;
  54. this.AllowMoveBack = true;
  55. this.AllowMoveNext = true;
  56. this.AllowCancel = true;
  57. cbAuthentication.SelectedIndex = 0;
  58. ParseConnectionString();
  59. }
  60. private void ParseConnectionString()
  61. {
  62. bool windowsAuthentication = false;
  63. if ( !string.IsNullOrEmpty(SetupVariables.DbInstallConnectionString ))
  64. {
  65. string[] pairs = SetupVariables.DbInstallConnectionString.Split(';');
  66. foreach (string pair in pairs)
  67. {
  68. string[] keyValue = pair.Split('=');
  69. if (keyValue.Length == 2)
  70. {
  71. string key = keyValue[0].Trim().ToLower();
  72. string value = keyValue[1];
  73. switch (key)
  74. {
  75. case "server":
  76. this.txtSqlServer.Text = value;
  77. break;
  78. case "database":
  79. this.txtDatabase.Text = value;
  80. break;
  81. case "integrated security":
  82. if (value.Trim().ToLower() == "sspi")
  83. windowsAuthentication = true;
  84. break;
  85. case "user":
  86. case "user id":
  87. txtLogin.Text = value;
  88. break;
  89. case "password":
  90. txtPassword.Text = value;
  91. break;
  92. }
  93. }
  94. }
  95. cbAuthentication.SelectedIndex = windowsAuthentication ? 0 : 1;
  96. }
  97. }
  98. protected internal override void OnAfterDisplay(EventArgs e)
  99. {
  100. base.OnAfterDisplay(e);
  101. //unattended setup
  102. if (!string.IsNullOrEmpty(SetupVariables.SetupXml))
  103. Wizard.GoNext();
  104. }
  105. protected internal override void OnBeforeMoveNext(CancelEventArgs e)
  106. {
  107. try
  108. {
  109. if (!CheckFields())
  110. {
  111. e.Cancel = true;
  112. return;
  113. }
  114. string connectionString = CreateConnectionString();
  115. string component = SetupVariables.ComponentFullName;
  116. if (CheckConnection(connectionString))
  117. {
  118. // check SQL server version
  119. string sqlVersion = GetSqlServerVersion(connectionString);
  120. if (!sqlVersion.StartsWith("9.") && !sqlVersion.StartsWith("10."))
  121. {
  122. // SQL Server 2005 engine required
  123. e.Cancel = true;
  124. ShowWarning("This program can be installed on SQL Server 2005/2008 only.");
  125. return;
  126. }
  127. int securityMode = GetSqlServerSecurityMode(connectionString);
  128. if (securityMode != 0)
  129. {
  130. // mixed mode required
  131. e.Cancel = true;
  132. ShowWarning("Please switch SQL Server authentication to mixed SQL Server and Windows Authentication mode.");
  133. return;
  134. }
  135. }
  136. else
  137. {
  138. e.Cancel = true;
  139. ShowWarning("SQL Server does not exist or access denied");
  140. return;
  141. }
  142. string database = this.txtDatabase.Text;
  143. if (SqlUtils.DatabaseExists(connectionString, database))
  144. {
  145. e.Cancel = true;
  146. ShowWarning(string.Format("{0} database already exists.", database));
  147. return;
  148. }
  149. string server = this.txtSqlServer.Text;
  150. Log.WriteInfo(string.Format("Sql server \"{0}\" selected for {1}", server, component));
  151. SetupVariables.Database = database;
  152. SetupVariables.DatabaseServer = server;
  153. SetupVariables.DbInstallConnectionString = connectionString;
  154. //AppConfig.SetComponentSettingStringValue(SetupVariables.ComponentId, "Database", database);
  155. //AppConfig.SetComponentSettingStringValue(SetupVariables.ComponentId, "DatabaseServer", server);
  156. //AppConfig.SetComponentSettingStringValue(SetupVariables.ComponentId, "InstallConnectionString", connectionString);
  157. }
  158. catch
  159. {
  160. e.Cancel = true;
  161. ShowError("Unable to configure the database server.");
  162. return;
  163. }
  164. base.OnBeforeMoveNext(e);
  165. }
  166. private void OnAuthenticationChanged(object sender, System.EventArgs e)
  167. {
  168. UpdateFields();
  169. }
  170. private void UpdateFields()
  171. {
  172. bool winAuthentication = (cbAuthentication.SelectedIndex == 0);
  173. txtLogin.Enabled = !winAuthentication;
  174. txtPassword.Enabled = !winAuthentication;
  175. lblLogin.Enabled = !winAuthentication;
  176. lblPassword.Enabled = !winAuthentication;
  177. Update();
  178. }
  179. private bool CheckFields()
  180. {
  181. if (txtSqlServer.Text.Trim().Length == 0)
  182. {
  183. ShowWarning("Please enter valid SQL Server name.");
  184. return false;
  185. }
  186. if (cbAuthentication.SelectedIndex == 1)
  187. {
  188. if (txtLogin.Text.Trim().Length == 0)
  189. {
  190. ShowWarning("Please enter valid Login name.");
  191. return false;
  192. }
  193. }
  194. string database = txtDatabase.Text;
  195. if (database.Trim().Length == 0 || !SqlUtils.IsValidDatabaseName(database))
  196. {
  197. ShowWarning("Please enter valid database name.");
  198. return false;
  199. }
  200. return true;
  201. }
  202. private bool CheckConnection(string connectionString)
  203. {
  204. return SqlUtils.CheckSqlConnection(connectionString);
  205. }
  206. private string GetSqlServerVersion(string connectionString)
  207. {
  208. return SqlUtils.GetSqlServerVersion(connectionString);
  209. }
  210. private int GetSqlServerSecurityMode(string connectionString)
  211. {
  212. return SqlUtils.GetSqlServerSecurityMode(connectionString);
  213. }
  214. private string CreateConnectionString()
  215. {
  216. if (cbAuthentication.SelectedIndex == 0)
  217. {
  218. return SqlUtils.BuildDbServerMasterConnectionString(txtSqlServer.Text, null, null);
  219. }
  220. else
  221. {
  222. return SqlUtils.BuildDbServerMasterConnectionString(txtSqlServer.Text, txtLogin.Text, txtPassword.Text);
  223. }
  224. }
  225. }
  226. }