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);
}
}
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);
}
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.
What am I missing?
Solved this by inserting directly to the database.
var db = ApplicationContext.Current.DatabaseContext.Database;
foreach (var user in users) { var staff = userService.GetByEmail(user.email);
}
is working on a reply...