]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
User class throws exception on register failure
authorMikael Nordfeldth <mmn@hethane.se>
Sun, 1 Mar 2015 11:36:19 +0000 (12:36 +0100)
committerMikael Nordfeldth <mmn@hethane.se>
Sun, 1 Mar 2015 11:36:19 +0000 (12:36 +0100)
actions/apiaccountregister.php
actions/register.php
classes/User.php
lib/authenticationplugin.php
lib/installer.php
plugins/TwitterBridge/actions/twitterauthorization.php

index b5481df1cba369e239fb9564ab75c85711d359e8..14a75fc27ea6f21cec9e3ece1be61c17e52888d1 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-        
 /**
  * StatusNet, the distributed open-source microblogging tool
  *
@@ -152,34 +151,29 @@ class ApiAccountRegisterAction extends ApiAction
             // TRANS: Form validation error displayed when trying to register with non-matching passwords.
                $this->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);
+            }
+        }
     }
 
     /**
index c97201b666762b67611233e60544a0e134ad78a5..7e48df9ac7d9efd0d92be90d3e663fed70a9ce01 100644 (file)
@@ -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.'));
             }
         }
     }
index 764574d2254d7765d44ada464eb486ed4d5d5500..2f4670a2dfbe8092e400d1f5dc7ed1724674ea71 100644 (file)
@@ -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;
     }
 
index bf5f3aafe3a06f3d0c75eebac2f1e07a9880f517..66f11ca1a9507a5850bdb11418314b8407a39020 100644 (file)
@@ -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;
                 }
index d6e7fddf53f40f804d933a6acfa55fdaa19534fc..e69de7a33ac3eab0b215b1d2840182cb873e7bba 100644 (file)
@@ -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;
         }
 
index ce6af091e2d5adcd61a8ccca503ad024417e7b52..ae293edcf7942db0fe403c62449b3aaa6dfc662c 100644 (file)
@@ -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,