From: Mikael Nordfeldth Date: Sun, 1 Mar 2015 11:36:19 +0000 (+0100) Subject: User class throws exception on register failure X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=bece816ec77d4bd6005768e950907c6bdd2e8868;p=quix0rs-gnu-social.git User class throws exception on register failure --- diff --git a/actions/apiaccountregister.php b/actions/apiaccountregister.php index b5481df1cb..14a75fc27e 100644 --- a/actions/apiaccountregister.php +++ b/actions/apiaccountregister.php @@ -1,5 +1,4 @@ clientError(_('Passwords do not match.'), 400); } else { - - // annoy spammers - sleep(7); - - if ($user = User::register(array('nickname' => $nickname, - 'password' => $password, - 'email' => $email, - 'fullname' => $fullname, - 'homepage' => $homepage, - 'bio' => $bio, - 'location' => $location, - 'code' => $this->code))) { - if (!$user instanceof User) { - // TRANS: Form validation error displayed when trying to register with an invalid username or password. - $this->clientError(_('Invalid username or password.'), 400); - } - - Event::handle('EndRegistrationTry', array($this)); - - $this->initDocument('json'); - $this->showJsonObjects($this->twitterUserArray($user->getProfile())); - $this->endDocument('json'); - - } else { - // TRANS: Form validation error displayed when trying to register with an invalid username or password. - $this->clientError(_('Invalid username or password.'), 400); - } - } + + // annoy spammers + sleep(7); + + try { + $user = User::register(array('nickname' => $nickname, + 'password' => $password, + 'email' => $email, + 'fullname' => $fullname, + 'homepage' => $homepage, + 'bio' => $bio, + 'location' => $location, + 'code' => $this->code))) { + Event::handle('EndRegistrationTry', array($this)); + + $this->initDocument('json'); + $this->showJsonObjects($this->twitterUserArray($user->getProfile())); + $this->endDocument('json'); + + } catch (Exception $e) { + $this->clientError($e->getMessage()), 400); + } + } } /** diff --git a/actions/register.php b/actions/register.php index c97201b666..7e48df9ac7 100644 --- a/actions/register.php +++ b/actions/register.php @@ -27,9 +27,7 @@ * @link http://status.net/ */ -if (!defined('STATUSNET') && !defined('LACONICA')) { - exit(1); -} +if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); } /** * An action for registering a new user account @@ -229,7 +227,9 @@ class RegisterAction extends Action } else if ($password != $confirm) { // TRANS: Form validation error displayed when trying to register with non-matching passwords. $this->showForm(_('Passwords do not match.')); - } else if ($user = User::register(array('nickname' => $nickname, + } else { + try { + $user = User::register(array('nickname' => $nickname, 'password' => $password, 'email' => $email, 'fullname' => $fullname, @@ -237,32 +237,28 @@ class RegisterAction extends Action 'bio' => $bio, 'location' => $location, 'code' => $code))) { - if (!($user instanceof User)) { + // success! + if (!common_set_user($user)) { + // TRANS: Server error displayed when saving fails during user registration. + $this->serverError(_('Error setting user.')); + } + // this is a real login + common_real_login(true); + if ($this->boolean('rememberme')) { + common_debug('Adding rememberme cookie for ' . $nickname); + common_rememberme($user); + } + + // Re-init language env in case it changed (not yet, but soon) + common_init_language(); + + Event::handle('EndRegistrationTry', array($this)); + + $this->showSuccess(); + } catch (Exception $e) { // TRANS: Form validation error displayed when trying to register with an invalid username or password. - $this->showForm(_('Invalid username or password.')); - return; - } - // success! - if (!common_set_user($user)) { - // TRANS: Server error displayed when saving fails during user registration. - $this->serverError(_('Error setting user.')); - } - // this is a real login - common_real_login(true); - if ($this->boolean('rememberme')) { - common_debug('Adding rememberme cookie for ' . $nickname); - common_rememberme($user); + $this->showForm($e->getMessage()); } - - // Re-init language env in case it changed (not yet, but soon) - common_init_language(); - - Event::handle('EndRegistrationTry', array($this)); - - $this->showSuccess(); - } else { - // TRANS: Form validation error displayed when trying to register with an invalid username or password. - $this->showForm(_('Invalid username or password.')); } } } diff --git a/classes/User.php b/classes/User.php index 764574d225..2f4670a2df 100644 --- a/classes/User.php +++ b/classes/User.php @@ -191,7 +191,8 @@ class User extends Managed_DataObject * string 'password' (may be missing for eg OpenID registrations) * string 'code' invite code * ?string 'uri' permalink to notice; defaults to local notice URL - * @return mixed User object or false on failure + * @return User object + * @throws Exception on failure */ static function register(array $fields) { @@ -205,12 +206,8 @@ class User extends Managed_DataObject $email = common_canonical_email($email); } - try { - $profile->nickname = Nickname::normalize($nickname, true); - } catch (NicknameException $e) { - common_log(LOG_WARNING, sprintf('Bad nickname during User registration for %s: %s', $nickname, $e->getMessage()), __FILE__); - return false; - } + // Normalize _and_ check whether it is in use. Throw NicknameException on failure. + $profile->nickname = Nickname::normalize($nickname, true); $profile->profileurl = common_profile_url($profile->nickname); @@ -277,7 +274,9 @@ class User extends Managed_DataObject $id = $profile->insert(); if ($id === false) { common_log_db_error($profile, 'INSERT', __FILE__); - return false; + $profile->query('ROLLBACK'); + // TRANS: Profile data could not be inserted for some reason. + throw new ServerException(_m('Could not insert profile data for new user.')); } $user->id = $id; @@ -297,7 +296,8 @@ class User extends Managed_DataObject if ($result === false) { common_log_db_error($user, 'INSERT', __FILE__); $profile->query('ROLLBACK'); - return false; + // TRANS: User data could not be inserted for some reason. + throw new ServerException(_m('Could not insert user data for new user.')); } // Everyone is subscribed to themself @@ -312,7 +312,8 @@ class User extends Managed_DataObject if (!$result) { common_log_db_error($subscription, 'INSERT', __FILE__); $profile->query('ROLLBACK'); - return false; + // TRANS: Subscription data could not be inserted for some reason. + throw new ServerException(_m('Could not insert subscription data for new user.')); } // Mark that this invite was converted @@ -334,7 +335,8 @@ class User extends Managed_DataObject if (!$result) { common_log_db_error($confirm, 'INSERT', __FILE__); $profile->query('ROLLBACK'); - return false; + // TRANS: Email confirmation data could not be inserted for some reason. + throw new ServerException(_m('Could not insert email confirmation data for new user.')); } } @@ -385,6 +387,10 @@ class User extends Managed_DataObject Event::handle('EndUserRegister', array($profile)); } + if (!$user instanceof User) { + throw new ServerException('User could not be registered. Probably an event hook that failed.'); + } + return $user; } diff --git a/lib/authenticationplugin.php b/lib/authenticationplugin.php index bf5f3aafe3..66f11ca1a9 100644 --- a/lib/authenticationplugin.php +++ b/lib/authenticationplugin.php @@ -27,9 +27,7 @@ * @link http://status.net/ */ -if (!defined('STATUSNET') && !defined('LACONICA')) { - exit(1); -} +if (!defined('GNUSOCIAL')) { exit(1); } /** * Superclass for plugins that do authentication @@ -139,7 +137,7 @@ abstract class AuthenticationPlugin extends Plugin //not much else we can do }else{ $user = $this->autoRegister($nickname, $suggested_nickname); - if($user){ + if ($user instanceof User) { User_username::register($user,$nickname,$this->provider_name); return false; } diff --git a/lib/installer.php b/lib/installer.php index d6e7fddf53..e69de7a33a 100644 --- a/lib/installer.php +++ b/lib/installer.php @@ -513,9 +513,9 @@ abstract class Installer if ($this->adminEmail) { $data['email'] = $this->adminEmail; } - $user = User::register($data); - - if (empty($user)) { + try { + $user = User::register($data); + } catch (Exception $e) { return false; } diff --git a/plugins/TwitterBridge/actions/twitterauthorization.php b/plugins/TwitterBridge/actions/twitterauthorization.php index ce6af091e2..ae293edcf7 100644 --- a/plugins/TwitterBridge/actions/twitterauthorization.php +++ b/plugins/TwitterBridge/actions/twitterauthorization.php @@ -28,9 +28,7 @@ * @link http://status.net/ */ -if (!defined('STATUSNET') && !defined('LACONICA')) { - exit(1); -} +if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); } require_once dirname(__DIR__) . '/twitter.php'; @@ -535,11 +533,10 @@ class TwitterauthorizationAction extends Action $args['email'] = $email; } - $user = User::register($args); - - if (empty($user)) { - // TRANS: Server error displayed when creating a new user has failed. - $this->serverError(_m('Error registering user.')); + try { + $user = User::register($args); + } catch (Exception $e) { + $this->serverError($e->getMessage()); } $result = $this->saveForeignLink($user->id,