Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 11, 2014 @ 10:05
    Frans de Jong
    0

    Prevent deletion and prevent double homepage

    Hi,

    I'm looking for a way to lock certain pages for users. The homepage for instance sould not have a delete option and the document type can only exist once. The content however should be editable for the user.

    Is there a way to manage these poperties?

    Kind regards,

    Frans

    edit: I do know that I can prevent deletion per page per user but I am looking for some sort of a global setting for all users except admin for example

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Jun 11, 2014 @ 12:28
    Dave Woestenborghs
    2

    I used a eventhandler for that. This prevents deleting and unpublishing for certain doctypes

    namespace Customer.Cms.Events
    {
        using System.Collections.Generic;
        using System.Linq;
    
        using Umbraco.Core;
        using Umbraco.Core.Services;
    
        /// <summary>
        /// The prevent delete event handler
        /// </summary>
        public class PreventDelete : ApplicationEventHandler
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="PreventDelete"/> class.
            /// </summary>
            public PreventDelete()
            {
                ContentService.Trashing += this.ContentServiceTrashing;
                ContentService.UnPublishing += this.ContentServiceUnPublishing;
            }
    
            /// <summary>
            /// Gets the content types to prevent deleting and unpublishing for
            /// </summary>
            private IEnumerable<string> ContentTypes
            {
                get
                {
                    return new[]
                               {
                                   "Homepage",
                                   "Settings"
                               };
                }
            }
    
            /// <summary>
            /// The content trashing event handler
            /// </summary>
            /// <param name="sender">
            /// The sender.
            /// </param>
            /// <param name="e">
            /// The e.
            /// </param>
            private void ContentServiceTrashing(IContentService sender, Umbraco.Core.Events.MoveEventArgs<Umbraco.Core.Models.IContent> e)
            {
                if (this.ContentTypes.Contains(e.Entity.ContentType.Alias))
                {
                    e.Cancel = true;
                }
            }
    
            /// <summary>
            /// The content unpublishing event handler
            /// </summary>
            /// <param name="sender">
            /// The sender.
            /// </param>
            /// <param name="e">
            /// The e.
            /// </param>
            private void ContentServiceUnPublishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
            {
                foreach (var item in e.PublishedEntities)
                {
                    if (this.ContentTypes.Contains(item.ContentType.Alias))
                    {
                        e.Cancel = true;
                        break;
                    }
                }
            }
        }
    }
    

    Dave

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Jun 11, 2014 @ 12:37
    Dave Woestenborghs
    1

    And here is a eventhandler to allow certain doctypes only once in your site

    namespace Customer.Cms.Events
    {
        using System.Collections.Generic;
        using System.Linq;
    
        using Umbraco.Core;
        using Umbraco.Core.Models;
        using Umbraco.Core.Services;
    
        /// <summary>
        /// Allow document types to be created once
        /// </summary>
        public class AllowOnce : ApplicationEventHandler
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="AllowOnce"/> class.
            /// </summary>
            public AllowOnce()
            {
                ContentService.Creating += this.ContentServiceCreating;
            }
    
            /// <summary>
            /// Gets the content types that are allowed once under the parent
            /// </summary>
            private IEnumerable<string> ContentTypes
            {
                get
                {
                    return new[]
                               {
                                   "Homepage",
                                   "Settings"
                               };
                }
            }
    
            /// <summary>
            /// The content service_ creating.
            /// </summary>
            /// <param name="sender">
            /// The sender.
            /// </param>
            /// <param name="e">
            /// The e.
            /// </param>       
            private void ContentServiceCreating(IContentService sender, Umbraco.Core.Events.NewEventArgs<IContent> e)
            {
                if (this.ContentTypes.Contains(e.Entity.ContentType.Alias))
                {
                    if (e.ParentId == -1 && e.Entity.ContentType.Alias == DocumentTypes.Homepage.Alias)
                    {
                        // only allow one homepage
                        e.Cancel = true;
                    }
                    else
                    {
                        IContent parent = sender.GetById(e.ParentId);
                        if (parent != null)
                        {
                            if (parent.Children().Any(x => x.ContentType.Alias == e.Entity.ContentType.Alias))
                            {
                                e.Cancel = true;
                            }
                        }
                    }
                }
            }
        }
    }
  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 13, 2014 @ 11:13
    Frans de Jong
    0

    Hi Dawoe,

    Thanks for the fast reply!
    I'll try this solution in our next site.

    Kind regards,

    Frans

Please Sign in or register to post replies

Write your reply to:

Draft