feature: email service and configuration
This commit is contained in:
@@ -60,6 +60,7 @@ namespace Disco.Services.Authorization
|
||||
{ "Config.Plugin.Show", new Tuple<Func<RoleClaims, bool>, Action<RoleClaims, bool>, string, string, bool>(c => c.Config.Plugin.Show, (c, v) => c.Config.Plugin.Show = v, "Show Plugins", "Can show plugins", false) },
|
||||
{ "Config.Plugin.Uninstall", new Tuple<Func<RoleClaims, bool>, Action<RoleClaims, bool>, string, string, bool>(c => c.Config.Plugin.Uninstall, (c, v) => c.Config.Plugin.Uninstall = v, "Uninstall Plugins", "Can uninstall plugins", false) },
|
||||
{ "Config.System.ConfigureActiveDirectory", new Tuple<Func<RoleClaims, bool>, Action<RoleClaims, bool>, string, string, bool>(c => c.Config.System.ConfigureActiveDirectory, (c, v) => c.Config.System.ConfigureActiveDirectory = v, "Configure Active Directory Settings", "Can configure the Active Directory interoperability settings", false) },
|
||||
{ "Config.System.ConfigureEmail", new Tuple<Func<RoleClaims, bool>, Action<RoleClaims, bool>, string, string, bool>(c => c.Config.System.ConfigureEmail, (c, v) => c.Config.System.ConfigureEmail = v, "Configure Email Settings", "Can configure the email settings", false) },
|
||||
{ "Config.System.ConfigureProxy", new Tuple<Func<RoleClaims, bool>, Action<RoleClaims, bool>, string, string, bool>(c => c.Config.System.ConfigureProxy, (c, v) => c.Config.System.ConfigureProxy = v, "Configure Proxy Settings", "Can configure the proxy settings", false) },
|
||||
{ "Config.System.Show", new Tuple<Func<RoleClaims, bool>, Action<RoleClaims, bool>, string, string, bool>(c => c.Config.System.Show, (c, v) => c.Config.System.Show = v, "Show System Configuration", "Can show the system configuration", false) },
|
||||
{ "Config.Organisation.ConfigureAddresses", new Tuple<Func<RoleClaims, bool>, Action<RoleClaims, bool>, string, string, bool>(c => c.Config.Organisation.ConfigureAddresses, (c, v) => c.Config.Organisation.ConfigureAddresses = v, "Configure Addresses", "Can configure organisation addresses", false) },
|
||||
@@ -297,6 +298,7 @@ namespace Disco.Services.Authorization
|
||||
}),
|
||||
new ClaimNavigatorItem("Config.System", "System", "Permissions related to System Configuration", false, new List<IClaimNavigatorItem>() {
|
||||
new ClaimNavigatorItem("Config.System.ConfigureActiveDirectory", false),
|
||||
new ClaimNavigatorItem("Config.System.ConfigureEmail", false),
|
||||
new ClaimNavigatorItem("Config.System.ConfigureProxy", false),
|
||||
new ClaimNavigatorItem("Config.System.Show", false)
|
||||
}),
|
||||
@@ -574,6 +576,7 @@ namespace Disco.Services.Authorization
|
||||
c.Config.Plugin.Show = true;
|
||||
c.Config.Plugin.Uninstall = true;
|
||||
c.Config.System.ConfigureActiveDirectory = true;
|
||||
c.Config.System.ConfigureEmail = true;
|
||||
c.Config.System.ConfigureProxy = true;
|
||||
c.Config.System.Show = true;
|
||||
c.Config.Organisation.ConfigureAddresses = true;
|
||||
@@ -993,6 +996,11 @@ namespace Disco.Services.Authorization
|
||||
/// </summary>
|
||||
public const string ConfigureActiveDirectory = "Config.System.ConfigureActiveDirectory";
|
||||
|
||||
/// <summary>Configure Email Settings
|
||||
/// <para>Can configure the email settings</para>
|
||||
/// </summary>
|
||||
public const string ConfigureEmail = "Config.System.ConfigureEmail";
|
||||
|
||||
/// <summary>Configure Proxy Settings
|
||||
/// <para>Can configure the proxy settings</para>
|
||||
/// </summary>
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
|
||||
[ClaimDetails("Configure Proxy Settings", "Can configure the proxy settings")]
|
||||
public bool ConfigureProxy { get; set; }
|
||||
|
||||
[ClaimDetails("Configure Email Settings", "Can configure the email settings")]
|
||||
public bool ConfigureEmail { get; set; }
|
||||
|
||||
[ClaimDetails("Configure Active Directory Settings", "Can configure the Active Directory interoperability settings")]
|
||||
public bool ConfigureActiveDirectory { get; set; }
|
||||
|
||||
@@ -390,6 +390,7 @@
|
||||
<Compile Include="Logging\Persistance\LogPersistContext.cs" />
|
||||
<Compile Include="Logging\Persistance\LogPersistContextInitializer.cs" />
|
||||
<Compile Include="Logging\Utilities.cs" />
|
||||
<Compile Include="Messaging\EmailService.cs" />
|
||||
<Compile Include="Plugins\Features\CertificateAuthorityProvider\CertificateAuthorityProviderFeature.cs" />
|
||||
<Compile Include="Plugins\Features\CertificateProvider\ProvisionPersonalCertificateResult.cs" />
|
||||
<Compile Include="Plugins\Features\CertificateAuthorityProvider\ProvisionAuthorityCertificatesResult.cs" />
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
using Disco.Data.Configuration;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Services.Messaging;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace Disco.Services.Messaging
|
||||
{
|
||||
public static class EmailService
|
||||
{
|
||||
private static string smtpServer;
|
||||
private static int smtpPort;
|
||||
private static string smtpFromAddress;
|
||||
private static bool smtpEnableSsl;
|
||||
private static string smtpUsername;
|
||||
private static string smtpPassword;
|
||||
|
||||
public static bool IsConfigured { get; private set; }
|
||||
|
||||
static EmailService()
|
||||
{
|
||||
using (var database = new DiscoDataContext())
|
||||
{
|
||||
Update(database.DiscoConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ValidateConfiguration(string smtpServer, int smtpPort, string fromAddress, bool enableSsl, string username, string password)
|
||||
{
|
||||
// if smtpServer is null, we aren't configured (emailing is disabled)
|
||||
if (!string.IsNullOrWhiteSpace(smtpServer))
|
||||
{
|
||||
// validate
|
||||
if (smtpPort <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(smtpPort), "Invalid SMTP port specified");
|
||||
if (string.IsNullOrWhiteSpace(fromAddress))
|
||||
throw new ArgumentOutOfRangeException(nameof(fromAddress), "From Address is required");
|
||||
// try parse FromAddress
|
||||
new MailAddress(fromAddress);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Update(SystemConfiguration systemConfiguration)
|
||||
{
|
||||
smtpServer = systemConfiguration.EmailSmtpServer;
|
||||
smtpPort = systemConfiguration.EmailSmtpPort;
|
||||
smtpEnableSsl = systemConfiguration.EmailEnableSsl;
|
||||
smtpFromAddress = systemConfiguration.EmailFromAddress;
|
||||
smtpUsername = systemConfiguration.EmailUsername;
|
||||
smtpPassword = systemConfiguration.EmailPassword;
|
||||
|
||||
IsConfigured =
|
||||
!string.IsNullOrWhiteSpace(smtpServer) &&
|
||||
smtpPort > 0 &&
|
||||
!string.IsNullOrWhiteSpace(smtpFromAddress);
|
||||
}
|
||||
|
||||
public static void SendEmail(Email email)
|
||||
{
|
||||
if (!IsConfigured)
|
||||
throw new InvalidOperationException("Unable to send email, the email service has not been configured.");
|
||||
|
||||
var message = new MailMessage()
|
||||
{
|
||||
Subject = email.Subject,
|
||||
IsBodyHtml = email.IsBodyHtml,
|
||||
Body = email.Body,
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(email.From))
|
||||
message.From = new MailAddress(email.From);
|
||||
else
|
||||
message.From = new MailAddress(smtpFromAddress);
|
||||
|
||||
if (email.To.Count > 0)
|
||||
{
|
||||
foreach (var recipient in email.To)
|
||||
message.To.Add(recipient);
|
||||
}
|
||||
if (email.CC.Count > 0)
|
||||
{
|
||||
foreach (var recipient in email.CC)
|
||||
message.CC.Add(recipient);
|
||||
}
|
||||
if (email.BCC.Count > 0)
|
||||
{
|
||||
foreach (var recipient in email.BCC)
|
||||
message.Bcc.Add(recipient);
|
||||
}
|
||||
|
||||
using (var smtpClient = new SmtpClient(smtpServer, smtpPort))
|
||||
{
|
||||
smtpClient.EnableSsl = smtpEnableSsl;
|
||||
if (!string.IsNullOrWhiteSpace(smtpUsername))
|
||||
smtpClient.Credentials = new NetworkCredential(smtpUsername, smtpPassword);
|
||||
|
||||
smtpClient.Send(message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SendTestEmail(string recipient)
|
||||
{
|
||||
var email = new Email(recipient, "Disco ICT Test Email", @"Disco ICT has successfully been configured to send to this recipient.");
|
||||
SendEmail(email);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user