Copied to clipboard

Flag this post as spam?

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


  • Carlos Casalicchio 138 posts 561 karma points
    May 28, 2019 @ 15:20
    Carlos Casalicchio
    1

    Default User Group Permissions

    This is not so much as a question, but just something I wanted to share, since it took some work for me to figure it out.

    When creating new groups programmatically, one can set the default permissions, but they are somewhat cryptic, and not that intuitive.

    Here is the list of default permissions for Groups (these will give access to specific features)

    User Group Permissions

    Cultures and Hostnames (I)
    Public Access (P)
    Rollback (K)
    Browse Node (F)
    Create Content Template (ï)
    Delete (D)
    Create (C)
    Publish (U)
    Permissions (R)
    Send To Publish (H)
    Unpublish (Z)
    Update (A)
    Copy (O)
    Move (M)
    Sort (S)
    

    so, in order to use these in your code, do something like this:

    First create the IUserGroup

    UserGroup group = new UserGroup
            {
                Alias = "mycustomgroup",
                Name = "My Custom Group",
                Icon = "icon-umb-members",
                Permissions = new List<string> { "I", "P", "K", "F", "ï", "D", "C", "U", "A", "O", "S" },
                StartContentId = 1080,
                StartMediaId = 1081,
                Key = Guid.NewGuid()
            };
            group.AddAllowedSection("content");
            group.AddAllowedSection("media");
    

    Then create the group in Umbraco

    public UserGroup CreateUserGroup(IUserGroup group)
            {
                UserGroup newGroup = (UserGroup)userService.GetUserGroupByAlias(group.Alias);
                if (newGroup != null) throw new Exception("Group already exists");
    
                try
                {
                    userService.Save(newGroup);
                    ConnectorContext.AuditService.Add(AuditType.New, -1, newGroup.Id, "User Group", $"User Group {newGroup.Name} has been created");
                    return newGroup;
                }
                catch (Exception ex)
                {
                    logger.Error(typeof(UserGroupHelper), ex.Message);
                    logger.Error(typeof(UserGroupHelper), ex.StackTrace);
                    throw;
                }
            }
    
  • Martin Rhodes 12 posts 129 karma points
    Jan 27, 2021 @ 15:07
    Martin Rhodes
    0

    This is exactly what I was looking for - thanks for sharing!

  • Martin Rhodes 12 posts 129 karma points
    Jan 27, 2021 @ 15:26
    Martin Rhodes
    100

    For anyone who would like these as string constants, here you go!

    public static class UmbracoUserGroupPermission
    {
        public static string CulturesAndHostnames => "I";
        public static string PublicAccess => "P";
        public static string Rollback => "K";
        public static string BrowseNode => "F";
        public static string CreateContentTemplate => "ï";
        public static string Delete => "D";
        public static string Create => "C";
        public static string Publish => "U";
        public static string Permissions => "R";
        public static string SendToPublish => "H";
        public static string Unpublish => "Z";
        public static string Update => "A";
        public static string Copy => "O";
        public static string Move => "M";
        public static string Sort => "S";
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft