Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Bit stuck with this one.
I want to hide/show a panel dependent on user in a content view in CMS.
I can do this with success but when I try and get the property to be readonly using: .Readonly, the property stays the same.
It looks like the classes are trying to be added but they are taking no effect.
Has anyone figured out If this is a bug or is there a workaround?
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class SubscribeToEditorModelEventsComposer : ComponentComposer
public class SubscribeToEditorModelEvents : IComponent
private readonly IUmbracoContextFactory _umbracoContextFactory;
public SubscribeToEditorModelEvents(IUmbracoContextFactory umbracoContextFactory)
_umbracoContextFactory = umbracoContextFactory;
// Initialize: runs once when Umbraco starts
public void Initialize()
EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel;
// Terminate: runs once when Umbraco stops
public void Terminate()
// Unsubscribe during shutdown
EditorModelEventManager.SendingContentModel -= EditorModelEventManager_SendingContentModel;
private void EditorModelEventManager_SendingContentModel(
IUser currentUser = null;
// use the umbraco context factory to ensure the umbraco context which we need to get the auth ticket of the user
using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
var userTicket = umbracoContextReference.UmbracoContext.HttpContext.GetUmbracoAuthTicket();
if (userTicket != null)
//Get current user from user service
currentUser = Current.Services.UserService.GetByUsername(userTicket.Identity.Name);
//Get all usergroups
var userGroups = currentUser.Groups;
bool userIsAdmin = false;
//Loop through user groups and check against a switch case to trigger true false statement
foreach (var group in userGroups)
// code block
userIsAdmin = true;
// code block
//Check if page alias is "home" then do stuff (locks it down to specific pages)
if (e.Model.ContentTypeAlias == "home")
//access the property you want to pre-populate
//in V8 each content item can have 'variations' - each variation is represented by the `ContentVariantDisplay` class.
//if your site uses variants, then you need to decide whether to set the default value for all variants or a specific variant
// eg. set by variant name:
// var variant = e.Model.Variants.FirstOrDefault(f => f.Name == "specificVariantName");
// OR loop through all the variants:
foreach (var variant in e.Model.Variants)
//try get the property you're wanting to change
var trueFalseTest = variant.Tabs.SelectMany(f => f.Properties)
.FirstOrDefault(f => f.Alias.InvariantEquals("trueFalseTest"));
//If page has property to set in doctype do stuff
if (trueFalseTest != null)
int trueFalseTestValue = 0;
//preset value if admin true or false
trueFalseTestValue = 1;
//set the property to the value dictated by user sign in
//any user other than admin should get a false check box
trueFalseTest.Value = trueFalseTestValue;
trueFalseTest.Readonly = true;
trueFalseTest.IsSensitive = true;
//TODO - NOTE THE READONLY PROPERTY DOESNT WORK, FORUM SUGGESTS CREATING OWN TRUE/FALSE TO GET AROUND IT WITH REQUIRED LOGIC.
//Also used this to test: https://our.umbraco.com/packages/backoffice-extensions/conditional-displayers/
var hideTheTrueFalsy = variant.Tabs.SelectMany(f => f.Properties)
.FirstOrDefault(f => f.Alias.InvariantEquals("hideTheTrueFalsy"));
if (hideTheTrueFalsy != null)
hideTheTrueFalsy.Value = 0;
hideTheTrueFalsy.Readonly = true;
hideTheTrueFalsy.IsSensitive = true;
I had a bit of an adventure with setting properties readonly with a site using variants, I wrote about it here:
The readonly property just stops the value being saved, but doesn't update the UI :-(
but the hack I found was to set the culture to be different to the current culture the editor is editing in, then magically the UI becomes readonly for that property!
Not sure if it will help in your scenario...
Thanks Marc, I shall have a read through but seems mad it doesn't work :(
What I may look to do is see if I can change the data type itself dependent on User.
Yes (sorry about the long blog post, the trick is about 3/4 of way down if you search for 'read only')
But essentially it's a coincidence we want to make the property ReadOnly and we see a property on the model called ReadOnly, but the UI is oblivious to it, it's only there for the database to tell it not to be updated...
I had some success replacing the property editors view with a 'ReadOnlyView', that was ok for textboxes, but not so cool for editors that store JSON.
The trick of switching the culture gave me the exact UI I wanted... a bit mad I know.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted