예제 #1
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 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));
        }