Hi everybody,

I try to create a new user when saving a new node. But I can't set the user's password.

Here is how I try to process it:
1- user data are typed in a node
2- Rules: after saving the nodes (event), Rules creates a new entity: user (action)

I tried to add another Rules action: "Set a data value". But there is no "password" token.

Any ID about how to set the password ?
I have very little programming skills, so I prefer to user the Rules interface rather than code.

Configuration:
Drupal 7.9
Entity API 7.x-1.0-beta11
Rules 7.x-2.0

Comments

eiriksm’s picture

Had the same problem today, here is my solution:

I realize you say you have little programming skills, but here is a simple snippet to do exactly what you want (and some more):

$pass = 'static_word_' . rand(10,999); //generates a password like "static_word_734" where the 3 numbers are random.
$edit = array(
'pass' => $pass,
);
user_save($entity_created, $edit); //$entity_created must be replaced with your own variable if you used something else than rules' standard.
//The following part also sets the author of the node to the newly created user.
$uid = $entity_created->uid;
$node->uid = $uid;
$node->is_new = FALSE;
node_save($node);

Hope that helps!

Jarviss’s picture

I Try to apply this code for Rules but no success, I changed $entity_created by $user_created but still no result! (Drupal 7.21)

RikD’s picture

After I created a new user with rules I send an email with the token [entity-created:one-time-login-url] so the user can login and set there own password.

brad.curnow’s picture

Hi all,

I needed to set the user's password whilst running a Rule and have broken the code by eiriksm (thanks!) down a little further. My process was to:

a) create user entity ($newuser)
b) set a rules variable $password (use PHP code to generate a random string)
c) execute custom PHP which looks like


$edit = array('pass' => $password,);
user_save($newuser, $edit);

This way you can also use the variable $password in say, an email to the user letting them know what their automatically generated password is.

Hope that helps someone.

BC

mctwist’s picture

It sure did! Thanks a million!

ShaunDychko’s picture

Thank you for the code above. It inspired this custom Rules action which accomplishes the same thing, but this way PHP doesn't need to be entered in the Rules interface, and the PHP Filter doesn't need to be enabled. Paste the following into a custom_module.rules.inc file.

/**
 * Custom Rules Actions.
 */
function custom_module_rules_action_info() {

  $actions['set_user_password'] = array(
    'parameter' => array(
      'account' => array(
        'type' => 'user',
        'label' => t('User'),
      ),
      'new_password' => array(
        'type' => 'text',
        'label' => t('New password'),
      )
    ),
    'group' => t('Custom Module'),
    'label' => t('Set a user password'),
    'base' => 'custom_module_rules_action_set_user_password',
  );
  return $actions;
}

/**
 * Action: Set a user account password.
 */
function custom_module_rules_action_set_user_password($account, $new_password) {
  $edit = array('pass' => $new_password);
  user_save($account, $edit);
}
jimkikon’s picture

Hi,
Regarding the above custom code shared by "ShaunDychko".... How do I actually use it?
I save the code in "custom_module.rules.inc" and then where to save this file at? "C:\xampp\htdocs\drupal\sites\all\modules\rules\modules?"

I would like to try this solution. Kindly advise.

kingandy’s picture

I realise this is an old question but: You'd save custom_module.rules.inc in a "custom_module" folder (eg sites/all/modules/custom_module) along with a "custom_module.info" and a "custom_module.module" file.

If that doesn't mean anything, I'd suggest a quick read through the documentation on on Creating custom modules.

++Andy
Developing Drupal websites for Livelink New Media since 2008

niallmurphy-ie’s picture

Worked great, thanks!

albie001’s picture

Worked like a charm.

Thanks