You are here

function support_account_load in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support.module \support_account_load()

Match up a user account with an incoming email. Create account if email doesn't match any. TODO: Make it possible to assign multiple email addresses to one account.

2 calls to support_account_load()
support_mailcmd_support_fetch_message_alter in support_mailcmd/support_mailcmd.module
Implementation of hook_support_fetch_message_alter().
support_save_message in ./support.module
Save the message.

File

./support.module, line 708
support.module

Code

function support_account_load($from, $ticket, $subject, $client) {
  $uid = db_query("SELECT uid FROM {users} WHERE mail = :mail", array(
    ':mail' => $from,
  ))
    ->fetchField();
  if ($uid) {

    // TODO Convert "user_load" to "user_load_multiple" if "$uid" is other than a uid.
    // To return a single user object, wrap "user_load_multiple" with "array_shift" or equivalent.
    // Example: array_shift(user_load_multiple(array(), $uid))
    return user_load($uid);
  }
  else {
    if ($client->user_creation == 2 || $client->user_creation == 0 && variable_get('support_autocreate_users', TRUE) == FALSE) {

      // User does not exist and the setting to allow automatic creation is
      // disabled.  Send notification to user.
      watchdog('support', 'An autocreation of a user from the e-mail address: !from was denied. The client recieving the request was: !client', array(
        '!from' => utf8_encode($from),
        '!client' => $client->name,
      ));
      _support_mail_deny($from);
      return FALSE;
    }
    else {

      // extract the domain out of the from email address
      $matches = array();
      preg_match('~[\\w-]+\\.\\w+(?=/|$)~', $from, $matches);
      $domain = $matches[0];
      $domains = _support_domains($client->domains, variable_get('support_global_domains', ''));
      $valid = TRUE;
      if (!empty($domains)) {
        $valid = FALSE;
        foreach ($domains as $match) {
          if ($domain == $match || $match == '*') {
            $valid = TRUE;
            break;
          }
        }
      }

      // TODO: this isn't a role, this is a permission

      //$role = "access $client->name tickets";
      if ($valid) {
        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' => $from,
          'status' => 1,
        ));
      }
      else {
        $ticket = support_ticket_load($ticket);
        $node = node_load($ticket->nid);
        watchdog('support', 'Email update from !from denied for ticket "!title", subject "!subject."', array(
          '!from' => $from,
          '!title' => check_plain($node->title),
          '!subject' => $subject,
        ), WATCHDOG_NOTICE);
        return FALSE;
      }
    }
  }
}