2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-06-21 11:11:30 +02:00
|
|
|
import render_settings_resend_invite_modal from "../templates/confirm_dialog/confirm_resend_invite.hbs";
|
2021-06-21 11:15:47 +02:00
|
|
|
import render_settings_revoke_invite_modal from "../templates/confirm_dialog/confirm_revoke_invite.hbs";
|
2021-04-22 18:34:53 +02:00
|
|
|
import render_admin_invites_list from "../templates/settings/admin_invites_list.hbs";
|
2021-02-28 01:22:20 +01:00
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 01:22:20 +01:00
|
|
|
import * as channel from "./channel";
|
2021-05-26 09:37:20 +02:00
|
|
|
import * as confirm_dialog from "./confirm_dialog";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-02-28 01:22:20 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
|
|
|
import * as loading from "./loading";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:22:20 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as settings_config from "./settings_config";
|
2023-01-24 08:36:05 +01:00
|
|
|
import * as settings_data from "./settings_data";
|
2021-02-28 01:22:20 +01:00
|
|
|
import * as timerender from "./timerender";
|
|
|
|
import * as ui_report from "./ui_report";
|
|
|
|
import * as util from "./util";
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2017-10-21 03:15:12 +02:00
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2021-02-28 01:22:20 +01:00
|
|
|
export function reset() {
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.loaded = false;
|
2021-02-28 01:22:20 +01:00
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
|
|
|
|
function failed_listing_invites(xhr) {
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#admin_page_invites_loading_indicator"));
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.error(
|
|
|
|
$t_html({defaultMessage: "Error listing invites"}),
|
|
|
|
xhr,
|
|
|
|
$("#invites-field-status"),
|
|
|
|
);
|
2017-10-21 03:15:12 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 16:59:21 +01:00
|
|
|
function add_invited_as_text(invites) {
|
2020-02-09 05:08:58 +01:00
|
|
|
for (const data of invites) {
|
2020-06-23 15:32:18 +02:00
|
|
|
data.invited_as_text = settings_config.user_role_map.get(data.invited_as);
|
2020-02-09 05:08:58 +01:00
|
|
|
}
|
2019-01-03 16:59:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 20:06:01 +02:00
|
|
|
function sort_invitee(a, b) {
|
|
|
|
// multi-invite links don't have an email field,
|
|
|
|
// so we set them to empty strings to let them
|
|
|
|
// sort to the top
|
2020-07-15 01:29:15 +02:00
|
|
|
const str1 = (a.email || "").toUpperCase();
|
|
|
|
const str2 = (b.email || "").toUpperCase();
|
2020-04-11 20:06:01 +02:00
|
|
|
|
|
|
|
return util.strcmp(str1, str2);
|
|
|
|
}
|
2019-01-03 16:59:21 +01:00
|
|
|
|
2017-10-21 03:15:12 +02:00
|
|
|
function populate_invites(invites_data) {
|
|
|
|
if (!meta.loaded) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-03 16:59:21 +01:00
|
|
|
|
|
|
|
add_invited_as_text(invites_data.invites);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $invites_table = $("#admin_invites_table").expectOne();
|
|
|
|
ListWidget.create($invites_table, invites_data.invites, {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "admin_invites_list",
|
2023-05-01 13:44:40 +02:00
|
|
|
get_item: ListWidget.default_get_item,
|
2020-07-20 22:18:43 +02:00
|
|
|
modifier(item) {
|
2020-04-11 15:17:26 +02:00
|
|
|
item.invited_absolute_time = timerender.absolute_time(item.invited * 1000);
|
2021-12-01 12:10:58 +01:00
|
|
|
if (item.expiry_date !== null) {
|
|
|
|
item.expiry_date_absolute_time = timerender.absolute_time(item.expiry_date * 1000);
|
|
|
|
}
|
2020-04-30 21:41:21 +02:00
|
|
|
item.is_admin = page_params.is_admin;
|
2020-07-15 00:34:28 +02:00
|
|
|
item.disable_buttons =
|
|
|
|
item.invited_as === settings_config.user_role_values.owner.code &&
|
|
|
|
!page_params.is_owner;
|
2020-07-03 22:03:39 +02:00
|
|
|
item.referrer_name = people.get_by_user_id(item.invited_by_user_id).full_name;
|
2020-07-16 22:40:18 +02:00
|
|
|
return render_admin_invites_list({invite: item});
|
2020-04-11 15:17:26 +02:00
|
|
|
},
|
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $invites_table.closest(".settings-section").find(".search"),
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate(item, value) {
|
2020-07-03 22:03:39 +02:00
|
|
|
const referrer = people.get_by_user_id(item.invited_by_user_id);
|
|
|
|
const referrer_email = referrer.email;
|
|
|
|
const referrer_name = referrer.full_name;
|
|
|
|
const referrer_name_matched = referrer_name.toLowerCase().includes(value);
|
2020-06-26 21:26:57 +02:00
|
|
|
const referrer_email_matched = referrer_email.toLowerCase().includes(value);
|
2020-04-11 15:17:26 +02:00
|
|
|
if (item.is_multiuse) {
|
2020-07-03 22:03:39 +02:00
|
|
|
return referrer_name_matched || referrer_email_matched;
|
2020-04-11 15:17:26 +02:00
|
|
|
}
|
|
|
|
const invitee_email_matched = item.email.toLowerCase().includes(value);
|
2020-07-03 22:03:39 +02:00
|
|
|
return referrer_email_matched || referrer_name_matched || invitee_email_matched;
|
2017-10-21 03:15:12 +02:00
|
|
|
},
|
2020-04-11 15:17:26 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $("#admin-invites-list").expectOne(),
|
2023-05-03 07:06:19 +02:00
|
|
|
init_sort: sort_invitee,
|
2020-04-11 16:23:29 +02:00
|
|
|
sort_fields: {
|
|
|
|
invitee: sort_invitee,
|
2023-05-03 07:06:19 +02:00
|
|
|
...ListWidget.generic_sort_functions("alphabetic", ["ref"]),
|
|
|
|
...ListWidget.generic_sort_functions("numeric", [
|
|
|
|
"invited",
|
|
|
|
"expires_at",
|
|
|
|
"invited_as",
|
|
|
|
]),
|
2020-04-11 16:23:29 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $("#admin-invites-list .progressive-table-wrapper"),
|
2020-04-11 15:17:26 +02:00
|
|
|
});
|
2019-08-22 07:34:19 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#admin_page_invites_loading_indicator"));
|
2017-10-21 03:15:12 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 23:12:41 +01:00
|
|
|
function do_revoke_invite() {
|
2021-07-05 12:41:37 +02:00
|
|
|
const modal_invite_id = $(".dialog_submit_button").attr("data-invite-id");
|
|
|
|
const modal_is_multiuse = $(".dialog_submit_button").attr("data-is-multiuse");
|
2022-01-25 11:36:19 +01:00
|
|
|
const $revoke_button = meta.$current_revoke_invite_user_modal_row.find("button.revoke");
|
2019-02-15 23:12:41 +01:00
|
|
|
|
2019-02-15 19:09:25 +01:00
|
|
|
if (modal_invite_id !== meta.invite_id || modal_is_multiuse !== meta.is_multiuse) {
|
2019-02-15 23:12:41 +01:00
|
|
|
blueslip.error("Invite revoking canceled due to non-matching fields.");
|
2021-01-27 08:16:28 +01:00
|
|
|
ui_report.client_error(
|
2021-04-13 05:18:25 +02:00
|
|
|
$t_html({
|
|
|
|
defaultMessage: "Resending encountered an error. Please reload and try again.",
|
|
|
|
}),
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#home-error"),
|
|
|
|
);
|
2019-02-15 23:12:41 +01:00
|
|
|
}
|
2021-05-26 09:37:20 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$revoke_button.prop("disabled", true).text($t({defaultMessage: "Working…"}));
|
2020-07-15 01:29:15 +02:00
|
|
|
let url = "/json/invites/" + meta.invite_id;
|
2019-02-15 19:09:25 +01:00
|
|
|
|
|
|
|
if (modal_is_multiuse === "true") {
|
2020-07-15 01:29:15 +02:00
|
|
|
url = "/json/invites/multiuse/" + meta.invite_id;
|
2019-02-15 19:09:25 +01:00
|
|
|
}
|
2019-02-15 23:12:41 +01:00
|
|
|
channel.del({
|
2020-07-20 22:18:43 +02:00
|
|
|
url,
|
|
|
|
error(xhr) {
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.generic_row_button_error(xhr, $revoke_button);
|
2019-02-15 23:12:41 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2022-01-25 11:36:19 +01:00
|
|
|
meta.$current_revoke_invite_user_modal_row.remove();
|
2019-02-15 23:12:41 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
|
2021-05-26 12:30:18 +02:00
|
|
|
function do_resend_invite() {
|
2021-07-05 12:41:37 +02:00
|
|
|
const modal_invite_id = $(".dialog_submit_button").attr("data-invite-id");
|
2022-01-25 11:36:19 +01:00
|
|
|
const $resend_button = meta.$current_resend_invite_user_modal_row.find("button.resend");
|
2021-05-26 12:30:18 +02:00
|
|
|
|
|
|
|
if (modal_invite_id !== meta.invite_id) {
|
|
|
|
blueslip.error("Invite resending canceled due to non-matching fields.");
|
|
|
|
ui_report.client_error(
|
|
|
|
$t_html({
|
|
|
|
defaultMessage: "Resending encountered an error. Please reload and try again.",
|
|
|
|
}),
|
|
|
|
$("#home-error"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$resend_button.prop("disabled", true).text($t({defaultMessage: "Working…"}));
|
2021-05-26 12:30:18 +02:00
|
|
|
channel.post({
|
|
|
|
url: "/json/invites/" + meta.invite_id + "/resend",
|
|
|
|
error(xhr) {
|
2022-01-25 11:36:19 +01:00
|
|
|
ui_report.generic_row_button_error(xhr, $resend_button);
|
2021-05-26 12:30:18 +02:00
|
|
|
},
|
|
|
|
success(data) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$resend_button.text($t({defaultMessage: "Sent!"}));
|
|
|
|
$resend_button.removeClass("resend btn-warning").addClass("sea-green");
|
2021-05-26 12:30:18 +02:00
|
|
|
data.timestamp = timerender.absolute_time(data.timestamp * 1000);
|
2022-01-25 11:36:19 +01:00
|
|
|
meta.$current_resend_invite_user_modal_row.find(".invited_at").text(data.timestamp);
|
2021-05-26 12:30:18 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:14:12 +01:00
|
|
|
export function set_up(initialize_event_handlers = true) {
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.loaded = true;
|
|
|
|
|
|
|
|
// create loading indicators
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.make_indicator($("#admin_page_invites_loading_indicator"));
|
2017-10-21 03:15:12 +02:00
|
|
|
|
|
|
|
// Populate invites table
|
|
|
|
channel.get({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/invites",
|
2018-12-18 19:34:45 +01:00
|
|
|
timeout: 10 * 1000,
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2021-02-28 01:22:20 +01:00
|
|
|
on_load_success(data, initialize_event_handlers);
|
2019-02-15 19:09:25 +01:00
|
|
|
},
|
2017-10-21 03:15:12 +02:00
|
|
|
error: failed_listing_invites,
|
|
|
|
});
|
2021-02-28 01:22:20 +01:00
|
|
|
}
|
2017-10-21 03:15:12 +02:00
|
|
|
|
2021-02-28 01:22:20 +01:00
|
|
|
export function on_load_success(invites_data, initialize_event_handlers) {
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.loaded = true;
|
|
|
|
populate_invites(invites_data);
|
2019-02-15 19:09:25 +01:00
|
|
|
if (!initialize_event_handlers) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-02 01:45:54 +02:00
|
|
|
$(".admin_invites_table").on("click", ".revoke", (e) => {
|
2018-05-23 22:29:00 +02:00
|
|
|
// This click event must not get propagated to parent container otherwise the modal
|
|
|
|
// will not show up because of a call to `close_active_modal` in `settings.js`.
|
2017-10-21 03:15:12 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $(e.target).closest(".invite_row");
|
|
|
|
const email = $row.find(".email").text();
|
|
|
|
const referred_by = $row.find(".referred_by").text();
|
|
|
|
meta.$current_revoke_invite_user_modal_row = $row;
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.invite_id = $(e.currentTarget).attr("data-invite-id");
|
2019-02-15 19:09:25 +01:00
|
|
|
meta.is_multiuse = $(e.currentTarget).attr("data-is-multiuse");
|
2020-07-15 00:34:28 +02:00
|
|
|
const ctx = {
|
|
|
|
is_multiuse: meta.is_multiuse === "true",
|
2020-07-20 22:18:43 +02:00
|
|
|
email,
|
|
|
|
referred_by,
|
2020-07-15 00:34:28 +02:00
|
|
|
};
|
2021-05-26 09:37:20 +02:00
|
|
|
const html_body = render_settings_revoke_invite_modal(ctx);
|
|
|
|
|
|
|
|
confirm_dialog.launch({
|
|
|
|
html_heading: ctx.is_multiuse
|
|
|
|
? $t_html({defaultMessage: "Revoke invitation link"})
|
|
|
|
: $t_html({defaultMessage: "Revoke invitation to {email}"}, {email}),
|
|
|
|
html_body,
|
|
|
|
on_click: do_revoke_invite,
|
|
|
|
});
|
|
|
|
|
2021-07-05 12:41:37 +02:00
|
|
|
$(".dialog_submit_button").attr("data-invite-id", meta.invite_id);
|
|
|
|
$(".dialog_submit_button").attr("data-is-multiuse", meta.is_multiuse);
|
2017-10-21 03:15:12 +02:00
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$(".admin_invites_table").on("click", ".resend", (e) => {
|
2018-05-23 22:29:00 +02:00
|
|
|
// This click event must not get propagated to parent container otherwise the modal
|
|
|
|
// will not show up because of a call to `close_active_modal` in `settings.js`.
|
2017-10-21 03:15:12 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $row = $(e.target).closest(".invite_row");
|
|
|
|
const email = $row.find(".email").text();
|
|
|
|
meta.$current_resend_invite_user_modal_row = $row;
|
2017-10-21 03:15:12 +02:00
|
|
|
meta.invite_id = $(e.currentTarget).attr("data-invite-id");
|
2021-05-26 12:30:18 +02:00
|
|
|
const html_body = render_settings_resend_invite_modal({email});
|
2017-10-21 03:15:12 +02:00
|
|
|
|
2021-05-26 12:30:18 +02:00
|
|
|
confirm_dialog.launch({
|
2021-05-28 10:26:32 +02:00
|
|
|
html_heading: $t_html({defaultMessage: "Resend invitation"}),
|
2021-05-26 12:30:18 +02:00
|
|
|
html_body,
|
|
|
|
on_click: do_resend_invite,
|
2017-10-21 03:15:12 +02:00
|
|
|
});
|
2021-05-26 12:30:18 +02:00
|
|
|
|
2021-07-05 12:41:37 +02:00
|
|
|
$(".dialog_submit_button").attr("data-invite-id", meta.invite_id);
|
2017-10-21 03:15:12 +02:00
|
|
|
});
|
2021-02-28 01:22:20 +01:00
|
|
|
}
|
2023-01-24 08:36:05 +01:00
|
|
|
|
|
|
|
export function update_invite_users_setting_tip() {
|
2023-06-26 23:38:08 +02:00
|
|
|
if (settings_data.user_can_invite_users_by_email() && !page_params.is_admin) {
|
2023-01-24 08:36:05 +01:00
|
|
|
$(".invite-user-settings-tip").hide();
|
|
|
|
return;
|
|
|
|
}
|
2023-06-26 23:38:08 +02:00
|
|
|
const permission_type = settings_config.email_invite_to_realm_policy_values;
|
2023-01-24 08:36:05 +01:00
|
|
|
const current_permission = page_params.realm_invite_to_realm_policy;
|
|
|
|
let tip_text;
|
|
|
|
switch (current_permission) {
|
|
|
|
case permission_type.by_admins_only.code: {
|
|
|
|
tip_text = $t({
|
|
|
|
defaultMessage:
|
|
|
|
"This organization is configured so that admins can invite users to this organization.",
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case permission_type.by_moderators_only.code: {
|
|
|
|
tip_text = $t({
|
|
|
|
defaultMessage:
|
|
|
|
"This organization is configured so that admins and moderators can invite users to this organization.",
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case permission_type.by_members.code: {
|
|
|
|
tip_text = $t({
|
|
|
|
defaultMessage:
|
|
|
|
"This organization is configured so that admins, moderators and members can invite users to this organization.",
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case permission_type.by_full_members.code: {
|
|
|
|
tip_text = $t({
|
|
|
|
defaultMessage:
|
|
|
|
"This organization is configured so that admins, moderators and full members can invite users to this organization.",
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
tip_text = $t({
|
|
|
|
defaultMessage:
|
|
|
|
"This organization is configured so that nobody can invite users to this organization.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$(".invite-user-settings-tip").show();
|
|
|
|
$(".invite-user-settings-tip").text(tip_text);
|
|
|
|
}
|
2023-06-13 09:47:53 +02:00
|
|
|
|
|
|
|
export function update_invite_user_panel() {
|
|
|
|
update_invite_users_setting_tip();
|
2023-08-03 16:06:40 +02:00
|
|
|
if (
|
|
|
|
!settings_data.user_can_invite_users_by_email() &&
|
|
|
|
!settings_data.user_can_create_multiuse_invite()
|
|
|
|
) {
|
2023-06-13 09:47:53 +02:00
|
|
|
$("#admin-invites-list .invite-user-link").hide();
|
|
|
|
} else {
|
|
|
|
$("#admin-invites-list .invite-user-link").show();
|
|
|
|
}
|
|
|
|
}
|