Copied to clipboard

Flag this post as spam?

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


  • alexander odhomi 3 posts 83 karma points
    1 week ago
    alexander odhomi
    0

    Umbraco 7.7 user group how to (Not member group or type)

    Hey guys My first post/solution seeking. C# developer Short question, how do I programmatically create new user groups and assign a user to that group. We just upgraded to 7.7.4 from 7.4.2.

    We had to change some of our custom code creating or editing users (not members) with existing user types and assigning permissions to users directly, to now assigning permissions to groups. This is all part of a sync service pushing users into umbraco from another application where users and and categorisation entities called tiers are created.

    The architecture of our application requires that we create multiple new groups unique to the user's role and each tier he has access to. Content also are categorised by these tiers so permissios have to be synced regularly.

    We really need to know the best way to create these new user groups in c# and immediately be able to assign users to them.

    Thanks in advance

  • Matt Darby 18 posts 242 karma points
    1 week ago
    Matt Darby
    100

    Hey Alex,

    You can use the UserService for this. For example:

    // Create a user group
    
    var userGroup = new UserGroup
    {
        Alias = "testgroup",
        Name = "Test Group"
    };
    
    userGroup.AddAllowedSection("content");
    userGroup.AddAllowedSection("media");
    
    Services.UserService.Save(userGroup);
    
    // Assign user to the group
    
    var user = Services.UserService.GetByEmail("test@test.com");
    user.AddGroup(userGroup);
    
    Services.UserService.Save(user);
    

    UserGroup is from Umbraco.Core.Models.Membership

  • alexander odhomi 3 posts 83 karma points
    1 week ago
    alexander odhomi
    0

    Thank you Matt, that worked. thanks

  • Mikhail 1 post 71 karma points
    1 day ago
    Mikhail
    0

    Hi Matt! Is there a way to do this at 7.7.2? Class UserGroup is internal at this version

  • alexander odhomi 3 posts 83 karma points
    1 week ago
    alexander odhomi
    0

    Thank you Matt, I'll try this and get back to you.

Please Sign in or register to post replies

Write your reply to:

Draft