Copied to clipboard

Flag this post as spam?

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


  • August 7 posts 117 karma points
    Jul 18, 2018 @ 20:49
    August
    0

    How to add user to the Administrator user group in 7.10

    I have this script that creates user accounts. It works fine when adding users to the 'editor' or 'writer' groups, but adding users to 'admin' group doesn't. The user is created, but when logged in they only get an '#undefined' page.

                Support[] users = new Support[]
            {
                    new Support { name = "<name>", username = "<username>", email = "<email>", password = "<password>", usertype = "admin" }, // undefined
                    new Support { name = "<name>", username = "<username>", email = "<email>", password = "<password>", usertype = "editor" }, // ok
                    new Support { name = "<name>", username = "<username>", email = "<email>", password = "<password>", usertype = "writer" }, // ok
            };
    
            var userService = ApplicationContext.Current.Services.UserService;
    
            foreach (var user in users)
            {
                var staff = userService.GetByEmail(user.email);
                var userGroup = userService.GetUserGroupByAlias(user.usertype);
    
                if (staff == null)
                {
                    // Create a new user
                    var newUser = userService.CreateWithIdentity(user.username, user.email, user.password, user.usertype);
    
                    newUser.Name = user.name;
    
                    var ids = new List<int>();
                    ids.Add(newUser.Id);
                    userService.Save(userGroup, ids.ToArray(), true);
    
                    userService.SavePassword(newUser, user.password);
                }
                else
                {
                    // User exists, reset password
                    var ids = new List<int>();
                    ids.Add(staff.Id);
                    userService.Save(userGroup, ids.ToArray(), true);
    
                    userService.SavePassword(staff, user.password);
                }
            }
    

    What am I missing?

  • August 7 posts 117 karma points
    Jul 20, 2018 @ 04:29
    August
    100

    Solved this by inserting directly to the database.

    var userService = ApplicationContext.Current.Services.UserService;
    

    var db = ApplicationContext.Current.DatabaseContext.Database;

    foreach (var user in users) { var staff = userService.GetByEmail(user.email);

    if (staff == null)
    {
        // Create a new user
        var newUser = userService.CreateUserWithIdentity(user.username, user.email);
    
        newUser.Name = user.name;
    
        userService.Save(newUser);
    
        userService.SavePassword(newUser, user.password);
    
        var insert = new Sql("INSERT INTO umbracoUser2UserGroup VALUES ("+ newUser.Id +", " + user.usertype + ")");
        var res = db.Execute(insert);
    }
    else
    {
        var result = new Sql("SELECT userId FROM umbracoUser2UserGroup WHERE userId = "+ staff.Id +" AND userGroupId = "+ user.usertype);
        List<int> id = new List<int>();
        id = db.Fetch<int>(result);
    
        if (id.Count() == 0)
        {
            var insert = new Sql("INSERT INTO umbracoUser2UserGroup VALUES (" + staff.Id + ", " + user.usertype + ")");
            var res = db.Execute(insert);
        }
    
        userService.SavePassword(staff, user.password);
    }
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft