From e4ab890b0767275706d08490a375b02af5a086c8 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 24 Jan 2015 21:19:31 +0100 Subject: [PATCH] Added System Settings --- Site5/App_GlobalResources/Pages.Admin.resx | 6 + Site5/Controls/v5/Base/AjaxTextBox.ascx | 6 +- Site5/Controls/v5/Base/AjaxTextBox.ascx.cs | 6 +- Site5/Pages/v5/Admin/Settings.aspx | 6 +- Site5/Pages/v5/Admin/Settings.aspx.cs | 115 +++++++++++++++++- .../Communications/CommsTransmitterMail.cs | 10 +- 6 files changed, 133 insertions(+), 16 deletions(-) diff --git a/Site5/App_GlobalResources/Pages.Admin.resx b/Site5/App_GlobalResources/Pages.Admin.resx index fd07e14f..fb6a345b 100644 --- a/Site5/App_GlobalResources/Pages.Admin.resx +++ b/Site5/App_GlobalResources/Pages.Admin.resx @@ -197,6 +197,12 @@ Externally Accessible URL + + Various technical settings for this Swarmops installation. Not specific to any organization or person, but installation-wide. + + + System Settings + SMTP Server (user:pass@host:port) diff --git a/Site5/Controls/v5/Base/AjaxTextBox.ascx b/Site5/Controls/v5/Base/AjaxTextBox.ascx index 824d7aac..ab6f3e64 100644 --- a/Site5/Controls/v5/Base/AjaxTextBox.ascx +++ b/Site5/Controls/v5/Base/AjaxTextBox.ascx @@ -11,13 +11,13 @@ if (currentValue != _initVal_<%=this.TextInput.ClientID%>) { var jsonData = {}; - jsonData.newString = currentValue; + jsonData.newValue = currentValue; jsonData.cookie = '<%=this.Cookie%>'; // The AJAX call expects the following function prototype: // // [WebMethod] - // public static AjaxTextBox.AjaxResult FunctionName (string newString, string cookie); + // public static AjaxTextBox.CallbackResult FunctionName (string newValue, string cookie); // // The page must include IncludedControls.JsonParameters and IncludedControls.ColorAnimation among its script. // @@ -54,7 +54,7 @@ $(this).css('background-color', '#E0FFE0'); _initVal_<%=this.TextInput.ClientID%> = msg.d.NewData; } - $(this).val(modalAccountInitialBalance); + $(this).val(_initVal_<%=this.TextInput.ClientID%>); $(this).animate({ backgroundColor: "#FFFFFF" }, 250); }, this), error: $.proxy(function (msg) { diff --git a/Site5/Controls/v5/Base/AjaxTextBox.ascx.cs b/Site5/Controls/v5/Base/AjaxTextBox.ascx.cs index 65308aea..7d95323f 100644 --- a/Site5/Controls/v5/Base/AjaxTextBox.ascx.cs +++ b/Site5/Controls/v5/Base/AjaxTextBox.ascx.cs @@ -11,7 +11,7 @@ namespace Swarmops.Frontend.Controls.v5.Base { protected void Page_Load(object sender, EventArgs e) { - + this.TextInput.Attributes["Placeholder"] = this.Placeholder; } public string Text @@ -28,6 +28,8 @@ namespace Swarmops.Frontend.Controls.v5.Base public string Cookie { set; get; } public string AjaxCallbackUrl { set; get; } + public string Placeholder { get; set; } + // Ajax result codes and classes public const int CodeUnknown = 0; @@ -36,7 +38,7 @@ namespace Swarmops.Frontend.Controls.v5.Base public const int CodeInvalid = 3; public const int CodeNoPermission = 4; - public class AjaxResult + public class CallbackResult { public int ResultCode { get; set; } public string DisplayMessage { get; set; } // may be empty diff --git a/Site5/Pages/v5/Admin/Settings.aspx b/Site5/Pages/v5/Admin/Settings.aspx index 885beaf0..f846e44c 100644 --- a/Site5/Pages/v5/Admin/Settings.aspx +++ b/Site5/Pages/v5/Admin/Settings.aspx @@ -19,10 +19,12 @@

Correspondence transmission settings

- ​
+ ​
+ ​
diff --git a/Site5/Pages/v5/Admin/Settings.aspx.cs b/Site5/Pages/v5/Admin/Settings.aspx.cs index 4f4720ad..a9750a40 100644 --- a/Site5/Pages/v5/Admin/Settings.aspx.cs +++ b/Site5/Pages/v5/Admin/Settings.aspx.cs @@ -7,6 +7,10 @@ using System.Web.UI.WebControls; using Swarmops.Logic; using Swarmops.Logic.Support; using Swarmops.Logic.Security; +using System.Globalization; +using System.Web.Services; +using System.Text.RegularExpressions; +using Swarmops.Frontend.Controls.v5.Base; namespace Swarmops.Frontend.Pages.v5.Admin { @@ -18,16 +22,115 @@ namespace Swarmops.Frontend.Pages.v5.Admin this.PageAccessRequired = new Access(this.CurrentOrganization, AccessAspect.System, AccessType.Write); this.IncludedControlsUsed = IncludedControl.JsonParameters | IncludedControl.SwitchButton; this.EasyUIControlsUsed = EasyUIControl.Tabs; + this.PageTitle = Resources.Pages.Admin.SystemSettings_PageTitle; + this.InfoBoxLiteral = Resources.Pages.Admin.SystemSettings_Info; if (!Page.IsPostBack) { - this.TextSmtpServer.Text = Persistence.Key["SmtpServer"]; - string smtpPort = Persistence.Key["SmtpPort"]; - if (smtpPort != "25") - { - this.TextSmtpServer.Text += ":" + smtpPort; - } + this.TextSmtpServer.Text = FormatSmtpAccessString(SystemSettings.SmtpUser, SystemSettings.SmtpPassword, + SystemSettings.SmtpHost, SystemSettings.SmtpPort); + this.TextExternalUrl.Text = SystemSettings.ExternalUrl; + + Localize(); } } + + private static string FormatSmtpAccessString (string user, string pass, string host, int port) + { + string result = host; + if (port != 25) + { + result += ":" + port.ToString(CultureInfo.InvariantCulture); + } + + if (!string.IsNullOrEmpty(pass)) + { + user += ":" + pass; + } + + if (!string.IsNullOrEmpty(user)) + { + result = user + "@" + result; + } + + return result; + } + + private void Localize() + { + this.LabelExternalUrl.Text = Resources.Pages.Admin.SystemSettings_ExternalUrl; + this.LabelSmtpServer.Text = Resources.Pages.Admin.SystemSettings_SmtpServer; + } + + [WebMethod] + static public AjaxTextBox.CallbackResult StoreCallback(string newValue, string cookie) + { + AjaxTextBox.CallbackResult result = new AjaxTextBox.CallbackResult(); + + switch (cookie) + { + case "Smtp": + Match match = Regex.Match (newValue, "((?[a-z0-9_]+)(:(?[^@]+))?@)?(?[a-z0-9_\\-\\.]+)(:(?[0-9]+))?", RegexOptions.IgnoreCase); + if (match.Success) + { + string user = match.Groups["user"].Value; + string pass = match.Groups["pass"].Value; + string host = match.Groups["host"].Value; + string portString = match.Groups["port"].Value; + int port = 25; + + if (!string.IsNullOrEmpty(portString)) + { + try + { + port = Int32.Parse(portString); + } + catch (FormatException) + { + result.DisplayMessage = "Port must be numeric. Unable to parse host:port."; + result.ResultCode = AjaxTextBox.CodeInvalid; + return result; // return early + } + } + + SystemSettings.SmtpUser = user ?? string.Empty; + SystemSettings.SmtpPassword = pass ?? string.Empty; + SystemSettings.SmtpHost = host; + SystemSettings.SmtpPort = port; + + result.ResultCode = AjaxTextBox.CodeChanged; + result.NewData = FormatSmtpAccessString (user, pass, host, port); + } + else + { + result.ResultCode = AjaxTextBox.CodeInvalid; + result.DisplayMessage = "Unable to parse user, pass, host, and/or port. (Only host is required.)"; + } + + break; + case "ExtUrl": + if (!newValue.EndsWith("/")) + { + newValue = newValue + "/"; + } + if (!newValue.StartsWith("http://") && !newValue.StartsWith("https://")) + { + newValue = "https://" + newValue; + } + + SystemSettings.ExternalUrl = newValue; + + result.NewData = newValue; + result.ResultCode = AjaxTextBox.CodeSuccess; + break; + default: + throw new NotImplementedException("Unknown cookie in StoreCallback"); + } + + return result; + } + + } + } \ No newline at end of file diff --git a/Utility/Communications/CommsTransmitterMail.cs b/Utility/Communications/CommsTransmitterMail.cs index f2dd5ec4..0e0dadbb 100644 --- a/Utility/Communications/CommsTransmitterMail.cs +++ b/Utility/Communications/CommsTransmitterMail.cs @@ -14,6 +14,7 @@ namespace Swarmops.Utility.Communications // To be implemented better private static string _smtpServerCache = string.Empty; + private static int _smtpPortCache = 25; private static DateTime _cacheReloadTime = DateTime.MinValue; public void Transmit (PayloadEnvelope envelope, Person person) @@ -52,23 +53,26 @@ namespace Swarmops.Utility.Communications mail.BodyEncoding = Encoding.UTF8; string smtpServer = _smtpServerCache; + int smtpPort = _smtpPortCache; DateTime now = DateTime.Now; if (now > _cacheReloadTime) { - smtpServer = _smtpServerCache = Persistence.Key["SmtpServer"]; + smtpServer = SystemSettings.SmtpHost; + smtpPort = SystemSettings.SmtpPort; _cacheReloadTime = now.AddMinutes (5); } if (string.IsNullOrEmpty (smtpServer)) { - smtpServer = "192.168.80.204"; + smtpServer = "localhost"; + smtpPort = 25; // For development use only - invalidate cache instead of this, forcing re-reload _cacheReloadTime = DateTime.MinValue; } - SmtpClient mailClient = new SmtpClient (smtpServer); + SmtpClient mailClient = new SmtpClient (smtpServer, smtpPort); // TODO: SMTP Server login credentials