2

I have a Drupal site with content type people. In that content type I have a field that contains an email id.

What I want to do is, whenever the new user is registered, I want to compare the user's email with the email field of all the nodes with people as the content type. If these 2 fields match, then set the user as the author of that node.

I don't know how to execute this process.

Can anyone give me a solution?

  • You requirement preety unique but using rules you can achieve this. Rules action when new user register compare in condition for both field. Event you need to set author. thats it. – vgoradiya Sep 2 '15 at 6:12
  • I'm new to drupal. I know this can be execute using rules. I tried but it's not working because this people content type has more than 10k nodes I want to check user mail with all the nodes mail through loop. Can you explain me in steps.? – Iswariya Ravichandran Sep 2 '15 at 6:26
  • What do you mean with "automatically authenticate the user" (as in your title)? Your question details do not say anything about that part of the question title (I think) ... – Pierre.Vriens Sep 2 '15 at 6:57
  • I has configured some rules in my site. That is whenever the new user registered i'll get mail. After getting mail, manually i'll go and set the user role. Whenever the user role is set automatically the user 'll get password change mail. This user authentication flow only i mentioned in title. – Iswariya Ravichandran Sep 2 '15 at 7:19
  • OK, these flow details help a bit. But is your question also about how to simplify this part of your flow? If so can you provide some ore details about what part to simplify? – Pierre.Vriens Sep 2 '15 at 8:01
2

Create a custom module and Use below logic hope it will work.

function hook_user_insert(&$edit, $account, $category) {
  $email = $account->email;
  $uid = $account->uid;

  $query = db_select('field_email', 'e');
  $query->addField('e', 'entity_id');
  $query->condition('e.field_email_data', $email, '=');
  $nids = $query->execute();

  foreach($nids as $nid){

    //Update node user
    $node = node_load($nid); // ...where $nid is the node id
    $node->uid    = $uid;
    node_save($node);

   //Add role to user 
   $role_name = 'member';
   if ($role = user_role_load_by_name($role_name)) {
     user_multiple_role_edit(array($uid), 'add_role', $role->rid);
   }

  }

}
1

Here is a summary of what you'll need to get it to work using the Rules module:

  1. Create a rules component (= Rule 1, of type "component") with these parameters:

    • node id.
    • user id.
    • eMail id.

    Here is what the specs are for this Rules component:

    • Condition: If the eMail id of the user id (indicated in the parameter userid id) matches the eMail id indicated in the parameter eMail id.
    • Action: Update author id of the node (indicated in the parameter node id) to the user id indicated in the parameter userid id.
  2. Create a rule (= Rule 2) containing a Loop that should be triggered (= Event) after a new user account is created. The goal of this rule is to loop over each of the nodes of content type "People". In each iteration of that loop you use an action which is to invoke the Rule Component as detailed in Step 1 (and which passes the required parameters for it). By using a Rules Component, you'll be able to add any "Conditions" you might need inside the "Loop" (in this case: check that the eMail Ids match).

Bonus features

  1. In the Rules component, if needed/appropriate, you might also do things like:

    • add additional conditions (e.g. because of changing requirements later on).
    • add additional actions (e.g. grant access to 1 or more roles for this user).
  2. You only need site building skills to implement this solution (could be interesting for somebody new to Drupal).

Video tutorials

If you're not familiar (enough/yet) with using Loops in rules, then watch the video about List and loops.

For a video that is fairly similar to what's needed in step 1 and step 2, refer to the 2nd video (= Solution video) about Creating multiple back links. Some more details about how your scenario slightly differs from what is shown in that solution video:

  • You are creating an entity reference to nodes also, but from a user (not a node).
  • You need 3 parameters in your Rules component (instead of 2).
  • Can you explain about to crate rules components that is first step in the above answer in more detail. – Iswariya Ravichandran Sep 3 '15 at 4:59

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