Exemplo n.º 1
0
        public static AjaxCallResult AssignPosition(int personId, int positionId, int durationMonths, int geographyId)
        {
            AuthenticationData authData  = GetAuthenticationDataAndCulture();
            Position           position  = Position.FromIdentity(positionId);
            Person             person    = Person.FromIdentity(personId);
            Geography          geography = (geographyId == 0 ? null : Geography.FromIdentity(geographyId));

            if (position.PositionLevel == PositionLevel.Geography ||
                position.PositionLevel == PositionLevel.GeographyDefault)
            {
                position.AssignGeography(geography);
            }

            if ((position.OrganizationId > 0 && authData.CurrentOrganization.Identity != position.OrganizationId) || person.Identity < 0)
            {
                throw new UnauthorizedAccessException();
            }
            if (position.PositionLevel == PositionLevel.SystemWide && !authData.Authority.HasAccess(new Access(AccessAspect.Administration)))
            {
                // Authority check for systemwide
                throw new UnauthorizedAccessException();
            }
            if ((position.GeographyId == Geography.RootIdentity || position.GeographyId == 0) &&
                !authData.Authority.HasAccess(new Access(authData.CurrentOrganization, AccessAspect.Administration)))
            {
                // Authority check for org-global
                throw new UnauthorizedAccessException();
            }
            if (
                !authData.Authority.HasAccess(new Access(authData.CurrentOrganization, geography,
                                                         AccessAspect.Administration)))
            {
                // Authority check for org/geo combo
                throw new UnauthorizedAccessException();
            }

            if (position.MaxCount > 0 && position.Assignments.Count >= position.MaxCount)
            {
                return(new AjaxCallResult
                {
                    Success = false,
                    DisplayMessage = Resources.Controls.Swarm.Positions_NoMorePeopleOnPosition
                });
            }

            // Deliberate: no requirement for membership (or equivalent) in order to be assigned to position.

            Position currentUserPosition = authData.CurrentUser.PositionAssignment.Position; // excludes acting positions. May throw!
            DateTime?expiresUtc          = null;

            if (durationMonths > 0)
            {
                expiresUtc = DateTime.UtcNow.AddMonths(durationMonths);
            }

            try
            {
                PositionAssignment.Create(position, geography, person, authData.CurrentUser, currentUserPosition,
                                          expiresUtc, string.Empty);
            }
            catch (DatabaseConcurrencyException)
            {
                return(new AjaxCallResult {
                    Success = false, DisplayMessage = Resources.Global.Error_DatabaseConcurrency
                });
            }

            return(new AjaxCallResult {
                Success = true
            });
        }
Exemplo n.º 2
0
        public static AjaxCallResult AssignPosition(int personId, int positionId, int durationMonths, int geographyId)
        {
            AuthenticationData authData  = GetAuthenticationDataAndCulture();
            Position           position  = Position.FromIdentity(positionId);
            Person             person    = Person.FromIdentity(personId);
            Geography          geography = (geographyId == 0 ? null : Geography.FromIdentity(geographyId));

            if (position.PositionLevel == PositionLevel.Geography ||
                position.PositionLevel == PositionLevel.GeographyDefault)
            {
                position.AssignGeography(geography);
            }

            if ((position.OrganizationId > 0 && authData.CurrentOrganization.Identity != position.OrganizationId) || person.Identity < 0)
            {
                throw new UnauthorizedAccessException();
            }
            if (position.PositionLevel == PositionLevel.SystemWide && !authData.Authority.HasAccess(new Access(AccessAspect.Administration)))
            {
                // Authority check for systemwide
                throw new UnauthorizedAccessException();
            }
            if ((position.GeographyId == Geography.RootIdentity || position.GeographyId == 0) &&
                !authData.Authority.HasAccess(new Access(authData.CurrentOrganization, AccessAspect.Administration)))
            {
                // Authority check for org-global
                throw new UnauthorizedAccessException();
            }
            if (
                !authData.Authority.HasAccess(new Access(authData.CurrentOrganization, geography,
                                                         AccessAspect.Administration)))
            {
                // Authority check for org/geo combo
                throw new UnauthorizedAccessException();
            }

            if (position.MaxCount > 0 && position.Assignments.Count >= position.MaxCount)
            {
                return(new AjaxCallResult
                {
                    Success = false,
                    DisplayMessage = Resources.Controls.Swarm.Positions_NoMorePeopleOnPosition
                });
            }

            // 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"
                });
            }

            DateTime?expiresUtc = null;

            if (durationMonths > 0)
            {
                expiresUtc = DateTime.UtcNow.AddMonths(durationMonths);
            }

            try
            {
                PositionAssignment.Create(position, geography, person, authData.CurrentUser, activePosition,
                                          expiresUtc, string.Empty);
            }
            catch (DatabaseConcurrencyException)
            {
                return(new AjaxCallResult {
                    Success = false, DisplayMessage = Resources.Global.Error_DatabaseConcurrency
                });
            }

            return(new AjaxCallResult {
                Success = true
            });
        }