JustNik

How to hide an Umbraco dashboard without a package manifest

Umbracologoblue05

Background

I recently came across a small dilemma, a client has requested that some dashboards in the Umbraco back office be hidden from certain user groups. Now you might all think "that's alright, we 'just' modify the package manifest file where the dashboard is registered. However, not all dashboards are registered via a package manifest, a prime example being the Umbraco Redirects dashboard.

In v7, you would modify the dashboards.config file, but again, this doesn't exist in v8 so I set about doing a quick bit of research and I have to confess this solution is not mine it's actually curtesy of Shannon (former HQ employee) from this GitHub issue

The Tip

This is code from the issue above

First up, we need to create our own dashboard definition, this seems like an absolute pain, but in reality we can inherit from the dashboard are trying to override and only implement a single property:

public class UmbracoRedirectsDashboard: RedirectUrlDashboard, IDashboard
{
    IAccessRule[] IDashboard.AccessRules { get; } = {
        new AccessRule {Type = AccessRuleType.Deny, Value = "myCustomerUserGroup"},
        new AccessRule {Type = AccessRuleType.Grant, Value = Umbraco.Core.Constants.Security.EditorGroupAlias},
        new AccessRule {Type = AccessRuleType.Grant, Value = Umbraco.Core.Constants.Security.AdminGroupAlias},
    };
}

As you can see , this isn't a full implementation, instead it is only setting the Access Rules.

Next up, we need to de-register the base dashboard, and register our own:

public class DashboardsOverrideComposer : IUserComposer
{
    public void Compose(Composition composition)
    {
        composition.Dashboards()
            .Remove<RedirectUrlDashboard>()
            .Add<UmbracoRedirectsDashboard>();
    }
}

It's as "simple" as that. This works for all sorts of dashboard, in fact I've used it for overriding the Umbraco redirects dashboard and the Skybrud Redirects dashboard.

Future Ideas

While I took this approach, I did see another thanks to Paul Seal and Kevin Jump where it can be controlled via Umbraco Editor events. Paul did this for content apps as a package and Kevin has a blog post on doing this for dashboards. I like this approach as the core code can be written once and then controlled by config again, however I had already implemented the above for speed.

In addition, a future improvement to my current approach is to create a helper that could be passed into the overridden dashboard that reads the permissions from a config setting/file instead of them being hard coded.

I'd also like to see the issue on GitHub re-opened so that the HQ can provide a clean way of managing these settings for both Dashboards and Content Apps in an integrated way without the need for overrides and re-registering dashboards.

JustNik
Connect with me
Twitter GitHub LinkedIn
© JustNik 2024