Added System Settings

This commit is contained in:
Rick Falkvinge
2015-01-24 21:19:31 +01:00
parent 96a94cda7e
commit e4ab890b07
6 changed files with 133 additions and 16 deletions
@@ -197,6 +197,12 @@
<data name="SystemSettings_ExternalUrl" xml:space="preserve">
<value>Externally Accessible URL</value>
</data>
<data name="SystemSettings_Info" xml:space="preserve">
<value>Various technical settings for this Swarmops installation. Not specific to any organization or person, but installation-wide.</value>
</data>
<data name="SystemSettings_PageTitle" xml:space="preserve">
<value>System Settings</value>
</data>
<data name="SystemSettings_SmtpServer" xml:space="preserve">
<value>SMTP Server (user:pass@host:port)</value>
</data>
+3 -3
View File
@@ -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) {
+4 -2
View File
@@ -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
+4 -2
View File
@@ -19,10 +19,12 @@
<div title="<img src='/Images/Icons/iconshock-mail-open-64px.png' />">
<h2>Correspondence transmission settings</h2>
<div class="entryFields">
<Swarmops5:AjaxTextBox runat="server" ID="TextSmtpServer" Cookie="Smtp" AjaxCallbackUrl="/Pages/v5/Admin/Settings.aspx" />&#8203;<br/>
<Swarmops5:AjaxTextBox runat="server" ID="TextSmtpServer" Cookie="Smtp" AjaxCallbackUrl="/Pages/v5/Admin/Settings.aspx/StoreCallback" Placeholder="localhost:587" />&#8203;<br/>
<Swarmops5:AjaxTextBox runat="server" ID="TextExternalUrl" Cookie="ExtUrl" AjaxCallbackUrl="/Pages/v5/Admin/Settings.aspx/StoreCallback" Placeholder="https://swarmops.example.com/" />&#8203;<br/>
</div>
<div class="entryLabels">
SMTP server (user:pass@host:port)<br/>
<asp:Label ID="LabelSmtpServer" runat="server" /><br/>
<asp:Label ID="LabelExternalUrl" runat="server" /><br />
</div>
</div>
</div>
+109 -6
View File
@@ -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, "((?<user>[a-z0-9_]+)(:(?<pass>[^@]+))?@)?(?<host>[a-z0-9_\\-\\.]+)(:(?<port>[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;
}
}
}
@@ -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