Set correct 'My Account' link after changing username

Don't print messages (and the account form) in process_account_form()
anymore, but return them to the caller. When updating accounts, this
function will be called before the headers are written.

If a username has been changed by process_account_form(), the headers
now show the updated username from the database in the 'My Account'
link. Clicking on it immediately after changing a username will no
longer lead to a non-existing URL.

Signed-off-by: Marcel Korpel <marcel.korpel@gmail.com>
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
This commit is contained in:
Marcel Korpel 2015-07-19 22:32:06 +02:00 committed by Lukas Fleischer
parent cb9c0d2477
commit 8db2ff5da6
3 changed files with 54 additions and 26 deletions

View file

@ -19,6 +19,26 @@ if (in_array($action, $need_userinfo)) {
$PK = implode("\n", account_get_ssh_keys($row["ID"])); $PK = implode("\n", account_get_ssh_keys($row["ID"]));
} }
/* This has to be done before the navigation headers are written,
* because html_header() fetches the current username from the database,
* which could be changed by process_account_form()
*/
if ($action == "UpdateAccount") {
$update_account_message = '';
/* Details for account being updated */
/* Verify user permissions and that the request is a valid POST */
if (can_edit_account($row) && check_token()) {
/* Update the details for the existing account */
list($success, $update_account_message) = process_account_form(
"edit", "UpdateAccount",
in_request("U"), in_request("T"), in_request("S"),
in_request("E"), in_request("P"), in_request("C"),
in_request("R"), in_request("L"), in_request("I"),
in_request("K"), in_request("PK"), in_request("J"),
in_request("ID"), $row["Username"]);
}
}
if ($action == "AccountInfo") { if ($action == "AccountInfo") {
html_header(__('Account') . ' ' . $row['Username']); html_header(__('Account') . ' ' . $row['Username']);
} else { } else {
@ -91,17 +111,15 @@ if (isset($_COOKIE["AURSID"])) {
} }
} elseif ($action == "UpdateAccount") { } elseif ($action == "UpdateAccount") {
/* Details for account being updated */ print $update_account_message;
/* Verify user permissions and that the request is a valid POST */
if (can_edit_account($row) && check_token()) { if (!$success) {
/* Update the details for the existing account */ display_account_form("UpdateAccount", in_request("U"), in_request("T"),
process_account_form("edit", "UpdateAccount", in_request("S"), in_request("E"), in_request("P"), in_request("C"),
in_request("U"), in_request("T"), in_request("S"), in_request("R"), in_request("L"), in_request("I"), in_request("K"),
in_request("E"), in_request("P"), in_request("C"), in_request("PK"), in_request("J"), in_request("ID"), $row["Username"]);
in_request("R"), in_request("L"), in_request("I"),
in_request("K"), in_request("PK"), in_request("J"),
in_request("ID"), $row["Username"]);
} }
} else { } else {
if (has_credential(CRED_ACCOUNT_SEARCH)) { if (has_credential(CRED_ACCOUNT_SEARCH)) {
# display the search page if they're a TU/dev # display the search page if they're a TU/dev

View file

@ -19,11 +19,20 @@ echo '<div class="box">';
echo '<h2>' . __('Register') . '</h2>'; echo '<h2>' . __('Register') . '</h2>';
if (in_request("Action") == "NewAccount") { if (in_request("Action") == "NewAccount") {
process_account_form("new", "NewAccount", in_request("U"), 1, 0, list($success, $message) = process_account_form(
"new", "NewAccount", in_request("U"), 1, 0,
in_request("E"), '', '', in_request("R"), in_request("E"), '', '', in_request("R"),
in_request("L"), in_request("I"), in_request("K"), in_request("L"), in_request("I"), in_request("K"),
in_request("PK")); in_request("PK"));
print $message;
if (!$success) {
display_account_form("NewAccount", in_request("U"), 1, 0,
in_request("E"), '', '', in_request("R"),
in_request("L"), in_request("I"), in_request("K"),
in_request("PK"));
}
} else { } else {
print '<p>' . __("Use this form to create an account.") . '</p>'; print '<p>' . __("Use this form to create an account.") . '</p>';
display_account_form("NewAccount", "", "", "", "", "", "", "", $LANG); display_account_form("NewAccount", "", "", "", "", "", "", "", $LANG);

View file

@ -89,13 +89,14 @@ function display_account_form($A,$U="",$T="",$S="",$E="",$P="",$C="",$R="",
* @param string $UID The user ID of the modified account * @param string $UID The user ID of the modified account
* @param string $N The username as present in the database * @param string $N The username as present in the database
* *
* @return string|void Return void if successful, otherwise return error * @return array Boolean indicating success and message to be printed
*/ */
function process_account_form($TYPE,$A,$U="",$T="",$S="",$E="",$P="",$C="", function process_account_form($TYPE,$A,$U="",$T="",$S="",$E="",$P="",$C="",
$R="",$L="",$I="",$K="",$PK="",$J="",$UID=0,$N="") { $R="",$L="",$I="",$K="",$PK="",$J="",$UID=0,$N="") {
global $SUPPORTED_LANGS; global $SUPPORTED_LANGS;
$error = ''; $error = '';
$message = '';
if (is_ipbanned()) { if (is_ipbanned()) {
$error = __('Account registration has been disabled ' . $error = __('Account registration has been disabled ' .
@ -247,10 +248,8 @@ function process_account_form($TYPE,$A,$U="",$T="",$S="",$E="",$P="",$C="",
} }
if ($error) { if ($error) {
print "<ul class='errorlist'><li>".$error."</li></ul>\n"; $message = "<ul class='errorlist'><li>".$error."</li></ul>\n";
display_account_form($A, $U, $T, $S, $E, "", "", return array(false, $message);
$R, $L, $I, $K, $PK, $J, $UID, $N);
return;
} }
if ($TYPE == "new") { if ($TYPE == "new") {
@ -278,25 +277,25 @@ function process_account_form($TYPE,$A,$U="",$T="",$S="",$E="",$P="",$C="",
$q.= "$I, $K)"; $q.= "$I, $K)";
$result = $dbh->exec($q); $result = $dbh->exec($q);
if (!$result) { if (!$result) {
print __("Error trying to create account, %s%s%s.", $message = __("Error trying to create account, %s%s%s.",
"<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>");
return; return array(false, $message);
} }
$uid = $dbh->lastInsertId(); $uid = $dbh->lastInsertId();
account_set_ssh_keys($uid, $ssh_keys, $ssh_fingerprints); account_set_ssh_keys($uid, $ssh_keys, $ssh_fingerprints);
print __("The account, %s%s%s, has been successfully created.", $message = __("The account, %s%s%s, has been successfully created.",
"<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>");
print "<p>\n"; $message .= "<p>\n";
if ($send_resetkey) { if ($send_resetkey) {
send_resetkey($email, true); send_resetkey($email, true);
print __("A password reset key has been sent to your e-mail address."); $message .= __("A password reset key has been sent to your e-mail address.");
print "</p>\n"; $message .= "</p>\n";
} else { } else {
print __("Click on the Login link above to use your account."); $message .= __("Click on the Login link above to use your account.");
print "</p>\n"; $message .= "</p>\n";
} }
} else { } else {
/* Modify an existing account. */ /* Modify an existing account. */
@ -341,13 +340,15 @@ function process_account_form($TYPE,$A,$U="",$T="",$S="",$E="",$P="",$C="",
$ssh_key_result = account_set_ssh_keys($UID, $ssh_keys, $ssh_fingerprints); $ssh_key_result = account_set_ssh_keys($UID, $ssh_keys, $ssh_fingerprints);
if ($result === false || $ssh_key_result === false) { if ($result === false || $ssh_key_result === false) {
print __("No changes were made to the account, %s%s%s.", $message = __("No changes were made to the account, %s%s%s.",
"<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>");
} else { } else {
print __("The account, %s%s%s, has been successfully modified.", $message = __("The account, %s%s%s, has been successfully modified.",
"<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>"); "<strong>", htmlspecialchars($U,ENT_QUOTES), "</strong>");
} }
} }
return array(true, $message);
} }
/** /**