I'm making a rule to automatically create a user when an anonymous user adds content. They add their email address in an extra field of the node and the rule then creates a user with the email address as name and as email address. Works like a charm. However, I need to be able to check if the email address is already used, aka if the user already exists, before it creates the user, so I can redirect to the login page instead of trying to create a duplicate user.

I can't figure out how to do that. I can only find that I can fetch the users in the Actions, but I need them in the conditions, where I can use Data comparison. Can anyone explain how to do this?

Comments

wjaspers’s picture

Component: Rules Core » Rules Engine

There's a missing condition in the user integration. I have it in code, and will create a patch sometime today. Pretty straightforward. My code checks by email address.

mitchell’s picture

Status: Active » Postponed

Perhaps a view of email addresses loaded into a list that you can loop over, or using profile2 load entity by field, or wjaspers way.

Louis Bob’s picture

Hi, I have the same problem, the only difference is that I create users when they post a comment.
So I tried this data comparison:

$enbase='';
$courrier = $comment->mail;  // email from comment author, that is going to be used for account creation 
if (db_result(db_query("SELECT * FROM `users` WHERE `mail` LIKE '" . $courrier . "';"))) {
$enbase = $courrier;  //if user email already exists in DB, pass this email value to the $enbase variable
  }
return ($enbase);

So if the user email is already in the database, it will be returned and compared to the email, and therefore there will be no new account creation.

Unfortunately, I'm a PHP newbie, and I get a blank page here... Any suggestion will be warmly welcomed!

mitchell’s picture

Louis Bob’s picture

Thanks for your suggestions, but I'm actually looking for the specific piece of code to check if the user email already exists in the database.
So then I have tried this code, where $courrier is the email variable:

if (!db_query("SELECT COUNT(*) FROM {users} WHERE mail = :  $courrier ", array(':name' => $courrier))->fetchField()) {
  echo ("Email doesn't exist");
} 

I have this PDO error:
PDOException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@example.com' at line 1: SELECT COUNT(*) FROM {users} WHERE mail = test@example.com ; Array ( [mail] => test@example.com ) dans eval() (ligne 11 dans /.../sites/all/modules/rules/modules/php.eval.inc(125) : eval()'d code).


What is wrong with my SQL Syntax here ?

btopro’s picture

Version: 7.x-2.0 » 7.x-2.x-dev
Component: Rules Engine » Rules Core

Change this function and you'll have a special user-case exception of loading an entity if it already exists instead of failing to create a new one. User entities are the only ones to my knowledge with such an exception as name needing to be unique:

/**
 * Action: Create entities.
 */
function rules_action_entity_create($args, $element) {
  $values = array();
  foreach ($element->pluginParameterInfo() as $name => $info) {
    if ($name != 'type') {
      // Remove the parameter name prefix 'param_'.
      $values[substr($name, 6)] = $args[$name];
    }
  }
  try {
    // account for user entity exists error
    if (!($args['type'] == 'user' && $account = user_load_by_name($values['name']))) {
      $data = entity_property_values_create_entity($args['type'], $values);
    }
		else {
			$data = entity_metadata_wrapper('user', $account);
		}
    return array('entity_created' => $data);
  }
  catch (EntityMetadataWrapperException $e) {
    throw new RulesEvaluationException('Unable to create entity @type": ' . $e->getMessage(), array('@type' => $args['type']), $element);
  }
}
stevenx’s picture

#6 works good. thx

john.woodcock’s picture

The user_authenticate() function will return the UID if it exists and FALSE if it doesn't.

if(user_authenticate($name,$password)){
  // the user exists
}
tr’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

There were a number of ways to solve this given above.

Perhaps a more general solution would be a new feature which could be used as a condition to find out if a specific entity (user in this case) with a specific property value (email in this case) already exists: #1777204: Add condition "Entity exists by property"

Please contribute to that issue if this would be useful to you.