Refactor some ConfirmaddressAction stuff

This commit is contained in:
Mikael Nordfeldth 2016-03-02 15:31:48 +01:00
parent 867cb225b6
commit 175b7e8541
6 changed files with 52 additions and 71 deletions

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);
}
/** /**
* Confirm an address * Confirm an address
@ -44,25 +42,14 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class ConfirmaddressAction extends Action class ConfirmaddressAction extends ManagedAction
{ {
/** type of confirmation. */ /** type of confirmation. */
var $address; protected $address;
/** protected function doPreparation()
* Accept a confirmation code
*
* Checks the code and confirms the address in the
* user record
*
* @param args $args $_REQUEST array
*
* @return void
*/
function handle($args)
{ {
parent::handle($args);
if (!common_logged_in()) { if (!common_logged_in()) {
common_set_returnto($this->selfUrl()); common_set_returnto($this->selfUrl());
common_redirect(common_local_url('login')); common_redirect(common_local_url('login'));
@ -70,32 +57,45 @@ class ConfirmaddressAction extends Action
$code = $this->trimmed('code'); $code = $this->trimmed('code');
if (!$code) { if (!$code) {
// TRANS: Client error displayed when not providing a confirmation code in the contact address confirmation action. // TRANS: Client error displayed when not providing a confirmation code in the contact address confirmation action.
$this->clientError(_('No confirmation code.')); throw new ClientException(_('No confirmation code.'));
} }
$confirm = Confirm_address::getKV('code', $code); $confirm = Confirm_address::getKV('code', $code);
if (!$confirm) { if (!$confirm instanceof Confirm_address) {
// TRANS: Client error displayed when providing a non-existing confirmation code in the contact address confirmation action. // TRANS: Client error displayed when providing a non-existing confirmation code in the contact address confirmation action.
$this->clientError(_('Confirmation code not found.')); throw new ClientException(_('Confirmation code not found.'), 404);
} }
$cur = common_current_user();
if ($cur->id != $confirm->user_id) { try {
$profile = Profile::getByID($confirm->user_id);
} catch (NoResultException $e) {
common_log(LOG_INFO, 'Tried to confirm the email for a deleted profile: '._ve(['id'=>$confirm->user_id, 'email'=>$confirm->address]));
$confirm->delete();
throw $e;
}
if (!$profile->sameAs($this->scoped)) {
// TRANS: Client error displayed when not providing a confirmation code for another user in the contact address confirmation action. // TRANS: Client error displayed when not providing a confirmation code for another user in the contact address confirmation action.
$this->clientError(_('That confirmation code is not for you!')); throw new AuthorizationException(_('That confirmation code is not for you!'));
} }
$type = $confirm->address_type; $type = $confirm->address_type;
$transports = array(); $transports = array();
Event::handle('GetImTransports', array(&$transports)); Event::handle('GetImTransports', array(&$transports));
if (!in_array($type, array('email', 'sms')) && !in_array($type, array_keys($transports))) { if (!in_array($type, array('email', 'sms')) && !in_array($type, array_keys($transports))) {
// TRANS: Server error for an unknown address type, which can be 'email', 'sms', or the name of an IM network (such as 'xmpp' or 'aim') // TRANS: Server error for an unknown address type, which can be 'email', 'sms', or the name of an IM network (such as 'xmpp' or 'aim')
$this->serverError(sprintf(_('Unrecognized address type %s'), $type)); throw new ServerException(sprintf(_('Unrecognized address type %s'), $type));
} }
$this->address = $confirm->address; $this->address = $confirm->address;
$cur = $this->scoped->getUser();
$cur->query('BEGIN'); $cur->query('BEGIN');
if (in_array($type, array('email', 'sms'))) if (in_array($type, array('email', 'sms'))) {
{ common_debug("Confirming {$type} address for user {$this->scoped->getID()}");
if ($cur->$type == $confirm->address) { if ($cur->$type == $confirm->address) {
// Already verified, so delete the confirm_address entry
$confirm->delete();
// TRANS: Client error for an already confirmed email/jabber/sms address. // TRANS: Client error for an already confirmed email/jabber/sms address.
$this->clientError(_('That address has already been confirmed.')); throw new AlreadyFulfilledException(_('That address has already been confirmed.'));
} }
$orig_user = clone($cur); $orig_user = clone($cur);
@ -122,16 +122,18 @@ class ConfirmaddressAction extends Action
$user_im_prefs->user_id = $cur->id; $user_im_prefs->user_id = $cur->id;
if ($user_im_prefs->find() && $user_im_prefs->fetch()) { if ($user_im_prefs->find() && $user_im_prefs->fetch()) {
if($user_im_prefs->screenname == $confirm->address){ if($user_im_prefs->screenname == $confirm->address){
// Already verified, so delete the confirm_address entry
$confirm->delete();
// TRANS: Client error for an already confirmed IM address. // TRANS: Client error for an already confirmed IM address.
$this->clientError(_('That address has already been confirmed.')); throw new AlreadyFulfilledException(_('That address has already been confirmed.'));
} }
$user_im_prefs->screenname = $confirm->address; $user_im_prefs->screenname = $confirm->address;
$result = $user_im_prefs->update(); $result = $user_im_prefs->update();
if (!$result) { if ($result === false) {
common_log_db_error($user_im_prefs, 'UPDATE', __FILE__); common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
// TRANS: Server error displayed when updating IM preferences fails. // TRANS: Server error displayed when updating IM preferences fails.
$this->serverError(_('Could not update user IM preferences.')); throw new ServerException(_('Could not update user IM preferences.'));
} }
}else{ }else{
$user_im_prefs = new User_im_prefs(); $user_im_prefs = new User_im_prefs();
@ -140,26 +142,18 @@ class ConfirmaddressAction extends Action
$user_im_prefs->user_id = $cur->id; $user_im_prefs->user_id = $cur->id;
$result = $user_im_prefs->insert(); $result = $user_im_prefs->insert();
if (!$result) { if ($result === false) {
common_log_db_error($user_im_prefs, 'INSERT', __FILE__); common_log_db_error($user_im_prefs, 'INSERT', __FILE__);
// TRANS: Server error displayed when adding IM preferences fails. // TRANS: Server error displayed when adding IM preferences fails.
$this->serverError(_('Could not insert user IM preferences.')); throw new ServerException(_('Could not insert user IM preferences.'));
} }
} }
} }
$result = $confirm->delete(); $confirm->delete();
if (!$result) {
common_log_db_error($confirm, 'DELETE', __FILE__);
// TRANS: Server error displayed when an address confirmation code deletion from the
// TRANS: database fails in the contact address confirmation action.
$this->serverError(_('Could not delete address confirmation.'));
}
$cur->query('COMMIT'); $cur->query('COMMIT');
$this->showPage();
} }
/** /**
@ -180,8 +174,6 @@ class ConfirmaddressAction extends Action
*/ */
function showContent() function showContent()
{ {
$cur = common_current_user();
$this->element('p', null, $this->element('p', null,
// TRANS: Success message for the contact address confirmation action. // TRANS: Success message for the contact address confirmation action.
// TRANS: %s can be 'email', 'jabber', or 'sms'. // TRANS: %s can be 'email', 'jabber', or 'sms'.

View File

@ -401,13 +401,7 @@ class EmailsettingsAction extends SettingsAction
throw new AlreadyFulfilledException(_('No pending confirmation to cancel.')); throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
} }
$result = $confirm->delete(); $confirm->delete();
if ($result === false) {
common_log_db_error($confirm, 'DELETE', __FILE__);
// TRANS: Server error thrown on database error canceling e-mail address confirmation.
throw new ServerException(_('Could not delete email confirmation.'));
}
// TRANS: Message given after successfully canceling e-mail address confirmation. // TRANS: Message given after successfully canceling e-mail address confirmation.
return _('Email confirmation cancelled.'); return _('Email confirmation cancelled.');

View File

@ -359,13 +359,7 @@ class ImsettingsAction extends SettingsAction
throw new AlreadyFulfilledException(_('No pending confirmation to cancel.')); throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
} }
$result = $confirm->delete(); $confirm->delete();
if ($result === false) {
common_log_db_error($confirm, 'DELETE', __FILE__);
// TRANS: Server error thrown on database error canceling IM address confirmation.
throw new ServerException(_('Could not delete confirmation.'));
}
// TRANS: Message given after successfully canceling IM address confirmation. // TRANS: Message given after successfully canceling IM address confirmation.
return _('IM confirmation cancelled.'); return _('IM confirmation cancelled.');

View File

@ -79,13 +79,7 @@ class RecoverpasswordAction extends Action
// Burn this code // Burn this code
$result = $confirm->delete(); $confirm->delete();
if (!$result) {
common_log_db_error($confirm, 'DELETE', __FILE__);
// TRANS: Server error displayed removing a password recovery code from the database.
$this->serverError(_('Error with confirmation code.'));
}
// These should be reaped, but for now we just check mod time // These should be reaped, but for now we just check mod time
// Note: it's still deleted; let's avoid a second attempt! // Note: it's still deleted; let's avoid a second attempt!

View File

@ -368,13 +368,7 @@ class SmssettingsAction extends SettingsAction
throw new AlreadyFulfilledException(_('No pending confirmation to cancel.')); throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
} }
$result = $confirm->delete(); $confirm->delete();
if ($result === false) {
common_log_db_error($confirm, 'DELETE', __FILE__);
// TRANS: Server error thrown on database error canceling SMS phone number confirmation.
throw new ServerException(_('Could not delete SMS confirmation.'));
}
// TRANS: Message given after successfully canceling SMS phone number confirmation. // TRANS: Message given after successfully canceling SMS phone number confirmation.
return _('SMS confirmation cancelled.'); return _('SMS confirmation cancelled.');

View File

@ -66,4 +66,17 @@ class Confirm_address extends Managed_DataObject
return $ca; return $ca;
} }
public function delete($useWhere=false)
{
$result = parent::delete($useWhere);
if ($result === false) {
common_log_db_error($confirm, 'DELETE', __FILE__);
// TRANS: Server error displayed when an address confirmation code deletion from the
// TRANS: database fails in the contact address confirmation action.
throw new ServerException(_('Could not delete address confirmation.'));
}
return $result;
}
} }