public IContentQuery <TerritoryPart, TerritoryPartRecord> GetTerritoriesQuery(TerritoryHierarchyPart hierarchyPart, VersionOptions versionOptions) { TerritoriesUtilities.ValidateArgument(hierarchyPart, nameof(hierarchyPart)); versionOptions = versionOptions ?? (hierarchyPart.ContentItem.IsPublished() ? VersionOptions.Published : VersionOptions.Latest); return(_contentManager .Query <TerritoryPart, TerritoryPartRecord>() .WithQueryHints(new QueryHints().ExpandRecords("TerritoryHierarchyPartRecord")) .ForVersion(versionOptions) .Where(tpr => tpr.Hierarchy.Id == hierarchyPart.Record.Id)); }
public void AssignParent(TerritoryPart territory, TerritoryPart parent) { TerritoriesUtilities.ValidateArgument(territory, nameof(territory)); TerritoriesUtilities.ValidateArgument(parent, nameof(parent)); // verify parent != territory if (parent.Record.Id == territory.Record.Id) { throw new InvalidOperationException(T("The parent and child territories cannot be the same.").Text); } // verify type if (territory.ContentItem.ContentType != parent.ContentItem.ContentType) { var territoryTypeText = territory.ContentItem .TypeDefinition.DisplayName; var parentTypeText = parent.ContentItem .TypeDefinition.DisplayName; throw new ArrayTypeMismatchException( T("The ContentType for the Territory ({0}) does not match the ContentType for the parent ({1})", territoryTypeText, parentTypeText).Text); } // verify hierarchies. if (territory.Record.Hierarchy == null) { throw new ArgumentException(T("The hierarchy for the Territory must not be null.").Text, nameof(territory)); } if (parent.Record.Hierarchy == null) { throw new ArgumentException(T("The hierarchy for the Territory must not be null.").Text, nameof(parent)); } if (parent.Record.Hierarchy.Id != territory.Record.Hierarchy.Id) { throw new ArrayTypeMismatchException(T("The two territories must belong to the same hierarchy.").Text); } // verify that the assignment would not create a cycle var recordCheck = parent.Record; while (recordCheck.ParentTerritory != null) { if (recordCheck.ParentTerritory.Id == territory.Record.Id) { throw new InvalidOperationException(T("The parent territory cannot be a leaf of the child.").Text); } recordCheck = recordCheck.ParentTerritory; } // finally move territory.Record.ParentTerritory = parent.Record; }
public void AddTerritory(TerritoryPart territory, TerritoryHierarchyPart hierarchy) { TerritoriesUtilities.ValidateArgument(territory, nameof(territory)); TerritoriesUtilities.ValidateArgument(hierarchy, nameof(hierarchy)); // check that types are correct if (territory.ContentItem.ContentType != hierarchy.TerritoryType) { var territoryTypeText = territory.ContentItem .TypeDefinition.DisplayName; var hierarchyTerritoryTypeText = _contentDefinitionManager .GetTypeDefinition(hierarchy.TerritoryType).DisplayName; throw new ArrayTypeMismatchException( T("The ContentType for the Territory ({0}) does not match the expected TerritoryType for the hierarchy ({1})", territoryTypeText, hierarchyTerritoryTypeText).Text); } // The territory may come from a different hierarchy if (territory.Record.Hierarchy != null && territory.Record.Hierarchy.Id != hierarchy.Record.Id) { // Verify that the TerritoryInternalRecords in the territory or its children can be moved there var internalRecords = new List <int>(); if (territory.Record.TerritoryInternalRecord != null) { internalRecords.Add(territory.Record.TerritoryInternalRecord.Id); } if (territory.Record.Children != null) { internalRecords.AddRange(territory .Record .Children .Where(tpr => tpr.TerritoryInternalRecord != null) .Select(tpr => tpr.TerritoryInternalRecord.Id)); } if (internalRecords.Any()) { if (hierarchy.Record .Territories .Select(tpr => tpr.TerritoryInternalRecord.Id) .Any(tir => internalRecords.Contains(tir))) { throw new TerritoryInternalDuplicateException(T("The territory being moved is already assigned in the current hierarchy.")); } } } // remove parent: This method always puts the territory at the root level of the hierarchy territory.Record.ParentTerritory = null; // set hierarchy and also set the hierarchy for all children: we need to move all levels of children, // and record.Children only contains the first level. AssignHierarchyToChildren(territory.Record, hierarchy.Record); }
/// <summary> /// This method performs a bunch of default checks to verify that the user is allowed to proceed /// with the action it called. This will return false if the user is authorized to proceed. /// </summary> /// <param name="hierarchyId">The Id of a hierarchy ContentItem.</param> /// <returns>Returns false if the caller is authorized to proceed. Otherwise the ou ActionResult /// argument is populated with the Action the user should be redirected to.</returns> private bool ShouldRedirectForPermissions(int hierarchyId, out ActionResult redirectTo) { redirectTo = null; if (AllowedHierarchyTypes == null) { redirectTo = new HttpUnauthorizedResult(TerritoriesUtilities.Default401HierarchyMessage); return(true); } if (AllowedTerritoryTypes == null) { redirectTo = new HttpUnauthorizedResult(TerritoriesUtilities.Default401TerritoryMessage); return(true); } var hierarchyItem = _contentManager.Get(hierarchyId, VersionOptions.Latest); if (hierarchyItem == null) { redirectTo = HttpNotFound(); return(true); } var hierarchyPart = hierarchyItem.As <TerritoryHierarchyPart>(); if (hierarchyPart == null) { redirectTo = HttpNotFound(); return(true); } if (!AllowedHierarchyTypes.Any(ty => ty.Name == hierarchyItem.ContentType)) { var typeName = _contentDefinitionManager.GetTypeDefinition(hierarchyItem.ContentType).DisplayName; redirectTo = new HttpUnauthorizedResult(TerritoriesUtilities.SpecificHierarchy401Message(typeName)); return(true); } if (!AllowedTerritoryTypes.Any(ty => ty.Name == hierarchyPart.TerritoryType)) { var typeName = _contentDefinitionManager.GetTypeDefinition(hierarchyPart.TerritoryType).DisplayName; redirectTo = new HttpUnauthorizedResult(TerritoriesUtilities.SpecificTerritory401Message(typeName)); return(true); } return(false); }
private ActionResult ExecuteHierarchyPost( TerritoriesAdminHierarchyExecutionContext context) { var hierarchyItem = context.HierarchyItem; if (hierarchyItem == null) { return(HttpNotFound()); } #region Authorize if (AllowedHierarchyTypes == null) { return(new HttpUnauthorizedResult(TerritoriesUtilities.Default401HierarchyMessage)); } var typeName = hierarchyItem.ContentType; var typeDefinition = _contentDefinitionManager.GetTypeDefinition(typeName); if (!typeDefinition.Parts.Any(pa => pa.PartDefinition.Name == TerritoryHierarchyPart.PartName)) { AddModelError("", T("The requested type \"{0}\" is not a Hierarchy type.", typeDefinition.DisplayName)); return(RedirectToAction("Index")); } typeDefinition = AllowedHierarchyTypes.FirstOrDefault(ctd => ctd.Name == typeName); if (typeDefinition == null) { return(new HttpUnauthorizedResult(TerritoriesUtilities.SpecificHierarchy401Message(typeName))); } if (!_authorizer.Authorize(TerritoriesPermissions.ManageTerritoryHierarchies, hierarchyItem, context.Message)) { return(new HttpUnauthorizedResult()); } foreach (var permission in context.AdditionalPermissions) { if (!_authorizer.Authorize(permission, hierarchyItem, context.Message)) { return(new HttpUnauthorizedResult()); } } #endregion return(context.ExecutionAction(hierarchyItem)); }
private ActionResult CreateHierarchy(ContentTypeDefinition typeDefinition) { if (AllowedHierarchyTypes == null) { return(new HttpUnauthorizedResult(TerritoriesUtilities.Default401HierarchyMessage)); } if (!AllowedHierarchyTypes.Any(ty => ty.Name == typeDefinition.Name)) { return(new HttpUnauthorizedResult(TerritoriesUtilities.SpecificHierarchy401Message(typeDefinition.DisplayName))); } if (!typeDefinition.Parts.Any(pa => pa.PartDefinition.Name == TerritoryHierarchyPart.PartName)) { AddModelError("", T("The requested type \"{0}\" is not a Hierarchy type.", typeDefinition.DisplayName)); return(RedirectToAction("Index")); } //We should have filtered out the cases where we cannot or should not be creating the new item here var hierarchyItem = _contentManager.New(typeDefinition.Name); var model = _contentManager.BuildEditor(hierarchyItem); return(View(model)); }
public ActionResult EditHierarchy(int id) { if (AllowedHierarchyTypes == null) { return(new HttpUnauthorizedResult(TerritoriesUtilities.Default401HierarchyMessage)); } var hierarchyItem = _contentManager.Get(id, VersionOptions.Latest); if (hierarchyItem == null) { return(HttpNotFound()); } var typeName = hierarchyItem.ContentType; var typeDefinition = _contentDefinitionManager.GetTypeDefinition(typeName); if (!typeDefinition.Parts.Any(pa => pa.PartDefinition.Name == TerritoryHierarchyPart.PartName)) { AddModelError("", T("The requested type \"{0}\" is not a Hierarchy type.", typeDefinition.DisplayName)); return(RedirectToAction("Index")); } typeDefinition = AllowedHierarchyTypes.FirstOrDefault(ctd => ctd.Name == typeName); if (typeDefinition == null) { return(new HttpUnauthorizedResult(TerritoriesUtilities.SpecificHierarchy401Message(typeName))); } if (!_authorizer.Authorize(Orchard.Core.Contents.Permissions.EditContent, hierarchyItem, TerritoriesUtilities.Edit401HierarchyMessage)) { return(new HttpUnauthorizedResult()); } //We should have filtered out the cases where we cannot or should not be editing the item here var model = _contentManager.BuildEditor(hierarchyItem); return(View(model)); }
public void AssignInternalRecord(TerritoryPart territory, TerritoryInternalRecord internalRecord) { TerritoriesUtilities.ValidateArgument(territory, nameof(territory)); if (internalRecord == null || _territoriesRepositoryService.GetTerritoryInternal(internalRecord.Id) == null) { throw new ArgumentNullException(nameof(internalRecord)); } // check that the internal record does not exist yet in the same hierarchy var hierarchyRecord = territory.Record.Hierarchy; if (hierarchyRecord != null) { if (hierarchyRecord .Territories .Where(tpr => tpr.Id != territory.Record.Id) // exclude current territory .Select(tpr => tpr.TerritoryInternalRecord) .Any(tir => tir.Id == internalRecord.Id)) { throw new TerritoryInternalDuplicateException(T("The selected territory is already assigned in the current hierarchy.")); } } territory.Record.TerritoryInternalRecord = internalRecord; }
private IEnumerable <TerritoryInternalRecord> _availableTerritoryInternals; // cache results of following method public IEnumerable <TerritoryInternalRecord> GetAvailableTerritoryInternals(TerritoryHierarchyPart hierarchyPart) { TerritoriesUtilities.ValidateArgument(hierarchyPart, nameof(hierarchyPart)); if (_availableTerritoryInternals == null) { _availableTerritoryInternals = _territoriesRepositoryService .GetTerritories() .Where(tir => !hierarchyPart .Record.Territories //.Territories .Where(tpr => tpr.TerritoryInternalRecord != null) //.Where(ci => ci.As<TerritoryPart>() // .Record // .TerritoryInternalRecord != null) .Select(tpr => tpr.TerritoryInternalRecord.Id) //.Select(ci => ci.As<TerritoryPart>() // .Record // .TerritoryInternalRecord // .Id) .Contains(tir.Id) ); } return(_availableTerritoryInternals); }