Copied to clipboard

Flag this post as spam?

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


  • Robert Martine-McEvoy 24 posts 109 karma points
    Aug 25, 2015 @ 18:05
    Robert Martine-McEvoy
    0

    Adding Custom Action / Permission for a UserType in Content Tree

    Here is my question:

    1. What is the Umbraco 7 prescribed method to add an Action to the "Default Permissions" list in UserTypes and then have it show up in the context menu for users who have that permission?

    This is what I've got so far:

    I created this class which gives me the Checkbox in Default Permissions:

    public class TestNewPermission : IAction
    {
        public char Letter
        {
            get { return '+';}
        }
    
        public bool ShowInNotifier
        {
            get{ return true;}
        }
    
        public string Alias
        {
            get{return "TestNewPermission";}
        }
    
        public bool CanBePermissionAssigned
        {
            get {return true;}
        }
    
        public string Icon
        {
            get { return umbraco.BusinessLogic.Actions.ActionNotify.Instance.Icon; }
        }
    
        public string JsFunctionName
        {
            get { return "LogToConsole();"; }
        }
    
        public string JsSource
        {
            get { return "function LogToConsole(){ Console.Log('Function From IAction was hit'); }"; }
        }
    }
    

    Result in Default Permissions

    Then I hooked into the Tree Menu Rendering Event to show this action where users have the permission.

    Example A:

     private void TreeControllerBase_MenuRendering(TreeControllerBase sender, MenuRenderingEventArgs e)
        {
            if (sender.TreeAlias == "content" && sender.Security.CurrentUser.UserType.Permissions.Contains("+"))
            {
                e.Menu.Items.Add(new MenuItem(new TestNewPermission(), "My Custom Action"));
            }
        }
    

    Result in ContextMenu

    I noted that the overload to use this IAction for MenuItem read:

    public MenuItem(IAction legacyMenu, string name = "");

    When I select "My Custom Action" from the Context Menu it doesn't run the javascript defined in the IAction class.

    I also went a different route and built a MenuItem object and added it via the permission:

    Example B:

       private void TreeControllerBase_MenuRendering(TreeControllerBase sender, MenuRenderingEventArgs e)
            {
                MenuItem myMenuItem = new MenuItem();
                myMenuItem.Name = "New Action Test";
                myMenuItem.Alias = "Test";
                myMenuItem.Icon = umbraco.BusinessLogic.Actions.ActionNotify.Instance.Icon;
                myMenuItem.SeperatorBefore = true;
    
                if (sender.TreeAlias == "content" && sender.Security.CurrentUser.UserType.Permissions.Contains("+"))
                {
                    e.Menu.Items.Add(myMenuItem);
                }
            }
    

    Even when using Method B, the solution still requires the new IAction class so that the to that the '+' permission can be assigned to the UserType. If Action is considered "legacy," why would it still be needed to accomplish this?

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Aug 25, 2015 @ 18:50
    Nicholas Westby
    1

    I use the LaunchDialogView() function to open an HTML file in the flyout dialog, as shown here (I forget, but LaunchDialogView may be an extension method): https://github.com/Nicholas-Westby/usergrouppermissions/blob/upgrade-part-2/UserGroupPermissions/Events/AddUserGroupPermissionToContextMenu.cs#L61

    You can see my menu action here: https://github.com/Nicholas-Westby/usergrouppermissions/blob/upgrade-part-2/UserGroupPermissions/MenuActions/UserGroupPermissions.cs

    I check if the user has the appropriate permission here: https://github.com/Nicholas-Westby/usergrouppermissions/blob/upgrade-part-2/UserGroupPermissions/Events/AddUserGroupPermissionToContextMenu.cs#L46

    No idea if this is the recommended way. From what I remember, the Umbraco core is doing similar; however, the Umbraco core also contains a bunch of legacy stuff (such as ASPX dialogs rather than Angular dialogs).

Please Sign in or register to post replies

Write your reply to:

Draft