Add Authorization to Content Tabs

Monday, September 12, 2022 | Technical
Surjit

Surjit Bharath

Director of Hidden Foundry

Optimizely Most Valued Professional (OMVP), Subject Matter Expert (SME), CMS and Commerce certified

Contact Me

Coded Solution

In response to a question on world.optimizely.com here is a code snippet that allows you to hide content tabs based the user not having a certain role.

In this example, any users with the role "MooAdmin" will be able to see any tabs named "MooTab". Otherwise it will be hidden.

As such is the power of [EditorDescriptor], you can do much more so I recommend familiarising yourself with it.

 

[EditorDescriptorRegistration(TargetType = typeof(ContentData))]
public class HideMooTabEditorDescriptor : EditorDescriptor
{
    public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable attributes)
    {
        var accessor = ServiceLocator.Current.GetInstance();
        if (accessor.Principal.IsInRole("MooAdmin"))
        {
            return;
        }

        foreach (ExtendedMetadata property in metadata.Properties)
        {
            if (property.GroupSettings != null 
            && property.GroupSettings.Name.Equals("MooTab", StringComparison.OrdinalIgnoreCase))
            {
                property.GroupSettings.DisplayUI = false;
                return;
            }
        }
    }
}


;