2

I am trying to use user_save api. i have passed the following object ($user)

object(stdClass)#30 (6) {
  ["name"]=>
  string(12) "user_name"
  ["mail"]=>
  string(22) "[email protected]"
  ["pass"]=>
  int(1311514302)
  ["created"]=>
  int(1311514302)
  ["status"]=>
  int(1)
  ["roles"]=>
  array(1) {
    [0]=>
    string(10) "OauthUsers"
  }
}

I have used the following function:

$user = new stdClass();
$user->name = $userdata['name'];
$user->mail = $userdata['email'];
$user->pass = time();
$user->created = time();
$user->status = 1;
$user->roles = array('OauthUsers');
user_save($user);

I have tried to drop the "roles" property as well, but in all the cases user_save() returns FALSE.

Do you have any idea of what I am doing wrong?

1 Answer 1

5

UPDATED:

If you are going to create new users using user_save() then follow the following syntax.

$user = array(
   'name' => $userdata['name'],
   'mail' => $userdata['mail'],
   'init' => $userdata['mail'],
   'pass' => user_password(8),
);

Now call the function user_save() and pass the user info as second arg.

$account = user_save(null, $user);
if($account){
     _user_mail_notify('register_admin_created', $account);
}

To update values for already saved user.

$account = user_load(array('uid' => $userdata['uid']));
$updates = array(
    'pass' => user_password(),
    'name' => 'new_name',
    'roles' => array(
        <rid> => '<role title>';
    ),
);

user_save($account, $updates);
2
  • what about the role?? i need the added user to have specific role which is "OauthUsers"??
    – Alaa
    Commented Jul 24, 2011 at 16:10
  • $user = user_save($user, array('roles' => array('3' => 'special role'))); Commented Jul 24, 2011 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.