public ActionResult Index(PagerParameters pagerParameters)
        {
            if (AllowedHierarchyTypes == null)
            {
                return(new HttpUnauthorizedResult(TerritoriesUtilities.Default401HierarchyMessage));
            }

            var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);

            HierarchyAdminIndexViewModel model;

            if (AllowedHierarchyTypes.Any())
            {
                var typeNames = AllowedHierarchyTypes.Select(ctd => ctd.Name).ToArray();

                var hierarchies = _territoriesService
                                  .GetHierarchiesQuery(typeNames)
                                  .Slice(pager.GetStartIndex(), pager.PageSize);

                var pagerShape = _shapeFactory
                                 .Pager(pager)
                                 .TotalItemCount(_territoriesService.GetHierarchiesQuery(typeNames).Count());

                var entries = hierarchies
                              .Select(CreateEntry)
                              .ToList();

                model = new HierarchyAdminIndexViewModel {
                    HierarchyEntries      = entries,
                    AllowedHierarchyTypes = AllowedHierarchyTypes.ToList(),
                    Pager = pagerShape
                };
            }
            else
            {
                //No ContentType has been defined that contains the TerritoryHierarchyPart
                var pagerShape = _shapeFactory
                                 .Pager(pager)
                                 .TotalItemCount(0);

                model = new HierarchyAdminIndexViewModel {
                    HierarchyEntries      = new List <HierarchyIndexEntry>(),
                    AllowedHierarchyTypes = AllowedHierarchyTypes.ToList(),
                    Pager = pagerShape
                };
                //For now we handle this by simply pointing out that the user should create types
                AddModelError("", T("There are no Hierarchy types that the user is allowed to manage."));
            }

            return(View(model));
        }
Esempio n. 2
0
        /// <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 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 CreateHierarchy(string id)
        {
            //id is the Name of the ContentType we are trying to create. Calling that id allows us to use standard
            //MVC routing (i.e. controller/action/id?querystring). This is especially nice for the post calls.
            if (AllowedHierarchyTypes == null)
            {
                return(new HttpUnauthorizedResult(TerritoriesUtilities.Default401HierarchyMessage));
            }

            if (!AllowedHierarchyTypes.Any())   //nothing to do
            {
                return(RedirectToAction("Index"));
            }

            if (!string.IsNullOrWhiteSpace(id))   //specific type requested
            {
                var typeDefinition = AllowedHierarchyTypes.FirstOrDefault(ctd => ctd.Name == id);
                if (typeDefinition != null)
                {
                    return(CreateHierarchy(typeDefinition));
                }
            }
            return(CreatableHierarchiesList());
        }