Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only show rollback button if allowed action #13563

Merged

Conversation

bjarnef
Copy link
Contributor

@bjarnef bjarnef commented Dec 12, 2022

Prerequisites

  • I have added steps to test this contribution in the description below

If there's an existing issue for this PR then this fixes #13539

Description

When rollback isn't an allowed action for the node, the rollback button shouldn't be rendered.

chrome_ZwNBFxsePW.mp4

Example using SendingContentNotification:

public class EditorSendingContentNotificationHandler : INotificationHandler<SendingContentNotification>
{
    private readonly IBackOfficeSecurityAccessor _backOfficeSecurity;

    public EditorSendingContentNotificationHandler(
        IBackOfficeSecurityAccessor backOfficeSecurity)
    {
        _backOfficeSecurity = backOfficeSecurity;
    }

    public void Handle(SendingContentNotification notification)
    {
        if (notification.Content.ContentTypeAlias.Equals("blogpost"))
        {
            var currentUser = _backOfficeSecurity?.BackOfficeSecurity?.CurrentUser;
            if (currentUser == null)
                return;

            List<string> readOnlyProperties = new()
            {
                "authors"
            };

            bool found = false;

            // Hide tabs/properties depending on user access.
            if (!found)
            {
                var tabAliases = notification.Content.Variants.First().Tabs
                    .Where(x => x.Alias != "content")
                    .Select(x => x.Alias)
                    .WhereNotNull()
                    .ToList();

                ReadOnlyProperties(notification.Content, readOnlyProperties);

                HideTabs(notification.Content, tabAliases);

                HideActions(notification.Content, new string[]
                {
                    $"{ActionUpdate.ActionLetter}",
                    $"{ActionUnpublish.ActionLetter}",
                    $"{ActionPublish.ActionLetter}",
                    $"{ActionToPublish.ActionLetter}",
                    $"{ActionRollback.ActionLetter}",
                    $"{ActionDelete.ActionLetter}"
                });
            }
        }
    }

    private static void ReadOnlyProperties(ContentItemDisplay contentItemDisplay, List<string> propertyAliases)
    {
        if (propertyAliases.Any())
        {
            foreach (var tab in contentItemDisplay.Variants.First().Tabs)
            {
                List<ContentPropertyDisplay> properties = new();

                foreach (var prop in tab.Properties!)
                {
                    if (propertyAliases.Contains(prop.Alias))
                    {
                        prop.Readonly = true;
                        //prop.Editor = "Umbraco.Label";
                        //prop.View = "readonlyvalue";
                    }

                    properties.Add(prop);
                }

                tab.Properties = properties;
            }
        }
    }

    private static void HideActions(ContentItemDisplay contentItemDisplay, string[] actions)
    {
        contentItemDisplay.AllowedActions = contentItemDisplay.AllowedActions?.Where(x => !actions.Contains(x));
    }

    private static void HideTabs(ContentItemDisplay contentItemDisplay, List<string> tabAliases)
    {
        if (tabAliases.Any())
        {
            contentItemDisplay.Variants.First().Tabs = contentItemDisplay.Variants.First().Tabs.Where(tab => !tabAliases.Contains(tab.Alias!) && tab.Properties?.Any() == true);
        }
    }

    private static void HideProperties(ContentItemDisplay contentItemDisplay, List<string> propertyAliases)
    {
        if (propertyAliases.Any())
        {
            foreach (var tab in contentItemDisplay.Variants.First().Tabs)
            {
                tab.Properties = tab.Properties?.Where(property => !propertyAliases.Contains(property.Alias));
            }
        }
    }

    private static void HideEmptyTabs(ContentItemDisplay contentItemDisplay)
    {
        contentItemDisplay.Variants.First().Tabs = contentItemDisplay.Variants.First().Tabs.Where(tab => tab.Properties?.Any() == true);
    }
}

@bjarnef
Copy link
Contributor Author

bjarnef commented Dec 12, 2022

@nikolajlauridsen @madsrasmussen @nielslyngsoe this should fix the UI issue 😉

@mikecp
Copy link
Contributor

mikecp commented Dec 18, 2022

Thanks @bjarnef for fixing this issue 👍 It now works as expected indeed 😁🎉
Cheers!

@mikecp mikecp merged commit 05a511a into umbraco:v11/contrib Dec 18, 2022
@bjarnef bjarnef deleted the v11/feature/hide-rollback-button branch December 19, 2022 10:05
@bjarnef
Copy link
Contributor Author

bjarnef commented Dec 19, 2022

@nul800sebastiaan could this be cherry picked for 10.4.0 release as well?

@nul800sebastiaan
Copy link
Member

Cherry picked for 10.5 in c04b11c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Filtering AllowedActions in SendingContentNotification doesn't prevent rollback
3 participants