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
Comment #1
wjaspers commentedThere'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.
Comment #2
mitchell commentedPerhaps a view of email addresses loaded into a list that you can loop over, or using profile2 load entity by field, or wjaspers way.
Comment #3
Louis Bob commentedHi, I have the same problem, the only difference is that I create users when they post a comment.
So I tried this data comparison:
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!
Comment #4
mitchell commentedThese are related:
Comment #5
Louis Bob commentedThanks 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:
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 ?
Comment #6
btopro commentedChange 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:
Comment #7
stevenx commented#6 works good. thx
Comment #8
john.woodcock commentedThe user_authenticate() function will return the UID if it exists and FALSE if it doesn't.
Comment #9
tr commentedThere 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.