From bdf73134d232e11696669ef6e2ebca7c258e4555 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Tue, 17 Jan 2017 22:30:30 +0100 Subject: [PATCH] Fixed assignment authority problem --- Site/Automation/SwarmFunctions.aspx.cs | 34 +++++++++++++++++++++-- Site/Controls/v5/Swarm/TreePositions.ascx | 11 +++++++- Site/Pages/v5/Public/Initialize.aspx.cs | 8 ++++-- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Site/Automation/SwarmFunctions.aspx.cs b/Site/Automation/SwarmFunctions.aspx.cs index bd9d18eb..236afc97 100644 --- a/Site/Automation/SwarmFunctions.aspx.cs +++ b/Site/Automation/SwarmFunctions.aspx.cs @@ -73,8 +73,38 @@ namespace Swarmops.Frontend.Automation } // Deliberate: no requirement for membership (or equivalent) in order to be assigned to position. + // Find the current user position used to assign. + + PositionAssignments currentUserAssignments = authData.CurrentUser.PositionAssignments; + + // Get the one this user is currently using to assign - it's either a system level position, + // one with a parent organization (TODO), or one with this organization + + Position activePosition = null; + + foreach (PositionAssignment currentUserAssignment in currentUserAssignments) + { + if (currentUserAssignment.OrganizationId == 0 && currentUserAssignment.Active) + { + activePosition = currentUserAssignment.Position; + break; // a system-level active position has priority over org-level + } + if (currentUserAssignment.OrganizationId == authData.CurrentOrganization.Identity && + currentUserAssignment.Active) + { + activePosition = currentUserAssignment.Position; + } + } + + if (activePosition == null) + { + return new AjaxCallResult + { + Success = false, + DisplayMessage = "Error: No authority to assign a position" + }; + } - Position currentUserPosition = authData.CurrentUser.PositionAssignment.Position; // excludes acting positions. May throw! DateTime? expiresUtc = null; if (durationMonths > 0) @@ -84,7 +114,7 @@ namespace Swarmops.Frontend.Automation try { - PositionAssignment.Create (position, geography, person, authData.CurrentUser, currentUserPosition, + PositionAssignment.Create (position, geography, person, authData.CurrentUser, activePosition, expiresUtc, string.Empty); } catch (DatabaseConcurrencyException) diff --git a/Site/Controls/v5/Swarm/TreePositions.ascx b/Site/Controls/v5/Swarm/TreePositions.ascx index bced3b2c..7c6f186b 100644 --- a/Site/Controls/v5/Swarm/TreePositions.ascx +++ b/Site/Controls/v5/Swarm/TreePositions.ascx @@ -134,9 +134,18 @@ // TODO: Check existing position for person; can't have two positions, at least not within same org + var jsonData = { + personId: personId, + positionId: current<%=this.ClientID%>PositionId, + durationMonths: <%= this.DropDuration.ClientID%>_val(), + geographyId: current<%=this.ClientID%>GeographyId + }; + + console.log(jsonData); + SwarmopsJS.ajaxCall( "/Automation/SwarmFunctions.aspx/AssignPosition", - { personId: personId, positionId: current<%=this.ClientID%>PositionId, durationMonths: $('#<%= this.DropDuration.ClientID%>').val(), geographyId: current<%=this.ClientID%>GeographyId }, + jsonData, function (result) { // Close modal and reload grid regardless of success or not // (the only failure here should be a concurrency error, in which case diff --git a/Site/Pages/v5/Public/Initialize.aspx.cs b/Site/Pages/v5/Public/Initialize.aspx.cs index 273e248f..32e5e783 100644 --- a/Site/Pages/v5/Public/Initialize.aspx.cs +++ b/Site/Pages/v5/Public/Initialize.aspx.cs @@ -11,6 +11,7 @@ using System.Web.Security; using System.Web.Services; using System.Web.SessionState; using System.Web.UI; +using NBitcoin.Protocol; using Resources; using Swarmops.Common; using Swarmops.Common.Enums; @@ -781,12 +782,15 @@ namespace Swarmops.Frontend.Pages.v5.Public SwarmDb.GetDatabaseForWriting().CreateExceptionLogEntry(DateTime.UtcNow, "Initialization", new Exception("Tracepoint 1")); - newPerson.AddParticipation(Organization.Sandbox, DateTime.UtcNow.AddYears(25)); // Add membership in Sandbox + newPerson.AddParticipation(Organization.Sandbox, DateTime.UtcNow.AddYears(25)); // Initialize staffing to System and Sandbox with the new user - Positions.CreateSysadminPositions(); + + Positions.ForOrganization(Organization.Sandbox).AtLevel(PositionLevel.OrganizationExecutive)[0].Assign( + newPerson, null /* assignedByPerson */, null /* assignedByPosition */, "Initial Sandbox executive", + null /* expires */); } catch (Exception exception) {