From 3b29ba7c7d8c79928b13f69a1233d076dfbac400 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 8 Dec 2014 00:34:49 +0100 Subject: [PATCH] Work on Add Person - checkpoint commit to Sandbox Backend --- ...nsmission.DefaultCommTemplates.Designer.cs | 91 ++++++++++++ ...ons.Transmission.DefaultCommTemplates.resx | 137 ++++++++++++++++++ Logic/Communications/ICommsRenderer.cs | 2 +- Logic/Communications/OutboundComm.cs | 22 +++ .../Transmission/ParticipantMailPayload.cs | 122 ++++++++++++++++ Logic/Logic.csproj | 10 ++ Site5/Controls/v5/Base/MainMenu.ascx.cs | 2 - Site5/Pages/v5/Swarm/AddPerson.aspx.cs | 26 +++- Site5/Security/Login.aspx | 2 - Site5/Security/Login.aspx.cs | 1 - .../Communications/CommsTransmitterMail.cs | 20 +-- 11 files changed, 419 insertions(+), 16 deletions(-) create mode 100644 Logic/App_GlobalResources/Logic.Communications.Transmission.DefaultCommTemplates.Designer.cs create mode 100644 Logic/App_GlobalResources/Logic.Communications.Transmission.DefaultCommTemplates.resx create mode 100644 Logic/Communications/Transmission/ParticipantMailPayload.cs diff --git a/Logic/App_GlobalResources/Logic.Communications.Transmission.DefaultCommTemplates.Designer.cs b/Logic/App_GlobalResources/Logic.Communications.Transmission.DefaultCommTemplates.Designer.cs new file mode 100644 index 00000000..d5cad0a2 --- /dev/null +++ b/Logic/App_GlobalResources/Logic.Communications.Transmission.DefaultCommTemplates.Designer.cs @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.18444 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Swarmops.Logic.App_GlobalResources { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Logic_Communications_Transmission_DefaultCommTemplates { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Logic_Communications_Transmission_DefaultCommTemplates() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Swarmops.Logic.App_GlobalResources.Logic.Communications.Transmission.DefaultCommT" + + "emplates", typeof(Logic_Communications_Transmission_DefaultCommTemplates).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Welcome to [OrganizationName]! + /// + ///You have been granted a [Regularship] by [ActingPerson]. It expires on [MembershipExpiry], so there's plenty of time to get acquainted with the organization. + /// + ///You should already have been contacted by people from [OrganizationName] for more information on how to get active. If not, then expect correspondence soon. + /// + ///Your initial and randomly generated password to Swarmops, the software used as a back-end system, is as follows: + ///[RandomPassword] + /// + ///(Unfortunately, you ca [rest of string was truncated]";. + /// + internal static string ParticipantManualAddWelcome_Body { + get { + return ResourceManager.GetString("ParticipantManualAddWelcome_Body", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to [[OrganizationName]] Welcome to [OrganizationName]!. + /// + internal static string ParticipantManualAddWelcome_Subject { + get { + return ResourceManager.GetString("ParticipantManualAddWelcome_Subject", resourceCulture); + } + } + } +} diff --git a/Logic/App_GlobalResources/Logic.Communications.Transmission.DefaultCommTemplates.resx b/Logic/App_GlobalResources/Logic.Communications.Transmission.DefaultCommTemplates.resx new file mode 100644 index 00000000..6ffbf501 --- /dev/null +++ b/Logic/App_GlobalResources/Logic.Communications.Transmission.DefaultCommTemplates.resx @@ -0,0 +1,137 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Welcome to [OrganizationName]! + +You have been granted a [Regularship] by [ActingPerson]. It expires on [MembershipExpiry], so there's plenty of time to get acquainted with the organization. + +You should already have been contacted by people from [OrganizationName] for more information on how to get active. If not, then expect correspondence soon. + +Your initial and randomly generated password to Swarmops, the software used as a back-end system, is as follows: +[RandomPassword] + +(Unfortunately, you can't change it yet. That functionality is expected early 2015. Swarmops is still in an early alpha; that's one of the drawbacks of pioneering new software: things can be a little half-baked as they come online.) + +Welcome aboard! + + + [[OrganizationName]] Welcome to [OrganizationName]! + + \ No newline at end of file diff --git a/Logic/Communications/ICommsRenderer.cs b/Logic/Communications/ICommsRenderer.cs index 461aeea5..48bd54e8 100644 --- a/Logic/Communications/ICommsRenderer.cs +++ b/Logic/Communications/ICommsRenderer.cs @@ -11,7 +11,7 @@ namespace Swarmops.Logic.Communications RenderedComm RenderComm (Person person); } - public class RenderedComm : Dictionary + public class RenderedComm : Dictionary { // typeset for readability } diff --git a/Logic/Communications/OutboundComm.cs b/Logic/Communications/OutboundComm.cs index 51c2a405..f82ab185 100644 --- a/Logic/Communications/OutboundComm.cs +++ b/Logic/Communications/OutboundComm.cs @@ -112,6 +112,28 @@ namespace Swarmops.Logic.Communications } + public static OutboundComm CreateMembershipLetter (ParticipantMailType mailType, Membership membership, + Person actingPerson) + { + List recipients = People.FromSingle (membership.Person); + + OutboundComm comm = OutboundComm.Create (null, null, membership.Organization, + CommResolverClass.Unknown, null, + CommTransmitterClass.CommsTransmitterMail, + new ParticipantMailPayload (mailType, membership, actingPerson).ToXml(), + OutboundCommPriority.Low); + + foreach (Person person in recipients) + { + comm.AddRecipient(person); + } + + comm.Resolved = true; + + return comm; + } + + public static OutboundComm CreateNotification (Organization organization, string notificationResourceString) { List recipients = People.FromSingle (Person.FromIdentity (1)); // Initial Admin recipient diff --git a/Logic/Communications/Transmission/ParticipantMailPayload.cs b/Logic/Communications/Transmission/ParticipantMailPayload.cs new file mode 100644 index 00000000..b498d987 --- /dev/null +++ b/Logic/Communications/Transmission/ParticipantMailPayload.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using MySql.Data.MySqlClient.Properties; +using Swarmops.Logic.Security; +using Swarmops.Logic.Swarm; + +namespace Swarmops.Logic.Communications.Transmission +{ + [Serializable] + public class ParticipantMailPayload: PayloadBase, ICommsRenderer + { + [Obsolete("Do not call this constructor - doing so will generate this compile-time error. For serialization only.", true)] + public ParticipantMailPayload() + { + // do not use - for serialization only + } + + public SerializableDictionary Strings{ get; set; } + public string SubjectTemplate { get; set; } + public string BodyTemplate { get; set; } + + + public ParticipantMailPayload (ParticipantMailType mailType, Membership membership, Person actingPerson) + { + Strings = new SerializableDictionary(); + + switch (mailType) + { + case ParticipantMailType.MemberAddedWelcome: + Strings[MailPayloadString.ActingPerson] = actingPerson.Name; + Strings[MailPayloadString.MembershipExpiry] = membership.Expires.ToLongDateString(); + Strings[MailPayloadString.OrganizationName] = membership.Organization.Name; + Strings[MailPayloadString.Regularship] = + Participant.Localized (membership.Organization.RegularLabel, TitleVariant.Ship); + + BodyTemplate = + App_GlobalResources.Logic_Communications_Transmission_DefaultCommTemplates + .ParticipantManualAddWelcome_Body; + + SubjectTemplate = + App_GlobalResources.Logic_Communications_Transmission_DefaultCommTemplates + .ParticipantManualAddWelcome_Subject; + + break; + default: + throw new NotImplementedException("Unknown mailType: " + mailType.ToString()); + } + } + + public RenderedComm RenderComm (Person person) + { + string body = BodyTemplate; + string subject = SubjectTemplate; + + // If the template contains "[RandomPassword]", set the person's pwd to random and replace macro + + if (body.Contains ("[RandomPassword]")) + { + string randomPassword = Authentication.CreateRandomPassword (24); + person.SetPassword (randomPassword); + body = body.Replace ("[RandomPassword]", randomPassword); + } + + // Replace each and every marker in template from lookup table + + foreach ( + MailPayloadString replacementString in (MailPayloadString[])Enum.GetValues(typeof(MailPayloadString)) + ) + { + if (Strings.ContainsKey (replacementString)) + { + body = body.Replace ("[" + replacementString.ToString() + "]", + Strings[replacementString]); + subject = subject.Replace ("[" + replacementString.ToString() + "]", + Strings[replacementString]); + } + } + + RenderedComm result = new RenderedComm(); + result[CommRenderPart.BodyText] = body; + result[CommRenderPart.Subject] = subject; + result[CommRenderPart.SenderMail] = "admin@swarmops.com"; // TODO: FIX FIX FIX HACK + result[CommRenderPart.SenderName] = "Swarmops Administrative"; + + return result; + } + } + + + public enum OrganizationMailSender + { + Unknown = 0, + HumanResources, + Accounting, + OrganizationName, + SystemNotifier, + HighestExecutive + } + + public enum ParticipantMailType + { + Unknown = 0, + MemberAddedWelcome + } + + [Serializable] + public enum MailPayloadString + { + // will likely need much more here + + Unknown = 0, + MembershipExpiry, + Regularship, + ActingPerson, + OrganizationName + } + +} diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index a6d7b008..f4fa70bc 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -70,6 +70,11 @@ + + True + True + Logic.Communications.Transmission.DefaultCommTemplates.resx + True True @@ -104,6 +109,7 @@ + @@ -369,6 +375,10 @@ + + ResXFileCodeGenerator + Logic.Communications.Transmission.DefaultCommTemplates.Designer.cs + ResXFileCodeGenerator Logic.Financial.FinancialTransactionTagSetTypesStock.Designer.cs diff --git a/Site5/Controls/v5/Base/MainMenu.ascx.cs b/Site5/Controls/v5/Base/MainMenu.ascx.cs index 354ad078..019d3d4d 100644 --- a/Site5/Controls/v5/Base/MainMenu.ascx.cs +++ b/Site5/Controls/v5/Base/MainMenu.ascx.cs @@ -80,8 +80,6 @@ namespace Swarmops.Controls.Base { // TODO: More types here, and check with the CSS. Some work to get good looking - // MEH forcing build - case MenuItemType.Link: output.Write ( "{2}", diff --git a/Site5/Pages/v5/Swarm/AddPerson.aspx.cs b/Site5/Pages/v5/Swarm/AddPerson.aspx.cs index a07a7e71..94acb4b5 100644 --- a/Site5/Pages/v5/Swarm/AddPerson.aspx.cs +++ b/Site5/Pages/v5/Swarm/AddPerson.aspx.cs @@ -1,8 +1,12 @@ using System; +using System.Reflection; using System.Runtime.Versioning; using System.Web.UI; using System.Web.UI.WebControls; using Resources; +using Swarmops.Basic.Enums; +using Swarmops.Logic.Communications; +using Swarmops.Logic.Communications.Transmission; using Swarmops.Logic.Security; using Swarmops.Logic.Structure; using Swarmops.Logic.Swarm; @@ -80,6 +84,7 @@ namespace Swarmops.Frontend.Pages.v5.Swarm this.LabelStreet2.Text = Resources.Pages.Swarm.AddPerson_Street2; this.LabelPostalCode.Text = Resources.Global.Global_PostalCode; this.LabelCity.Text = Resources.Global.Global_City; + this.LabelGeographyDetected.Text = Resources.Pages.Swarm.AddPerson_GeographyDetected; this.LabelHeaderStatData.Text = Resources.Pages.Swarm.AddPerson_StatisticalData; this.LabelDateOfBirth.Text = Resources.Global.Global_DateOfBirth; this.LabelLegalGender.Text = Resources.Pages.Swarm.AddPerson_LegalGender; @@ -95,8 +100,27 @@ namespace Swarmops.Frontend.Pages.v5.Swarm protected void ButtonSubmit_Click (object sender, EventArgs e) { - string randomPassword = Authentication.CreateRandomPassword (24); + /* + Person newPerson = Person.Create (this.TextName.Text, this.TextMail.Text, string.Empty, this.TextPhone.Text, + this.TextStreet1.Text + this.TextStreet2.Text, + this.TextPostal.Text, this.TextCity.Text, this.DropCountries.SelectedValue, DateTime.UtcNow, + PersonGender.Unknown); + Membership newMembership = Membership.Create (newPerson, CurrentOrganization, DateTime.Today.AddYears (1)); + + PayloadEnvelope envelope = new PayloadEnvelope(); + ParticipantMailPayload payload = new ParticipantMailPayload(ParticipantMailType.MemberAddedWelcome, newMembership, CurrentUser); + envelope.PayloadXml = payload.ToXml(); + envelope.PayloadClass = payload.GetType().ToString(); + + Assembly assembly = typeof (PayloadEnvelope).Assembly; + + Type payloadType = assembly.GetType (envelope.PayloadClass); + var methodInfo = payloadType.GetMethod("FromXml", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); + + ICommsRenderer renderer = (ICommsRenderer)(methodInfo.Invoke (null, new object[] { envelope.PayloadXml })); + + */ // Person.Create() diff --git a/Site5/Security/Login.aspx b/Site5/Security/Login.aspx index 3ccfada6..3a9597c3 100644 --- a/Site5/Security/Login.aspx +++ b/Site5/Security/Login.aspx @@ -144,8 +144,6 @@
-

-
diff --git a/Site5/Security/Login.aspx.cs b/Site5/Security/Login.aspx.cs index ebf566af..660ba560 100644 --- a/Site5/Security/Login.aspx.cs +++ b/Site5/Security/Login.aspx.cs @@ -271,7 +271,6 @@ namespace Swarmops.Pages.Security { this.LabelCurrentOrganizationName.Text = Global.Global_Organization; this.LabelCurrentUserName.Text = Participant.Localized (ParticipantTitle.Person, TitleVariant.Generic); - this.LabelPageTitle.Text = Resources.Pages.Security.Login_PageTitle; this.LabelPreferences.Text = Global.Global_NA; this.LabelSidebarInfoHeader.Text = Global.Sidebar_Information; this.LabelSidebarHelpHeader.Text = Global.Sidebar_Help; diff --git a/Utility/Communications/CommsTransmitterMail.cs b/Utility/Communications/CommsTransmitterMail.cs index 6b4af4f4..f2dd5ec4 100644 --- a/Utility/Communications/CommsTransmitterMail.cs +++ b/Utility/Communications/CommsTransmitterMail.cs @@ -1,5 +1,6 @@ using System; using System.Net.Mail; +using System.Reflection; using System.Text; using Swarmops.Logic.Communications; using Swarmops.Logic.Communications.Transmission; @@ -10,20 +11,21 @@ namespace Swarmops.Utility.Communications { public class CommsTransmitterMail : ICommsTransmitter { - // To be implemented + // To be implemented better private static string _smtpServerCache = string.Empty; private static DateTime _cacheReloadTime = DateTime.MinValue; public void Transmit (PayloadEnvelope envelope, Person person) { - if (envelope.PayloadClass != "Swarmops.Logic.Communications.Transmission.NotificationPayload") - { - throw new NotImplementedException(); - } + // Create the renderer via reflection of the static FromXml method - ICommsRenderer renderer = NotificationPayload.FromXml (envelope.PayloadXml); + Assembly assembly = typeof(PayloadEnvelope).Assembly; + Type payloadType = assembly.GetType(envelope.PayloadClass); + var methodInfo = payloadType.GetMethod("FromXml", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); + + ICommsRenderer renderer = (ICommsRenderer)(methodInfo.Invoke(null, new object[] { envelope.PayloadXml })); RenderedComm comm = renderer.RenderComm (person); MailMessage mail = new MailMessage(); @@ -32,7 +34,7 @@ namespace Swarmops.Utility.Communications try { - mail.From = new MailAddress (comm[CommRenderPart.SenderMail], comm[CommRenderPart.SenderName], + mail.From = new MailAddress ((string) comm[CommRenderPart.SenderMail], (string) comm[CommRenderPart.SenderName], Encoding.UTF8); mail.To.Add (new MailAddress (person.Mail, person.Name)); } @@ -44,8 +46,8 @@ namespace Swarmops.Utility.Communications throw new OutboundCommTransmitException ("Cannot send mail to " + person.Mail, e); } - mail.Subject = comm[CommRenderPart.Subject]; - mail.Body = comm[CommRenderPart.BodyText]; + mail.Subject = (string) comm[CommRenderPart.Subject]; + mail.Body = (string) comm[CommRenderPart.BodyText]; mail.SubjectEncoding = Encoding.UTF8; mail.BodyEncoding = Encoding.UTF8;