From dcaaa430f046a3c65b5c278d5c7f23b1092fe33e Mon Sep 17 00:00:00 2001 From: Nikolay Nikolaev Date: Thu, 30 Mar 2023 15:23:16 +0300 Subject: [PATCH 1/4] support `/users//invite/resend` admin api --- src/api/admin.rs | 16 +++++++++++++++- src/static/scripts/admin_users.js | 21 +++++++++++++++++++++ src/static/templates/admin/users.hbs | 3 +++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index 0fd7c2cf..9c3be7d8 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -53,7 +53,8 @@ pub fn routes() -> Vec { organizations_overview, delete_organization, diagnostics, - get_diagnostics_config + get_diagnostics_config, + resend_user_invite, ] } @@ -435,6 +436,19 @@ async fn remove_2fa(uuid: String, _token: AdminToken, mut conn: DbConn) -> Empty user.save(&mut conn).await } +#[post("/users//invite/resend")] +async fn resend_user_invite(uuid: String, _token: AdminToken, mut conn: DbConn) -> EmptyResult { + if let Some(user) = User::find_by_uuid(&uuid,&mut conn).await { + if CONFIG.mail_enabled() { + mail::send_invite(&user.email, &user.uuid, None, None, &CONFIG.invitation_org_name(), None).await + } else { + Ok(()) + } + } else { + err_code!("User doesn't exist", Status::NotFound.code); + } +} + #[derive(Deserialize, Debug)] struct UserOrgTypeData { user_type: NumberOrString, diff --git a/src/static/scripts/admin_users.js b/src/static/scripts/admin_users.js index 444bc77c..0eac278c 100644 --- a/src/static/scripts/admin_users.js +++ b/src/static/scripts/admin_users.js @@ -120,6 +120,24 @@ function inviteUser(event) { ); } +function resendUserInvite (event) { + event.preventDefault(); + event.stopPropagation(); + const id = event.target.parentNode.dataset.vwUserUuid; + const email = event.target.parentNode.dataset.vwUserEmail; + if (!id) { + alert("Required parameters not found!"); + return false; + } + const confirmed = confirm(`Are you sure you want to resend invitation for "${email}"?`); + if (confirmed) { + _post(`${BASE_URL}/admin/users/${id}/invite/resend`, + "Invite sent successfully", + "Error resend invite" + ); + } +} + const ORG_TYPES = { "0": { "name": "Owner", @@ -228,6 +246,9 @@ function initUserTable() { document.querySelectorAll("button[vw-enable-user]").forEach(btn => { btn.addEventListener("click", enableUser); }); + document.querySelectorAll("button[vw-resend-user-invite]").forEach(btn => { + btn.addEventListener("click", resendUserInvite); + }); if (jdenticon) { jdenticon(); diff --git a/src/static/templates/admin/users.hbs b/src/static/templates/admin/users.hbs index 9d9c684d..bdb2870f 100644 --- a/src/static/templates/admin/users.hbs +++ b/src/static/templates/admin/users.hbs @@ -72,6 +72,9 @@ {{else}}
{{/if}} + {{#case _Status 1}} +
+ {{/case}} From 0c085d21ce66425ce0c41a81ecbc8d9119e64623 Mon Sep 17 00:00:00 2001 From: Nikolay Nikolaev Date: Thu, 30 Mar 2023 16:04:35 +0300 Subject: [PATCH 2/4] fmt --- src/api/admin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index 9c3be7d8..7034274a 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -438,7 +438,7 @@ async fn remove_2fa(uuid: String, _token: AdminToken, mut conn: DbConn) -> Empty #[post("/users//invite/resend")] async fn resend_user_invite(uuid: String, _token: AdminToken, mut conn: DbConn) -> EmptyResult { - if let Some(user) = User::find_by_uuid(&uuid,&mut conn).await { + if let Some(user) = User::find_by_uuid(&uuid, &mut conn).await { if CONFIG.mail_enabled() { mail::send_invite(&user.email, &user.uuid, None, None, &CONFIG.invitation_org_name(), None).await } else { From 379f885354cf5cec58fd730e4c2738b46ec8474a Mon Sep 17 00:00:00 2001 From: Nikolay Nikolaev Date: Fri, 31 Mar 2023 13:00:57 +0300 Subject: [PATCH 3/4] add mail check --- src/static/scripts/admin_users.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/static/scripts/admin_users.js b/src/static/scripts/admin_users.js index 0eac278c..65815dd3 100644 --- a/src/static/scripts/admin_users.js +++ b/src/static/scripts/admin_users.js @@ -125,7 +125,7 @@ function resendUserInvite (event) { event.stopPropagation(); const id = event.target.parentNode.dataset.vwUserUuid; const email = event.target.parentNode.dataset.vwUserEmail; - if (!id) { + if (!id || !email) { alert("Required parameters not found!"); return false; } From aa6f774f6518c3caa623ecb9a2f739c649f7b7d1 Mon Sep 17 00:00:00 2001 From: Nikolay Nikolaev Date: Fri, 31 Mar 2023 14:03:37 +0300 Subject: [PATCH 4/4] add check user state --- src/api/admin.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/api/admin.rs b/src/api/admin.rs index 7034274a..0c56d0fa 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -439,6 +439,11 @@ async fn remove_2fa(uuid: String, _token: AdminToken, mut conn: DbConn) -> Empty #[post("/users//invite/resend")] async fn resend_user_invite(uuid: String, _token: AdminToken, mut conn: DbConn) -> EmptyResult { if let Some(user) = User::find_by_uuid(&uuid, &mut conn).await { + //TODO: replace this with user.status check when it will be available (PR#3397) + if !user.password_hash.is_empty() { + err_code!("User already accepted invitation", Status::BadRequest.code); + } + if CONFIG.mail_enabled() { mail::send_invite(&user.email, &user.uuid, None, None, &CONFIG.invitation_org_name(), None).await } else {