Create and use a $u->reset_email method.

        Patch by kareila. 

diffs (157 lines):

diff -r 578acc7420cd -r 9d070efc2781 bin/upgrading/en.dat
--- a/bin/upgrading/en.dat      Thu Jun 17 21:23:26 2010 +0800
+++ b/bin/upgrading/en.dat      Thu Jun 17 22:17:09 2010 +0800
@@ -761,6 +761,22 @@ edges.watch.error.usernotvisible=Your ac
 edges.watch.error.usernotvisible=Your account must be visible.
 
 email=Email
+
+email.emailreset.body<<
+The email address for your [[sitename]] account '[[user]]' has
+been reset. To validate the change, please go to this address:
+
+     [[siteroot]]confirm/[[auth]]
+
+Regards,
+[[sitename]] Team
+
+[[siteroot]]
+.
+
+email.emailreset.error=Unable to update user record for [[user]]
+
+email.emailreset.subject=Email Address Reset
 
 email.invitecoderequest.accept.body2<<
 Your previous request for invite codes has been granted. You received 
[[number]] invite [[?number|code|codes]] from site administrators, listed below:
diff -r 578acc7420cd -r 9d070efc2781 
cgi-bin/LJ/Console/Command/ChangeJournalType.pm
--- a/cgi-bin/LJ/Console/Command/ChangeJournalType.pm   Thu Jun 17 21:23:26 
2010 +0800
+++ b/cgi-bin/LJ/Console/Command/ChangeJournalType.pm   Thu Jun 17 22:17:09 
2010 +0800
@@ -132,32 +132,23 @@ sub execute {
 
     #############################
     # update the user info
-    my %extra = ();  # aggregates all the changes we're making
-
+    my $extra = {};  # aggregates all the changes we're making
 
     # update the password
-    if ($type eq "community") {
-        $extra{password} = '';
-    } else {
-        $extra{password} = $ou->password;
-    }
+    $extra->{password} = $type eq "community" ? '' : $ou->password;
 
     LJ::infohistory_add($u, 'password', Digest::MD5::md5_hex($u->password . 
'change'))
-        if $extra{password} ne $u->password;
+        if $extra->{password} ne $u->password;
 
     # reset the email address
-    $extra{email} = $ou->email_raw;
-    $extra{status} = 'A';
-    $dbh->do("UPDATE infohistory SET what='emailreset' WHERE userid=? AND 
what='email'", undef, $u->id)
-        or $self->error("Error updating infohistory for emailreset: " . 
$dbh->errstr);
-    LJ::infohistory_add($u, 'emailreset', $u->email_raw, $u->email_status)
-        unless $ou->email_raw eq $u->email_raw; # record only if it changed
+    $extra->{status} = 'A';
 
     # get the new journaltype
-    $extra{journaltype} = $typemap->{$type};
+    $extra->{journaltype} = $typemap->{$type};
 
-    # we haev update!
-    LJ::update_user($u, { %extra });
+    # do the update!
+    $u->reset_email( $ou->email_raw, \ my $update_err, undef, $extra );
+    $self->error( $update_err ) if $update_err;
 
     # journaltype, birthday changed
     $u->invalidate_directory_record;
diff -r 578acc7420cd -r 9d070efc2781 cgi-bin/LJ/Console/Command/ResetEmail.pm
--- a/cgi-bin/LJ/Console/Command/ResetEmail.pm  Thu Jun 17 21:23:26 2010 +0800
+++ b/cgi-bin/LJ/Console/Command/ResetEmail.pm  Thu Jun 17 22:17:09 2010 +0800
@@ -44,29 +44,9 @@ sub execute {
     return $self->error("Invalid user $username")
         unless $u;
 
-    my $aa = LJ::register_authaction($u->id, "validateemail", $newemail);
-
-    LJ::infohistory_add($u, 'emailreset', $u->email_raw, $u->email_status)
-        if $u->email_raw ne $newemail;
-
-    LJ::update_user($u, { email => $newemail, status => 'T' })
-        or return $self->error("Unable to set new email address for 
$username");
-
-    my $body = "The email address for your $LJ::SITENAME account '$username' 
has been reset. To\n";
-    $body .= "validate the change, please go to this address:\n\n";
-    $body .= "     $LJ::SITEROOT/confirm/$aa->{'aaid'}.$aa->{'authcode'}\n\n";
-    $body .= "Regards,\n$LJ::SITENAME Team\n\n$LJ::SITEROOT/\n";
-
-    LJ::send_mail({
-        'to' => $newemail,
-        'from' => $LJ::ADMIN_EMAIL,
-        'subject' => "Email Address Reset",
-        'body' => $body,
-    }) or $self->info("Confirmation email could not be sent.");
-
-    my $dbh = LJ::get_db_writer();
-    $dbh->do("UPDATE infohistory SET what='emailreset' WHERE userid=? AND 
what='email'",
-             undef, $u->id) or return $self->error("Database error: " . 
$dbh->errstr);
+    $u->reset_email( $newemail, \ my $update_err, \ my $esucc );
+    return $self->error( $update_err ) if $update_err;
+    $self->info( "Confirmation email could not be sent." ) unless $esucc;
 
     my $remote = LJ::get_remote();
     LJ::statushistory_add($u, $remote, "reset_email", $reason);
diff -r 578acc7420cd -r 9d070efc2781 cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm        Thu Jun 17 21:23:26 2010 +0800
+++ b/cgi-bin/LJ/User.pm        Thu Jun 17 22:17:09 2010 +0800
@@ -4158,6 +4158,47 @@ sub opt_findbyemail {
 }
 
 
+# initiate reset of user's email
+# newemail: the new address provided (not validated?)
+# err: reference for error messages
+# emailsucc: send email if defined, report success if reference
+# update_opts: additional options for the update_user call
+sub reset_email {
+    my ( $u, $newemail, $err, $emailsucc, $update_opts ) = @_;
+    my $errsub = sub { $$err = $_[0] if ref $err; return undef };
+
+    my $dbh = LJ::get_db_writer();
+    $dbh->do( "UPDATE infohistory SET what='emailreset'" .
+              " WHERE userid=? AND what='email'", undef, $u->id ) or
+        return $errsub->( LJ::Lang::ml( "error.dberror" ) . $dbh->errstr );
+
+    LJ::infohistory_add( $u, 'emailreset', $u->email_raw, $u->email_status )
+        if $u->email_raw ne $newemail; # record only if it changed
+
+    $update_opts ||= { status => 'T' };
+    $update_opts->{email} = $newemail;
+    LJ::update_user( $u, $update_opts ) or
+        return $errsub->( LJ::Lang::ml( "email.emailreset.error",
+                                        { user => $u->user } ) );
+
+    if ( defined $emailsucc ) {
+        my $aa = LJ::register_authaction( $u->id, "validateemail", $newemail );
+        my $auth = $aa->{aaid} . $aa->{authcode};
+        my $sent = LJ::send_mail( {
+            to => $newemail,
+            from => $LJ::ADMIN_EMAIL,
+            subject => LJ::Lang::ml( "email.emailreset.subject" ),
+            body => LJ::Lang::ml( "email.emailreset.body",
+                                  { user => $u->user,
+                                    sitename => $LJ::SITENAME,
+                                    siteroot => "$LJ::SITEROOT/",
+                                    auth => $auth } ),
+        } );
+        $$emailsucc = $sent if ref $emailsucc;
+    }
+}
+
+
 sub set_email {
     my ($u, $email) = @_;
     return LJ::set_email($u->id, $email);
_______________________________________________
dw-changelog mailing list
[email protected]
http://lists.dwscoalition.org/mailman/listinfo/dw-changelog

Reply via email to