Copied to clipboard

Flag this post as spam?

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


  • Harry Gordon 10 posts 86 karma points c-trib
    Feb 01, 2021 @ 14:49
    Harry Gordon
    1

    Creating custom IActions

    Hi all,

    I'd like to add custom granular permissions for a project where access control is a particular concern. Is it valid to add my own IActions or is this unsupported?

    For example, if I add this following to my Umbraco project it shows in the permission list:

    public class ActionDownloadCsv : IAction
    {
        public const char ActionLetter = '1';
    
        public char Letter => ActionLetter;
        public bool ShowInNotifier => false;
        public bool CanBePermissionAssigned => true;
        public string Icon => "";
        public string Alias => "downloadCsv";
        public string Category => "Custom";
    }
    

    enter image description here

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Feb 01, 2021 @ 16:02
    Kevin Jump
    100

    Hi Harry,

    yes you can do this and you are mostly there.

    you need to add values into a language xml file to get the names to appear. (e.g in a file called /app_plugins/mypackage/lang/en-us.xml)

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <language alias="en" intName="English (US)" localName="English (US)" lcid="" culture="en-US">
        <area alias="actions">
            <key alias="downloadCsv">Download Csv</key>
        </area>
        <area alias="actionDescriptions">
            <key alias="downloadCsv">Download the CSV file</key>
        </area>
        <area alias="actionCategories">
            <key alias="custom">My Custom bit</key>
        </area>
    </language>
    

    checking the permissions (in your code), you will need to get them from the current user

    the permissions come back as an array from the user.

    e.g

     // get user permissions
     var permissions = _userService.GetPermissions(e.UmbracoContext.Security.CurrentUser, -1);
    
    // does the user NOT have translate permissions?
    if (!permissions.Any(x => x.AssignedPermissions.Contains(ActionDownloadCsv.ActionLetter))) { 
    
        /// do your thing
    }
    

    I would just check you are not reusing an umbraco permission letter, https://github.com/umbraco/Umbraco-CMS/tree/34e80d86e8c0b754f6b7a02e307f53cb32806bbe/src/Umbraco.Web/Actions (not sure if this is documented somewhere, but the code has them all here)

  • Harry Gordon 10 posts 86 karma points c-trib
    Feb 01, 2021 @ 19:38
    Harry Gordon
    0

    Thanks Kevin, that's good to hear. It doesn't seem to be documented so that made me think maybe it's not intended to be extended.

    The permission letters are mentioned here actually: https://our.umbraco.com/Documentation/Extending/Section-Trees/tree-actions-v8

    I'll get in touch with the docs team and see about writing something on creating custom Actions.

    Thanks for including those snippets, they will certainly save me some time tomorrow 🎉.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies