I'm attempting to programmatically create a user. I'd like to be able to create the user, then set the current global $user
object to this newly created user. Here is what my code looks like.
$new_user = array(
'name' => $form_state['values']['username'],
'pass' => $form_state['values']['password'],
'mail' => $form_state['values']['email'],
'status' => 1,
'init' => $form_state['values']['email'],
'roles' => array(
DRUPAL_AUTHENTICATED_RID => 'authenticated user'
)
);
if ($saved_user = user_save(NULL, $new_user)){
user_load($saved_user->uid, TRUE);
$form_state['redirect'] = 'member/profile';
} else {
$form_state['redirect'] = 'member/join';
}
The reason for me essentially wrapping the existing user page is that there is a lot of additional functionality needed. This code does create a user in the database, but it fails in two places: 1) It does not create any roles for this new user in the users_roles
table, and 2) It does not log the current user in or adjust the global $user
object.
Any ideas why this is failing?
EDIT: Using Drupal 7.9
EDIT2: I solved the user not being logged in issue by using the function user_login_submit
. Roles still aren't creating correctly, however.