diff --git a/Logic/Security/Authority.cs b/Logic/Security/Authority.cs index 4913ba2e..6eddf8f3 100644 --- a/Logic/Security/Authority.cs +++ b/Logic/Security/Authority.cs @@ -76,20 +76,48 @@ namespace Swarmops.Logic.Security this._data = data; } - public static Authority FromLogin (Person person) + public Person Person + { + get { return Person.FromIdentity (_data.PersonId); } + } + + public Organization Organization + { + get { return Organization.FromIdentity (_data.OrganizationId); } + } + + public PositionAssignment Assignment + { + get + { + if (_data.PositionAssignmentId == 0) + { + return null; + } + + return PositionAssignment.FromIdentity (_data.PositionAssignmentId); + } + } + + public Position Position + { + get { return Assignment != null? Assignment.Position: null; } + } + + public static Authority FromLogin(Person person) { int lastOrgId = person.LastLogonOrganizationId; PositionAssignment assignment = null; if (lastOrgId != 0) { - Organization organization = Organization.FromIdentity (lastOrgId); - assignment = person.GetPrimaryAssignment (organization); + Organization organization = Organization.FromIdentity(lastOrgId); + assignment = person.GetPrimaryAssignment(organization); } // TODO: Verify membership OR position OR volunteer - return new Authority (new AuthorityData + return new Authority(new AuthorityData { CustomData = new Basic.Types.Common.SerializableDictionary(), LoginDateTimeUtc = DateTime.UtcNow, @@ -99,6 +127,22 @@ namespace Swarmops.Logic.Security }); } + public static Authority FromLogin(Person person, Organization organization) + { + PositionAssignment assignment = person.GetPrimaryAssignment(organization); + + // TODO: Verify membership OR position OR volunteer + + return new Authority(new AuthorityData + { + CustomData = new Basic.Types.Common.SerializableDictionary(), + LoginDateTimeUtc = DateTime.UtcNow, + OrganizationId = organization.Identity, + PersonId = person.Identity, + PositionAssignmentId = (assignment != null ? assignment.Identity : 0) + }); + } + private readonly AuthorityData _data; diff --git a/Logic/Swarm/DashboardTodos.cs b/Logic/Swarm/DashboardTodos.cs index aa01631f..d7ed1ce1 100644 --- a/Logic/Swarm/DashboardTodos.cs +++ b/Logic/Swarm/DashboardTodos.cs @@ -10,6 +10,11 @@ namespace Swarmops.Logic.Swarm { public class DashboardTodos : List { + public static DashboardTodos ForAuthority (Authority authority) + { + return ForPerson (authority.Person, authority.Organization); // TODO: Add actual assigned-position check here + } + public static DashboardTodos ForPerson (Person person, Organization organization) { DashboardTodos result = new DashboardTodos(); diff --git a/Site5/App_Code/CommonV5.cs b/Site5/App_Code/CommonV5.cs index 411cd38d..f60af5aa 100644 --- a/Site5/App_Code/CommonV5.cs +++ b/Site5/App_Code/CommonV5.cs @@ -3,6 +3,7 @@ using System.Drawing; using System.Globalization; using System.Threading; using System.Web; +using Swarmops.Logic.Security; using Swarmops.Logic.Structure; using Swarmops.Logic.Support; using Swarmops.Logic.Swarm; @@ -79,39 +80,15 @@ public class CommonV5 public static AuthenticationData GetAuthenticationDataAndCulture (HttpContext suppliedContext) { - // This function is called from static page methods in AJAX calls to get + // This function is called from all the V5 base classes to have a common + // authentication point for every derived master, page, and control. + + // This function is also called from static page methods in AJAX calls to get // the current set of authentication data. Static page methods cannot access // the instance data of PageV5Base. - AuthenticationData result = new AuthenticationData(); - - // Find various credentials - - string identity = suppliedContext.User.Identity.Name; - - // TODO: If identity is null or empty, set null user + org - - string[] identityTokens = identity.Split (','); - - string userIdentityString = identityTokens[0]; - string organizationIdentityString = identityTokens[1]; - - int currentUserId = Convert.ToInt32 (userIdentityString); - int currentOrganizationId = Convert.ToInt32 (organizationIdentityString); - - result.CurrentUser = Person.FromIdentity (currentUserId); - try - { - result.CurrentOrganization = Organization.FromIdentity (currentOrganizationId); - } - catch (ArgumentException) - { - if (PilotInstallationIds.IsPilot (PilotInstallationIds.DevelopmentSandbox)) - { - // It's possible this organization was deleted. Log on to Sandbox instead. - result.CurrentOrganization = Organization.Sandbox; - } - } + AuthenticationData result = + AuthenticationData.FromAuthority (Authority.FromEncryptedXml (suppliedContext.User.Identity.Name)); CulturePreInit (HttpContext.Current.Request); @@ -120,6 +97,11 @@ public class CommonV5 public static string JavascriptEscape (string input) { + if (String.IsNullOrEmpty (input)) + { + return String.Empty; + } + return System.Uri.EscapeDataString (input).Replace ("'", "%27").Replace ("\"", "%22"); // makes it safe to embed in single/double quotes client-side } @@ -295,6 +277,38 @@ public class CommonV5 } + +public class AuthenticationData +{ + public static AuthenticationData FromAuthority(Authority authority) + { + return new AuthenticationData { Authority = authority }; + } + + public Authority Authority { get; private set; } + + public Organization CurrentOrganization + { + get { return Authority.Organization; } + } + + public Person CurrentUser + { + get { return Authority.Person; } + } +} + + +public class AjaxCallResult +{ + public bool Success { get; set; } + public string DisplayMessage { get; set; } +} + + + + + public enum ColorType { Unkown = 0, diff --git a/Site5/App_Code/ControlV5Base.cs b/Site5/App_Code/ControlV5Base.cs index bb87d941..af82e010 100644 --- a/Site5/App_Code/ControlV5Base.cs +++ b/Site5/App_Code/ControlV5Base.cs @@ -14,17 +14,15 @@ using Swarmops.Logic.Swarm; public class ControlV5Base : UserControl { private Authority _authority; - private Organization _currentOrganization; - private Person _currentUser; protected Organization CurrentOrganization { - get { return this._currentOrganization; } + get { return _authority.Organization; } } protected Person CurrentUser { - get { return this._currentUser; } + get { return _authority.Person; } } protected Authority CurrentAuthority @@ -32,37 +30,11 @@ public class ControlV5Base : UserControl get { return this._authority; } } - protected override void OnLoad (EventArgs e) + protected override void OnInit (EventArgs e) { - throw new NotImplementedException(); // this MUST be replaced with CommonV5 function + this._authority = CommonV5.GetAuthenticationDataAndCulture (HttpContext.Current).Authority; - int currentUserId = 0; - int currentOrganizationId = 0; - - string identity = HttpContext.Current.User.Identity.Name; - string[] identityTokens = identity.Split (','); - - string userIdentityString = identityTokens[0]; - string organizationIdentityString = identityTokens[1]; - - currentUserId = Convert.ToInt32 (userIdentityString); - currentOrganizationId = Convert.ToInt32 (organizationIdentityString); - this._currentUser = Person.FromIdentity (currentUserId); - //this._authority = this._currentUser.GetAuthority(); - try - { - this._currentOrganization = Organization.FromIdentity (currentOrganizationId); - } - catch (ArgumentException) - { - if (PilotInstallationIds.IsPilot (PilotInstallationIds.DevelopmentSandbox)) - { - // It's possible this organization was deleted. Log on to Sandbox instead. - this._currentOrganization = Organization.Sandbox; - } - } - - base.OnLoad (e); + base.OnInit (e); } protected string JavascriptEscape (string input) diff --git a/Site5/App_Code/DataV5Base.cs b/Site5/App_Code/DataV5Base.cs index 6e6ac1fe..9eb2b639 100644 --- a/Site5/App_Code/DataV5Base.cs +++ b/Site5/App_Code/DataV5Base.cs @@ -15,10 +15,33 @@ using Swarmops.Logic.Swarm; public class DataV5Base : Page { public Access PageAccessRequired = null; // v5 mechanism - public PermissionSet pagePermissionDefault = new PermissionSet (Permission.CanSeeSelf); //Use from menu; - protected Person CurrentUser { get; private set; } - protected Organization CurrentOrganization { get; private set; } + protected Person CurrentUser { + get + { + if (CurrentAuthority != null) + { + return CurrentAuthority.Person; + } + + return null; + } + } + + protected Organization CurrentOrganization + { + get + { + if (CurrentAuthority != null) + { + return CurrentAuthority.Organization; + } + + return null; + } + } + + protected Authority CurrentAuthority { get; private set; } /// An that contains the event data. protected override void OnInitComplete (EventArgs e) @@ -29,32 +52,20 @@ public class DataV5Base : Page if (!string.IsNullOrEmpty (identity)) { - // this MUST be replaced with CommonV5 function - throw new NotImplementedException(); - - string[] identityTokens = identity.Split (','); - - string userIdentityString = identityTokens[0]; - string organizationIdentityString = identityTokens[1]; - - CurrentUser = Person.FromIdentity (Int32.Parse (userIdentityString)); try { - CurrentOrganization = Organization.FromIdentity (Int32.Parse (organizationIdentityString)); + CurrentAuthority = Authority.FromEncryptedXml(identity); } - catch (ArgumentException) + catch (Exception) { - if (PilotInstallationIds.IsPilot (PilotInstallationIds.DevelopmentSandbox)) - { - // It's possible this organization was deleted. Log on to Sandbox instead. - CurrentOrganization = Organization.Sandbox; - } + // if this fails FOR ANY REASON, we're unauthenticated + + CurrentAuthority = null; } } else { - CurrentUser = null; // unauthenticated! - CurrentOrganization = null; // unauthenticated! + CurrentAuthority = null; // unauthenticated! } } diff --git a/Site5/App_Code/MasterV5Base.cs b/Site5/App_Code/MasterV5Base.cs index 6dbbb9d3..1b0505a8 100644 --- a/Site5/App_Code/MasterV5Base.cs +++ b/Site5/App_Code/MasterV5Base.cs @@ -18,9 +18,7 @@ public class MasterV5Base : MasterPage public string CurrentPageTitle = string.Empty; // ReSharper disable InconsistentNaming - protected Authority _authority = null; - protected Organization _currentOrganization = null; - protected Person _currentUser = null; // These are set in Master-v5.master.cs + protected Authority _authority = null; // This is set in Master-v5.master.cs public EasyUIControl EasyUIControlsUsed { get; set; } // these are set by each page, and called by Master to render in ExternalScripts control @@ -49,12 +47,12 @@ public class MasterV5Base : MasterPage public Person CurrentUser { - get { return this._currentUser; } + get { return this._authority.Person; } } public Organization CurrentOrganization { - get { return this._currentOrganization; } + get { return this._authority.Organization; } } public Authority CurrentAuthority diff --git a/Site5/App_Code/PageV5Base.cs b/Site5/App_Code/PageV5Base.cs index d293133f..a9d296c5 100644 --- a/Site5/App_Code/PageV5Base.cs +++ b/Site5/App_Code/PageV5Base.cs @@ -8,7 +8,7 @@ using Swarmops.Logic.Security; using Swarmops.Logic.Structure; using Swarmops.Logic.Support; using Swarmops.Logic.Swarm; -using PermissionSet = Swarmops.Logic.Security.PermissionSet; + /// /// Base class to use for all data generators (JSON, etc). It supplies identification and localization. @@ -17,7 +17,6 @@ public class PageV5Base : Page { public int DbVersionRequired = 0; // v5 mechanism public Access PageAccessRequired = null; // v5 mechanism - public PermissionSet pagePermissionDefault = new PermissionSet (Permission.CanSeeSelf); //Use from menu; protected new MasterV5Base Master { @@ -71,12 +70,12 @@ public class PageV5Base : Page } /// - /// This is used to identify special cases for pilot installations. Because of the privacy implications, it - /// should not be used at all from general availability onwards, except for those pilot installations. + /// This is used to identify special cases for pilot installations. It is also used as an installation-unique + /// encryption key salt in addition to record-specific salting. /// protected string InstallationId { - get { return Persistence.Key["SwarmopsInstallationId"]; } + get { return SystemSettings.InstallationId; } } /// An that contains the event data. @@ -176,15 +175,3 @@ public class PageV5Base : Page } -public class AuthenticationData -{ - public Organization CurrentOrganization; - public Person CurrentUser; -} - - -public class AjaxCallResult -{ - public bool Success { get; set; } - public string DisplayMessage { get; set; } -} diff --git a/Site5/Master-v5.master.cs b/Site5/Master-v5.master.cs index 984bea5c..23bb736f 100644 --- a/Site5/Master-v5.master.cs +++ b/Site5/Master-v5.master.cs @@ -22,36 +22,9 @@ namespace Swarmops { protected void Page_Init (object sender, EventArgs e) { - // Security stuff - // Current authentication - - // this MUST be replaced with CommonV5 function - throw new NotImplementedException(); - - string identity = HttpContext.Current.User.Identity.Name; - string[] identityTokens = identity.Split (','); - - string userIdentityString = identityTokens[0]; - string organizationIdentityString = identityTokens[1]; - - int currentUserId = Convert.ToInt32 (userIdentityString); - int currentOrganizationId = Convert.ToInt32 (organizationIdentityString); - - _currentUser = Person.FromIdentity (currentUserId); - //_authority = _currentUser.GetAuthority(); - try - { - _currentOrganization = Organization.FromIdentity (currentOrganizationId); - } - catch (ArgumentException) - { - if (PilotInstallationIds.IsPilot (PilotInstallationIds.DevelopmentSandbox)) - { - // It's possible this organization was deleted. Log on to Sandbox instead. - _currentOrganization = Organization.Sandbox; - } - } - + this._authority = + AuthenticationData.FromAuthority (Authority.FromEncryptedXml (HttpContext.Current.User.Identity.Name)) + .Authority; } protected void Page_Load (object sender, EventArgs e) @@ -114,8 +87,8 @@ namespace Swarmops } _cacheVersionMark = SHA1.Hash (_cacheVersionMark).Replace(" ", "").Substring (0, 8); - this.LabelCurrentUserName.Text = _currentUser.Name; - this.LabelCurrentOrganizationName.Text = _currentOrganization.Name; + this.LabelCurrentUserName.Text = CurrentAuthority.Person.Name; + this.LabelCurrentOrganizationName.Text = CurrentAuthority.Organization.Name; this.LabelActionPlaceholder1.Text = "Action shortcut 1 (TODO)"; this.LabelActionPlaceholder2.Text = "Action shortcut 2 (TODO)"; @@ -123,7 +96,7 @@ namespace Swarmops // Set up todo items - DashboardTodos todos = DashboardTodos.ForPerson (_currentUser, _currentOrganization); + DashboardTodos todos = DashboardTodos.ForAuthority (CurrentAuthority); this.RepeaterTodoItems.DataSource = todos; this.RepeaterTodoItems.DataBind(); diff --git a/Site5/Security/Login.aspx.cs b/Site5/Security/Login.aspx.cs index 7bf2a6fd..a590f695 100644 --- a/Site5/Security/Login.aspx.cs +++ b/Site5/Security/Login.aspx.cs @@ -81,7 +81,7 @@ namespace Swarmops.Pages.Security Request.QueryString["SuppressAutologin"] != "true") { DashboardMessage.Set ("

You have been logged on as Sandbox Administrator to the Swarmops Development Sandbox.


This machine runs the latest development build, so you may run into diagnostic code and half-finished features. All data here is bogus test data and is reset every night.


In other words, welcome, and play away!

"); - FormsAuthentication.SetAuthCookie ("1,1", true); + FormsAuthentication.SetAuthCookie (Authority.FromLogin (Person.FromIdentity (1), Organization.Sandbox).ToEncryptedXml(), true); Response.Redirect ("/"); } @@ -92,7 +92,7 @@ namespace Swarmops.Pages.Security if (organizationOpenLedgers != null) { DashboardMessage.Set (String.Format(Resources.Pages.Security.Login_AsOpenLedgers, organizationOpenLedgers.Name)); - FormsAuthentication.SetAuthCookie(Person.OpenLedgersIdentity.ToString() + "," + organizationOpenLedgers.Identity.ToString(), true); + FormsAuthentication.SetAuthCookie(Authority.FromLogin (Person.FromIdentity (Person.OpenLedgersIdentity), organizationOpenLedgers).ToEncryptedXml(), true); Response.Redirect (@"/Ledgers/BalanceSheet"); } @@ -275,11 +275,11 @@ namespace Swarmops.Pages.Security { // If the person doesn't have access to the last organization (anymore), log on to Sandbox - lastOrgId = 1; + lastOrgId = Organization.SandboxIdentity; } GuidCache.Set (logonUri + "-LoggedOn", - authenticatedPerson.Identity.ToString (CultureInfo.InvariantCulture) + "," + lastOrgId.ToString(CultureInfo.InvariantCulture) + ",,AuthPlain"); + Authority.FromLogin (authenticatedPerson, Organization.FromIdentity (lastOrgId)).ToEncryptedXml()); return "Success"; // Prepare here for "2FARequired" return code }