diff --git a/include/class.organization.php b/include/class.organization.php index 4f55f290b3d08d038c07c2d97552095d80932633..e1e3f44350bad64c26dcde031da2a1a2844b3eb8 100644 --- a/include/class.organization.php +++ b/include/class.organization.php @@ -159,13 +159,29 @@ class Organization extends OrganizationModel { function isMappedToDomain($domain) { foreach (explode(',', $this->domain) as $d) { - if (strcasecmp($domain, trim($d)) === 0) { + $d = trim($d); + if ($d[0] == '.') { + // Subdomain syntax (.osticket.com accepts all subdomains of + // osticket.com) + if (strcasecmp(mb_substr($domain, -mb_strlen($d)), $d) === 0) + return true; + } + elseif (strcasecmp($domain, $d) === 0) { return true; } } return false; } + static function forDomain($domain) { + foreach (static::objects() + ->filter(array('domain__contains'=>$domain)) as $org) { + if ($org->isMappedToDomain($domain)) { + return $org; + } + } + } + function to_json() { $info = array( diff --git a/include/class.orm.php b/include/class.orm.php index 2817c66dcfca74c3f194a03c34afe84a3156b47b..4a6102de98027654ee6bcca7286f5df7b72952ef 100644 --- a/include/class.orm.php +++ b/include/class.orm.php @@ -786,7 +786,7 @@ class MySqlCompiler extends SqlCompiler { function __contains($a, $b) { # {%a} like %{$b}% - return sprintf('%s LIKE %s', $a, $this->input("%$b%")); + return sprintf('%s LIKE %s', $a, $this->input($b = "%$b%")); } function __in($a, $b) { diff --git a/include/class.ticket.php b/include/class.ticket.php index 0bf1e57d7c80e088db109e2e989914d464dd55eb..57cfe653c4b14300cac586c1836881a6758a5ddc 100644 --- a/include/class.ticket.php +++ b/include/class.ticket.php @@ -2300,19 +2300,28 @@ class Ticket { // Allow vars to be changed in ticket filter and applied to the user // account created or detected + if (!$user && $vars['email']) + $user = User::lookupByEmail($vars['email']); + if (!$user) { + // Reject emails if not from registered clients (if + // configured) + if ($source == 'email' && !$cfg->acceptUnregisteredEmail()) { + list($mailbox, $domain) = explode('@', $vars['email'], 2); + // Users not yet created but linked to an organization + // are still acceptable + if (!Organization::forDomain($domain)) { + return $reject_ticket( + sprintf('Ticket rejected (%s) (unregistered client)', + $vars['email'])); + } + } + $user_form = UserForm::getUserForm()->getForm($vars); if (!$user_form->isValid($field_filter('user')) || !($user=User::fromVars($user_form->getClean()))) $errors['user'] = 'Incomplete client information'; } - - // Reject emails if not from registered clients (if configured) - if (!$cfg->acceptUnregisteredEmail() && !$user->getAccount()) { - return $reject_ticket( - sprintf('Ticket rejected (%s) (unregistered client)', - $vars['email'])); - } } // Any error above is fatal. diff --git a/include/class.user.php b/include/class.user.php index 8adb24ea07d5294cdcd2ad4d99331dd937c9789b..ac544e8f4c79bd384bc1570c8ddc177893accce9 100644 --- a/include/class.user.php +++ b/include/class.user.php @@ -144,7 +144,7 @@ class User extends UserModel { static function fromVars($vars) { // Try and lookup by email address - $user = User::lookup(array('emails__address'=>$vars['email'])); + $user = static::lookupByEmail($vars['email']); if (!$user) { $user = User::create(array( 'name'=>$vars['name'], @@ -156,13 +156,8 @@ class User extends UserModel { )); // Is there an organization registered for this domain list($mailbox, $domain) = explode('@', $vars['email'], 2); - foreach (Organization::objects() - ->filter(array('domain__contains'=>$domain)) as $org) { - if ($org->isMappedToDomain($domain)) { - $user->setOrganization($org); - break; - } - } + if ($org = Organization::forDomain($domain)) + $user->setOrganization($org); $user->save(true); $user->emails->add($user->default_email); @@ -396,6 +391,10 @@ class User extends UserModel { // Delete user return parent::delete(); } + + static function lookupByEmail($email) { + return self::lookup(array('emails__address'=>$email)); + } } class PersonsName { diff --git a/include/staff/settings-emails.inc.php b/include/staff/settings-emails.inc.php index 1d12a8e142e2bb94ae98cb0c02e24c613592299c..e42691430cd2583d4068bca30f99b7985770b30b 100644 --- a/include/staff/settings-emails.inc.php +++ b/include/staff/settings-emails.inc.php @@ -118,10 +118,10 @@ if(!defined('OSTADMININC') || !$thisstaff || !$thisstaff->isAdmin() || !$config) </td> </tr> <tr> - <td width="180">Accept Unregistered Email:</td> + <td width="180">Accept All Emails:</td> <td><input type="checkbox" name="accept_unregistered_email" <?php echo $config['accept_unregistered_email'] ? 'checked="checked"' : ''; ?>/> - Allow emailed tickets from clients without an account + Accept email from unknown Clients </tr> <tr> <td width="180">Accept Email Collaborators:</td>