User class throws exception on register failure

This commit is contained in:
Mikael Nordfeldth 2015-03-01 12:36:19 +01:00
parent 7fdf2f50f4
commit bece816ec7
6 changed files with 71 additions and 80 deletions

View File

@ -1,5 +1,4 @@
<?php <?php
/** /**
* StatusNet, the distributed open-source microblogging tool * 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. // TRANS: Form validation error displayed when trying to register with non-matching passwords.
$this->clientError(_('Passwords do not match.'), 400); $this->clientError(_('Passwords do not match.'), 400);
} else { } 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)); // annoy spammers
sleep(7);
$this->initDocument('json'); try {
$this->showJsonObjects($this->twitterUserArray($user->getProfile())); $user = User::register(array('nickname' => $nickname,
$this->endDocument('json'); 'password' => $password,
'email' => $email,
'fullname' => $fullname,
'homepage' => $homepage,
'bio' => $bio,
'location' => $location,
'code' => $this->code))) {
Event::handle('EndRegistrationTry', array($this));
} else { $this->initDocument('json');
// TRANS: Form validation error displayed when trying to register with an invalid username or password. $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
$this->clientError(_('Invalid username or password.'), 400); $this->endDocument('json');
}
} } catch (Exception $e) {
$this->clientError($e->getMessage()), 400);
}
}
} }
/** /**

View File

@ -27,9 +27,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
exit(1);
}
/** /**
* An action for registering a new user account * An action for registering a new user account
@ -229,7 +227,9 @@ class RegisterAction extends Action
} else if ($password != $confirm) { } else if ($password != $confirm) {
// TRANS: Form validation error displayed when trying to register with non-matching passwords. // TRANS: Form validation error displayed when trying to register with non-matching passwords.
$this->showForm(_('Passwords do not match.')); $this->showForm(_('Passwords do not match.'));
} else if ($user = User::register(array('nickname' => $nickname, } else {
try {
$user = User::register(array('nickname' => $nickname,
'password' => $password, 'password' => $password,
'email' => $email, 'email' => $email,
'fullname' => $fullname, 'fullname' => $fullname,
@ -237,32 +237,28 @@ class RegisterAction extends Action
'bio' => $bio, 'bio' => $bio,
'location' => $location, 'location' => $location,
'code' => $code))) { '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. // TRANS: Form validation error displayed when trying to register with an invalid username or password.
$this->showForm(_('Invalid username or password.')); $this->showForm($e->getMessage());
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);
}
// 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.'));
} }
} }
} }

View File

@ -191,7 +191,8 @@ class User extends Managed_DataObject
* string 'password' (may be missing for eg OpenID registrations) * string 'password' (may be missing for eg OpenID registrations)
* string 'code' invite code * string 'code' invite code
* ?string 'uri' permalink to notice; defaults to local notice URL * ?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) { static function register(array $fields) {
@ -205,12 +206,8 @@ class User extends Managed_DataObject
$email = common_canonical_email($email); $email = common_canonical_email($email);
} }
try { // Normalize _and_ check whether it is in use. Throw NicknameException on failure.
$profile->nickname = Nickname::normalize($nickname, true); $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;
}
$profile->profileurl = common_profile_url($profile->nickname); $profile->profileurl = common_profile_url($profile->nickname);
@ -277,7 +274,9 @@ class User extends Managed_DataObject
$id = $profile->insert(); $id = $profile->insert();
if ($id === false) { if ($id === false) {
common_log_db_error($profile, 'INSERT', __FILE__); 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; $user->id = $id;
@ -297,7 +296,8 @@ class User extends Managed_DataObject
if ($result === false) { if ($result === false) {
common_log_db_error($user, 'INSERT', __FILE__); common_log_db_error($user, 'INSERT', __FILE__);
$profile->query('ROLLBACK'); $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 // Everyone is subscribed to themself
@ -312,7 +312,8 @@ class User extends Managed_DataObject
if (!$result) { if (!$result) {
common_log_db_error($subscription, 'INSERT', __FILE__); common_log_db_error($subscription, 'INSERT', __FILE__);
$profile->query('ROLLBACK'); $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 // Mark that this invite was converted
@ -334,7 +335,8 @@ class User extends Managed_DataObject
if (!$result) { if (!$result) {
common_log_db_error($confirm, 'INSERT', __FILE__); common_log_db_error($confirm, 'INSERT', __FILE__);
$profile->query('ROLLBACK'); $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)); 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; return $user;
} }

View File

@ -27,9 +27,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { if (!defined('GNUSOCIAL')) { exit(1); }
exit(1);
}
/** /**
* Superclass for plugins that do authentication * Superclass for plugins that do authentication
@ -139,7 +137,7 @@ abstract class AuthenticationPlugin extends Plugin
//not much else we can do //not much else we can do
}else{ }else{
$user = $this->autoRegister($nickname, $suggested_nickname); $user = $this->autoRegister($nickname, $suggested_nickname);
if($user){ if ($user instanceof User) {
User_username::register($user,$nickname,$this->provider_name); User_username::register($user,$nickname,$this->provider_name);
return false; return false;
} }

View File

@ -513,9 +513,9 @@ abstract class Installer
if ($this->adminEmail) { if ($this->adminEmail) {
$data['email'] = $this->adminEmail; $data['email'] = $this->adminEmail;
} }
$user = User::register($data); try {
$user = User::register($data);
if (empty($user)) { } catch (Exception $e) {
return false; return false;
} }

View File

@ -28,9 +28,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
exit(1);
}
require_once dirname(__DIR__) . '/twitter.php'; require_once dirname(__DIR__) . '/twitter.php';
@ -535,11 +533,10 @@ class TwitterauthorizationAction extends Action
$args['email'] = $email; $args['email'] = $email;
} }
$user = User::register($args); try {
$user = User::register($args);
if (empty($user)) { } catch (Exception $e) {
// TRANS: Server error displayed when creating a new user has failed. $this->serverError($e->getMessage());
$this->serverError(_m('Error registering user.'));
} }
$result = $this->saveForeignLink($user->id, $result = $this->saveForeignLink($user->id,