Hey guys,

I'm writing a xml importer for some daily classified ads.

Currently, if an advertiser in the feed does not have a user account, one is automatically created. For the most part it is all working. There is an issue I'm having with the email fields and roles not making it. Advertisers have a user role of "Classified advertiser" (rid 3).

The user generation code thus far, definitely not clean enough but im more interested in seeing it work right now:

//-----------------------------------------------
// Create new advertiser, for now the email will be incorrect and the password random
//-----------------------------------------------
function create_advertiser($advertiser_name)
{  
  $mail = str_replace(' ', '', $advertiser_name);
  $mail = str_replace('.', '', $mail);
  $mail .= '@unknownemail.com';
  $name = $advertiser_name;
  $pass = rand();
  $status = 1;
  $roles = array("3");

  $from = variable_get('site_mail', ini_get('sendmail_from'));

  $merge_data = array('name' => $name, 'pass' => $pass, 'init' => $mail, 'status'=> $status, 'roles' => $roles);

  $account = user_save('', $merge_data);
}

This is basically the user_save function, chopped down to the bare bones of what I want.

Thanks,
-Todd

Comments

TPerkins’s picture

I meant to say this function is a chopped down version of user_register_submit.

Also, I've tried
$roles = array(3);
$roles = array('3');
$roles = 3;
$roles = array(0 => "3");
$roles[0] = '3';

TPerkins’s picture

Even though it doesn't display it in the original user_register_submit, I had to add "'mail' => $mail" into the merge data variable.

TPerkins’s picture

When you pass roles to the user_save function, the user_save function looks to the arrays key for the rid:

$roles = array(3 => 'classified advertiser');

psynaptic’s picture

My use case for this was using flag module to flag a node and have the author of the node assigned a role. Will work for any role assigning (adding) duties though I think:

$author = user_load($node->uid);
$roles = $author->roles; // This is so existing roles don't get overwritten.
$roles[10] = 'applicant'; // Edit this to your required rid and role name.
user_save($author, array('roles' => $roles));

This was pasted (without PHP tags) into the Execute custom PHP input of a Rules action set to fire on flagging of the node.