Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sam 23 posts 115 karma points
    Dec 03, 2021 @ 11:33
    Sam
    0

    Umbraco 8 setting backoffice property in homepage doctype to readonly

    Hi all,

    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?

    Class below:

    using System.Linq;
    

    using Umbraco.Core; using Umbraco.Core.Composing; using Umbraco.Core.Models.Membership; using Umbraco.Core.Persistence.FaultHandling; using Umbraco.Web; using Umbraco.Web.Editors; using Umbraco.Web.Security;

    namespace PlayGroundU8.App_Code.Backoffice { [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(
            System.Web.Http.Filters.HttpActionExecutedContext sender,
            EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
        {
    
            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)
                    {
                        switch (group.Alias)
                        {
                            case "admin":
                                // code block
                                userIsAdmin = true;
                                break;
                            default:
                                // code block
                                break;
                        }
                    }
    
                    //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
                                if (userIsAdmin)
                                {
                                    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.
                                //https://our.umbraco.com/forum/using-umbraco-and-getting-started/100009-make-property-read-only#comment-314249
    
                                //Also used this to test: https://our.umbraco.com/packages/backoffice-extensions/conditional-displayers/
    
    
                            }
                            //TESTING GROUND
                            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;
                            }
                        }
                    }
                }
            }
        }
    }
    

    }

    Many thanks, Sam

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Dec 05, 2021 @ 13:17
    Marc Goodson
    0

    Hi Sam

    I had a bit of an adventure with setting properties readonly with a site using variants, I wrote about it here:

    http://tooorangey.co.uk/posts/umbraco-v8-variants-and-limiting-editor-access-by-language-an-adventure-story/

    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...

    regards

    Marc

  • Sam 23 posts 115 karma points
    Dec 06, 2021 @ 17:03
    Sam
    0

    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.

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Dec 07, 2021 @ 11:49
    Marc Goodson
    0

    Hi Sam

    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.

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft