protected virtual void OnItemCreating(ItemEventArgs e)
        {
            EventHandler <ItemEventArgs> handler = base.Events[EventItemCreating] as EventHandler <ItemEventArgs>;

            if (handler != null)
            {
                handler.Invoke(this, e);
            }

            if (e.AffectedItem != null)
            {
                IDefinitionManager definitions      = Engine.Definitions;
                ISecurityManager   security         = Engine.SecurityManager;
                ContentActivator   activator        = Engine.Resolve <ContentActivator>();
                ItemDefinition     parentDefinition = definitions.GetDefinition(parentItem);

                if (parentDefinition.IsChildAllowed(definitions, parentItem, parentDefinition))
                {
                    e.AffectedItem = Engine.Resolve <ContentActivator>().CreateInstance(parentItem.GetContentType(), parentItem);
                    return;
                }
                foreach (ItemDefinition definition in definitions.GetAllowedChildren(parentItem, null).WhereAuthorized(security, HttpContext.Current.User, parentItem))
                {
                    e.AffectedItem = activator.CreateInstance(definition.ItemType, parentItem);
                    return;
                }
                throw new N2.Definitions.NoItemAllowedException(parentItem);
            }
        }
Beispiel #2
0
        public void RestrictParentsAttribute_WithNoneAllowed_DisallowsAdd()
        {
            ItemDefinition parentDefinition = engine.Definitions.GetDefinition(typeof(ItemWithDetails));          // allows child ItemInZone1Or2, restricts parent ItemInZone1Or2
            ItemDefinition childDefinition2 = engine.Definitions.GetDefinition(typeof(SideshowItem));             // allows no parents
            bool           itemWithDetailsAllowsSideshowItemAsChild = parentDefinition.IsChildAllowed(engine.Definitions, new ItemWithDetails(), childDefinition2);

            Assert.IsFalse(itemWithDetailsAllowsSideshowItemAsChild);
        }
Beispiel #3
0
        public void AllowedChildrenAttribute_AllowsDefinedTypes()
        {
            ItemDefinition parentDefinition = engine.Definitions.GetDefinition(typeof(ItemWithDetails));             // allows child ItemInZone1Or2, restricts parent ItemInZone1Or2
            ItemDefinition childDefinition1 = engine.Definitions.GetDefinition(typeof(ItemInZone1Or2));              // -
            bool           itemWithDetailsAllowsItemInZone1Or2AsChild = parentDefinition.IsChildAllowed(engine.Definitions, new ItemWithDetails(), childDefinition1);

            Assert.IsTrue(itemWithDetailsAllowsItemInZone1Or2AsChild);
        }
Beispiel #4
0
        /// <summary>Check if an item can be created.</summary>
        /// <param name="item"></param>
        /// <param name="parent"></param>
        /// <returns>The exception that would be thrown if the item was created.</returns>
        public Exception GetCreateException(ContentItem item, ContentItem parent)
        {
            ItemDefinition parentDefinition = definitions.GetDefinition(parent);
            ItemDefinition itemDefinition   = definitions.GetDefinition(item);

            if (parentDefinition == null)
            {
                throw new InvalidOperationException("Couldn't find a definition for the parent item '" + parent + "' of type '" + parent.GetContentType() + "'");
            }
            if (itemDefinition == null)
            {
                throw new InvalidOperationException("Couldn't find a definition for the item '" + item + "' of type '" + item.GetContentType() + "'");
            }

            if (!parentDefinition.IsChildAllowed(definitions, parent, itemDefinition))
            {
                return(new NotAllowedParentException(itemDefinition, parent.GetContentType()));
            }

            return(null);
        }