1

Specific user roles need to be restricted from selecting multiple publishing targets (in this example it is CDPublish and CMPublish) for publishing items in Sitecore.

For example: Administrator should be able to select both the publishing targets and proceed with publishing but when user with some specific roles (like Content regional Publisher, Content Author) select both the publishing targets for publishing they should get an custom alert message like "This role do not have the permission to publish using both the publishing targets". These specific users can publish with only one publishing targets.

enter image description here

1

Best approach would be to disable or hide publishing targets completely based on role permissions.

Administrators would still have access to all publishing targets but content editors would be restricted which is exactly what you had in requirements .

You should always aim to hide or disable functionality which users should use or click to avoid confusion.

You can really easily achieve this without any custom code.

For your scenario, disabling would be great as publishing targets would be still present but cannot be selected / checked. Check out also my blog post with more details.

1) Hide publishing target

If you want to hide publishing target, go to Security Manager and deny "Inheritance" permission for particular publishing target. This will effectively disallow "Read" and "Write" permissions and therefore hide publishing target:

enter image description here

enter image description here

2) Disable publishing target

If you allow "Read" permission, your publishing target will be shown but also disabled for that particular role:

enter image description here

enter image description here

More details can be found in my blog post where you can find step by step guide how to achieve one or other approach (disabling/hiding completely publishing targets) -> http://tothecore.sk/2018/05/17/setting-permissions-to-publishing-targets-in-sitecore

0

This can be achieved by adding a Custom code in Publish.xml.

The Publish Wizard details are present in Publish.xml and can be found inside the "shell" folder (your website\Website\sitecore\shell\Applications\Dialogs\Publish).

Edit the Publish.xml file by adding your custom code in the CodeBeside attribute. Replace the existing PublishForm, Sitecore.client with your custom code.

For Example: <WizardForm CodeBeside="Your_Project_namespace.CustomPublishForm,Your_assembly"> enter image description here

Here is the sample code added in the CodeBeside to implement the requirement.

public class CustomPublishingTarget : PublishForm
{
    protected override bool ActivePageChanging(string page, ref string newpage)
    {
        Assert.ArgumentNotNull(page, "page");
        Assert.ArgumentNotNull(newpage, "newpage");
        if (page == "Retry")
        {
            newpage = "Settings";
        }
        if (newpage == "Publishing")
        {
            if (GetLanguages().Length == 0)
            {
                SheerResponse.Alert(Translate.Text("You must pick at least one language to publish."), new string[0]);
                return false;
            }
            if (GetPublishingTargetDatabases().Length == 0)
            {
                SheerResponse.Alert(Translate.Text("You must pick at least one publishing target."), new string[0]);
                return false;
            }
            /* If both the publishing targets are selected */
            if (GetPublishingTargetDatabases().Length == 2)
            {
                User user = Context.User;
                UserRoles roles = null;
                if (user != null)
                {
                    roles = user.Roles;
                }
                /* User/Role restriction */
                if (roles != null && roles.Count() != 0 && !user.IsAdministrator)
                {
                    var listRolesToPublishTarget = ConfigurationManager.AppSettings["RolesForPublishTarget"].Split('|').ToList();
                    bool publishAdminFlag = true;
                    foreach (string publishRole in listRolesToPublishTarget)
                    {
                        if (Context.User.IsInRole(publishRole))
                        {
                            publishAdminFlag = false;
                        }
                    }
                    if (publishAdminFlag)
                    {
                        var rolesList = roles.ToList();
                        string listRolesToRestrictPublishTarget = ConfigurationManager.AppSettings["RestrictedRolesForPublishTarget"].ToString();
                        foreach (var restrictedRole in rolesList)
                        {
                            string restrictedRoleName = ((Account)(restrictedRole)).Name.ToString();
                            if (listRolesToRestrictPublishTarget.Contains(restrictedRoleName + '|'))
                            {
                                string alertMessage = ConfigurationManager.AppSettings["PublishTargetAlertMessage"].ToString().Replace("\\n", Environment.NewLine);
                                SheerResponse.Alert(Translate.Text(alertMessage, new string[0]));
                                return false;
                            }
                        }
                    }
                }
            }
            if ((this.PublishChildrenPane.Visible || this.Republish.Checked) && !this.ConfirmedOptions)
            {
                Context.ClientPage.Start(this, "ConfirmPublishingOptions");
                return false;
            }
        }
        return base.ActivePageChanging(page, ref newpage);
    }
}

Voila! now the restricted users/roles(like Content regional Publisher, Content Author) are unable to publish using both the publishing targets and they get a popup "This role do not have the permission to publish using both the publishing targets". Only the administrator(or any role configured in the config with key "RolesForPublishTarget") can publish using both the Publishing Targets.

  • 1
    Instead of custom code, you could have use Sitecore Roles and Security access to whether show the publishing targets – Hishaam Namooya Apr 10 '18 at 7:43
  • Yes I do understand the roles and security will restrict the Publishing targets but here the requirement was different please have a look at the example mentioned in the question. – Amitabh Apr 10 '18 at 7:53
  • So, they can publish to 1 target at a time? – Hishaam Namooya Apr 10 '18 at 7:56
  • @Amitabh I would also go with roles and permissions as Hishaam suggested which is IMHO much clearer solution than custom code. It is always good to restrict access where it should be restricted from administration and also user point of view. If I shouldn't have access as user to something, I shouldn't see it or it should be grayed out. Seeing popup that I don't have permissions after I click something could be misleading and confusing and frustrating. – Peter Procházka Apr 10 '18 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.