From 6bbf8324008871068ddf46a80567a3887bb984be Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Thu, 2 Feb 2023 09:09:25 -0500 Subject: [PATCH] DEV: Allow HTML in grant admin dialog (#20133) In a private plugin, we need to show an error message containing HTML when the Grant Admin action fails. This change introduces a new flag (`html_message: true`) that when used will allow the dialog to render the HTML tags in the error message correctly. --- .../addon/controllers/admin-user-index.js | 9 ++++- .../tests/acceptance/admin-user-index-test.js | 34 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/addon/controllers/admin-user-index.js b/app/assets/javascripts/admin/addon/controllers/admin-user-index.js index b6859b9ce0e..25403010f1a 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-user-index.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-user-index.js @@ -232,7 +232,14 @@ export default Controller.extend(CanCheckEmails, { queryParams: { nonce }, }); } else { - popupAjaxError(error); + const htmlMessage = error.jqXHR?.responseJSON.html_message; + if (htmlMessage) { + this.dialog.alert({ + message: htmlSafe(error.jqXHR?.responseJSON.error), + }); + } else { + popupAjaxError(error); + } } }); }, diff --git a/app/assets/javascripts/discourse/tests/acceptance/admin-user-index-test.js b/app/assets/javascripts/discourse/tests/acceptance/admin-user-index-test.js index 8e14be163d4..c36888778bc 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/admin-user-index-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/admin-user-index-test.js @@ -123,6 +123,27 @@ acceptance("Admin - User Index", function (needs) { return helper.response({}); }); + + server.get("/admin/users/6.json", () => { + return helper.response({ + id: 6, + username: "user6", + name: null, + avatar_template: "/letter_avatar_proxy/v4/letter/b/f0a364/{size}.png", + active: true, + admin: false, + moderator: false, + can_grant_admin: true, + can_revoke_admin: false, + }); + }); + + server.put("/admin/users/6/grant_admin", () => { + return helper.response(403, { + error: "A message with bold text.", + html_message: true, + }); + }); }); needs.hooks.beforeEach(() => { @@ -222,7 +243,7 @@ acceptance("Admin - User Index", function (needs) { ); }); - test("grant admin - shows the confirmation bootbox", async function (assert) { + test("grant admin - shows the confirmation dialog", async function (assert) { await visit("/admin/users/3/user1"); await click(".grant-admin"); assert.ok(exists(".dialog-content")); @@ -234,6 +255,17 @@ acceptance("Admin - User Index", function (needs) { await click(".dialog-footer .btn-primary"); }); + test("grant admin - optionally allows HTML to be shown in the confirmation dialog", async function (assert) { + await visit("/admin/users/6/user6"); + await click(".grant-admin"); + assert.ok(exists(".dialog-content")); + + assert.ok( + exists(".dialog-content .dialog-body strong"), + "HTML is rendered in the dialog" + ); + }); + test("grant admin - redirects to the 2fa page", async function (assert) { await visit("/admin/users/4/user2"); await click(".grant-admin");