From 7bed9f3a1332bc1627cc3c64e7168ecaf3382194 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Tue, 22 Apr 2014 10:24:49 -0500 Subject: [PATCH] Provide failsafe for a client's name It is possible that a recipient received and processed by the system may not have a name set. In that case, the email mailbox should be used. As a failsafe, avoid crashing if the User::save() should fail. --- include/class.user.php | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/include/class.user.php b/include/class.user.php index f930379b8..a52c239b2 100644 --- a/include/class.user.php +++ b/include/class.user.php @@ -103,12 +103,13 @@ class UserModel extends VerySimpleModel { return $this->org; } - function setOrganization($org) { + function setOrganization($org, $save=true) { if (!$org instanceof Organization) return false; $this->set('org', $org); - $this->save(); + if ($save) + $this->save(); return true; } @@ -146,8 +147,12 @@ class User extends UserModel { // Try and lookup by email address $user = static::lookupByEmail($vars['email']); if (!$user) { + $name = $vars['name']; + if (!$name) + list($name) = explode('@', $vars['email'], 2); + $user = User::create(array( - 'name'=>$vars['name'], + 'name'=>$name, 'created'=>new SqlFunction('NOW'), 'updated'=>new SqlFunction('NOW'), //XXX: Do plain create once the cause @@ -156,13 +161,20 @@ class User extends UserModel { )); // Is there an organization registered for this domain list($mailbox, $domain) = explode('@', $vars['email'], 2); - if ($org = Organization::forDomain($domain)) - $user->setOrganization($org); - - $user->save(true); - $user->emails->add($user->default_email); - // Attach initial custom fields - $user->addDynamicData($vars); + if (isset($vars['org_id'])) + $user->set('org_id', $vars['org_id']); + elseif ($org = Organization::forDomain($domain)) + $user->setOrganization($org, false); + + try { + $user->save(true); + $user->emails->add($user->default_email); + // Attach initial custom fields + $user->addDynamicData($vars); + } + catch (OrmException $e) { + return null; + } } return $user; -- GitLab