From 9a3f70f32157faba1cfd5bf7b1ea4e46a3a2099f Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 18 Apr 2015 14:25:19 +0200 Subject: [PATCH] Restored organization switching --- Logic/Security/Authority.cs | 32 +++++++++++++++++++ .../Security/SetCurrentOrganization.aspx.cs | 27 ++++++---------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/Logic/Security/Authority.cs b/Logic/Security/Authority.cs index 29c43d31..a0bc98d4 100644 --- a/Logic/Security/Authority.cs +++ b/Logic/Security/Authority.cs @@ -86,6 +86,38 @@ namespace Swarmops.Logic.Security get { return Organization.FromIdentity (_data.OrganizationId); } } + public void SetOrganization (Organization organization) + { + // This changes the active organization. There may be a Primary Position in this + // organization; if so, activate that too. Otherwise, just null out the Position. + + // The reason this isn't an ordinary setter for the field is to minimize risk for + // miscoding in the security framework. + + PositionAssignment primaryAssignment = Person.GetPrimaryAssignment (organization); + + _data.OrganizationId = organization.Identity; + _data.PositionAssignmentId = (primaryAssignment == null ? 0 : primaryAssignment.Identity); + } + + public void SetPosition (PositionAssignment assignment) + { + // This changes to a Position. The Organization will change along with it, but only + // if it's not a system-level Position. + + // The reason this isn't an ordinary setter for the field is to minimize risk for + // miscoding in the security framework. + + _data.PositionAssignmentId = (assignment == null ? 0 : assignment.Identity); + if (assignment != null) + { + if (assignment.Position.PositionLevel != PositionLevel.Systemwide) + { + _data.OrganizationId = assignment.Position.OrganizationId; + } + } + } + public PositionAssignment Assignment { get diff --git a/Site5/Pages/v5/Security/SetCurrentOrganization.aspx.cs b/Site5/Pages/v5/Security/SetCurrentOrganization.aspx.cs index b0d50e53..e2161922 100644 --- a/Site5/Pages/v5/Security/SetCurrentOrganization.aspx.cs +++ b/Site5/Pages/v5/Security/SetCurrentOrganization.aspx.cs @@ -36,36 +36,29 @@ namespace Swarmops.Frontend.Pages.v5.Security // Some work here on PPSE pilot - we want everybody to be able to switch to Sandbox, which is #1 // except for in PPSE installation, where it is... #3 or something + // TODO: Allow logon to organization if there is a Position active + throw new UnauthorizedAccessException(); } - // this MUST be replaced with CommonV5 function - throw new NotImplementedException(); + // when we get here, we are authorized to log on to the suggested organization. + // The reason we're modifying the existing Authority object instead of creating a new one + // is to minimize the risk for impersonation exploits: The only way to assign a Person identity + // to an Authority object is on login. - - string logonFlags = string.Empty; - string[] currentIdentityParts = HttpContext.Current.User.Identity.Name.Split (','); - - if (currentIdentityParts.Length > 2) - { - logonFlags = currentIdentityParts[3]; - } - - string userIdentityString = CurrentUser.Identity.ToString (CultureInfo.InvariantCulture) + "," + - suggestedOrganization.Identity.ToString (CultureInfo.InvariantCulture) + ",," + - logonFlags; - + Authority newAuthority = CurrentAuthority; + newAuthority.SetOrganization (suggestedOrganization); // will/can also modify Position CurrentUser.LastLogonOrganizationId = suggestedOrganization.Identity; if (!string.IsNullOrEmpty (returnUrlString)) { - FormsAuthentication.SetAuthCookie (userIdentityString, true); + FormsAuthentication.SetAuthCookie (newAuthority.ToEncryptedXml(), true); Response.Redirect (returnUrlString); } else { - FormsAuthentication.RedirectFromLoginPage (userIdentityString, true); + FormsAuthentication.RedirectFromLoginPage (newAuthority.ToEncryptedXml(), true); } } }