Publishing Restriction for Sitecore Items when using Publishing Service

In Sitecore, we will have different workflows and roles provided for authoring and publishing content, but not every project will follow the workflow approach. This will be easy at the time of development, but later, maintaining the content will be very difficult, as anyone with access can publish the content without needing approval.

But if we don't create workflows but only provide roles to the content authors, then it is important to provide only the necessary content authoring and publishing permissions to the content author.

Let us consider a scenario in which Sitecore has a news article item that is a bucket-able parent and has child bucket items. Anyone who has access to this news item can publish it along with its descendants.

The problem is when multiple content authors do content authoring for different news articles which are not supposed to be published unless it is properly reviewed, but if any of the content authors publish the root parent News items along with its descendants then the unverified content will also get published, this will create a big impact.

So how can we restrict the content author from publishing the content that they are not supposed to publish?
For this, we can deny write access for the items that he is not supposed to publish along with the below Sitecore settings.

<setting name="Publishing.CheckSecurity">
         <patch:attribute name="value">true</patch:attribute>
</setting>

The above setting will not allow the content author to publish the item to which he doesn't have read and write access. All of this will work with the default Sitecore Publishing wizard, but when the Sitecore is installed with the Publishing service module then this Publishing.CheckSecurity setting will not work. 

Steps we followed to resolve this issue:
  • Trying to customize the publishing pipeline to restrict the item publishing if the user belongs to a certain role, this works as expected only with the default Sitecore publishing, but after installing the Sitecore Publishing Service this custom publishing pipeline never works, as once the content author clicks on the publish button then the Publishing service popup wizard will appear, which in turns calls the API to do necessary publishing of the Sitecore items, so as all the control from the Sitecore is now gone to the Sitecore Publishing which is a .Net core application, so the custom pipeline is not impacting the running of the publishing.
Solution:

I have researched how to make sure that we can stop the content author from publishing directly the root news items along with its children, I have customized the below commands to make sure that the content author is not able to publish the content even though we are using the Sitecore Publishing service.

Using the below highlighted command I have disabled the publishing option from the ribbon, which helps to restrict the publishing of restricted content based on certain roles.

      <commands>

            <!-- Override the existing command that launches publish site dialog, to launch the new shiny one. -->
            <command name="system:publish">
                <patch:attribute name="type">Sitecore.Publishing.Service.Client.Commands.PublishSite, Sitecore.Publishing.Service.Client</patch:attribute>
            </command>

            <!-- Launches the publishing dashboard  -->
            <command name="shell:publishdashboard">
                <patch:attribute name="type">Sitecore.Publishing.Service.Client.Commands.OpenPublishDashboard,Sitecore.Publishing.Service.Client</patch:attribute>
            </command>

            <!-- Override the existing command that launches the publish item dialog, to launch the new shiny one. -->
            <command name="item:publish">
                <patch:attribute name="type">Sitecore.Publishing.Service.Client.Commands.Publish, Sitecore.Publishing.Service.Client</patch:attribute>
            </command>

            <!-- Override the existing command that launches the publish item dialog, to launch the new shiny one. -->
            <command name="item:publishnow">
                <patch:attribute name="type">Sitecore.Publishing.Service.Client.Commands.Publish, Sitecore.Publishing.Service.Client</patch:attribute>
            </command>

        </commands>


Code Snippet:

I have created two settings to hold the templates that need to be restricted from publishing by the content authors and another setting for the content author who belongs to a certain role (this role will be used to restrict the content author from publishing the root items like News, buckets, etc).

<setting name="publishingRestrictedTemplateIds" value="{ADB6CA4F-03EF-4F47-B9AC-9CE2BA53FF97},{0198E3D5-251C-42C1-AEAD-5B0B4B15D9BA},{9FD8AFF3-9BA7-4CB5-8057-FD8649D60D2D},{6FFC59AC-DA9C-4A38-90DD-741CF7A0109C}"/>
<setting name="UserRole" value="sitecore\custom role author" />

public static class SitecoreSettings { //// <summary> /// Item ID of the items that needs to be restricted from publishing /// </summary> public static string publishingRestrictedTemplateIds { get; set; } =                  GetSetting("publishingRestrictedTemplateIds"); /// <summary> /// Sitecore user role /// </summary> public static string UserRole { get; set; } = GetSetting("UserRole"); }



using Sitecore.Shell.Framework.Commands;
using System;

public class PublishingRestriction : PublishItem
{
    public override CommandState QueryState(CommandContext context)
    {
        var item = context.Items[0];
        var publishingRestrictedTemplateIds = SitecoreSettings.publishingRestrictedTemplateIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        if (publishingRestrictedTemplateIds.Length > 0)
        {
            if (publishingRestrictedTemplateIds.Any(p => ID.Parse(p).Equals(item.TemplateID)) &&
                Sitecore.Context.User.IsInRole(SitecoreSettings.UserRole))
            {
                return CommandState.Disabled;
            }
        }
        return base.QueryState(context);
    }

}


<commands> <command name="item:publishnow"> <patch:attribute name="type">namespace.Logic.Pipeline.PublishingRestriction, namespace.Foundation.Logic</patch:attribute> </command> <command name="item:publish"> <patch:attribute name="type">namespace.Logic.Pipeline.PublishingRestriction, namespace.Foundation.Logic</patch:attribute> </command> </commands>








Let's learn and grow together, happy programming ðŸ˜Š


Comments

Popular posts from this blog

Custom Item Url and resolving the item in Sitecore - Buckets

Exploring Sitecore XM Cloud Forms: A Comprehensive Overview

Sitecore Custom Rule (Action and Condition)