When the support module processes an incoming e-mail from an unknown sender and creates an account, the 'name' (username) for that account is set to be their e-mail.

Because, like many other institutions, we use the LDAP module ( http://drupal.org/project/ldap ) for user authentication it is possible to lookup the user's LDAP/AD username using the "from" address of the incoming e-mail. I've written a small patch that adds this functionality if the LDAP module is enabled. Because the LDAP servers and settings are managed by the LDAP module itself, it's trivial to perform a search using these settings with no additional administrative controls.

There are several benefits, the greatest of which is that our users can log in with their existing username and password to view all of their submitted tickets -- even if they've never previously logged into the support site, having only created past tickets by e-mail.

(Architecturally, I'm not sure if this belongs in this module, or in a separate module using some as-yet-uncreated hook, etc. Or perhaps as part of hook_user_presave?)

I've attached a (lightly tested) patch that adds the following code just above where the user_save() occurs in support.module:

      // By default, username is set to match e-mail address.
      $username = $from;

      // If the LDAP module is enabled (specifically the ldap_servers submodule)
      // attempt to determine username by searching for email.
      if (module_exists("ldap_servers")) {
        $ldapservers = ldap_servers_get_servers();
        foreach ($ldapservers as $ls) {
          if ($ls->status == 1) {
            $ldap_server = ldap_servers_get_servers($ls->sid, 'all', TRUE);
            $filter = $ldap_server->mail_attr . "=" . strtolower($from);
            $ldap_user = $ldap_server->search($ldap_server->basedn, $filter, array($ldap_server->user_attr));
            if ($ldap_username = $ldap_user[0][$ldap_server->user_attr][0]) {
              $username = $ldap_username;
              break;
            }
          }
        }
      }

      watchdog('support', 'User !username automatically created.', array('!username' => $from), WATCHDOG_NOTICE);
      //return user_save(NULL, array('mail' => $from, 'init' => $from, 'name' => $from, 'status' => 1, 'roles' => array($role)));
      return user_save(NULL, array('mail' => $from, 'init' => $from, 'name' => $username, 'status' => 1));
CommentFileSizeAuthor
patch_commit_400ac22d7ee5.patch1.73 KBrchuber
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Purencool’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev