I'm using LDAP authentication via the LDAP_integration module, but believe it or not, the user's email is not currently part of our LDAP config. I can look it up pretty easily, and I've written the php code to get the email address, but rather than hacking ldapauth.module, I thought I would try to write a separate module.

So I think I want something like:

MyEmail_user($op, &$edit, &$user, $category = NULL) {
  switch($op) {
    case 'insert':
        watchdog('Email', t("Email is getting changed"), WATCHDOG_NOTICE);
//      print_r($edit);
        $edit[mail][init] = my_email_function($user);
        break;
  }
}

I'm pretty sure my module is getting called before the new user gets created. So I'm trying to over-ride what would get saved as the user's email. But this isn't working.

From the hook_user page insert is:

The user account is being added. The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit.

But I don't understand the details here. How can I save my custom additions to the database if the user hasn't been created yet? And aren't all the fields of $edit being saved?

Maybe I'm wrong about when my module gets called. If I comment out my $edit line, my watchdog message shows up before the messages about the new user. Am I getting fooled by that?

Comments

nevets’s picture

The user module saves the information before calling the user hook but prints the message after that. I think what you want to do is use'submit' op which allows you to modify the information before it is saved.

skor’s picture

but my module doesn't seem to get called during that op. Also, this page implies that my only choices are 'register' and 'insert'. Maybe 'submit' only gets called on subsequent updates, or only when it the user goes through the normal registration process instead of getting created via LDAP_integration.

So I think I'll have to stick with 'insert' and either

  1. do a db_query to write the data directly
  2. do a user_load, modify the data and then a user_save
  3. figure out what "The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit" means.

Actually, #1 and #2 look like the first part of #3, and the last part of #3 makes me think there's some trap waiting for me. Wish me luck.

byramm’s picture

But why not? Only, user_edit_submit() does. Is this a bug or an oversight?

timothyf’s picture

You want to use the 'register' hook to handle that.
Edit: Wait, maybe not. That's what I get for going off half-cocked without reading the api docs. Sorry.