2017-04-08 20:08:35 +02:00
|
|
|
var settings_users = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var meta = {
|
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2017-04-17 16:51:27 +02:00
|
|
|
exports.reset = function () {
|
|
|
|
meta.loaded = false;
|
|
|
|
};
|
|
|
|
|
2018-07-14 12:43:04 +02:00
|
|
|
function get_user_info_row(user_id) {
|
|
|
|
return $("tr.user_row[data-user-id='" + user_id + "']");
|
2017-04-08 20:08:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function update_view_on_deactivate(row) {
|
|
|
|
var button = row.find("button.deactivate");
|
|
|
|
row.find('button.open-user-form').hide();
|
|
|
|
button.addClass("btn-warning");
|
|
|
|
button.removeClass("btn-danger");
|
|
|
|
button.addClass("reactivate");
|
|
|
|
button.removeClass("deactivate");
|
|
|
|
button.text(i18n.t("Reactivate"));
|
|
|
|
row.addClass("deactivated_user");
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_view_on_reactivate(row) {
|
|
|
|
row.find(".user-admin-settings").show();
|
|
|
|
var button = row.find("button.reactivate");
|
|
|
|
row.find("button.open-user-form").show();
|
|
|
|
button.addClass("btn-danger");
|
|
|
|
button.removeClass("btn-warning");
|
|
|
|
button.addClass("deactivate");
|
|
|
|
button.removeClass("reactivate");
|
|
|
|
button.text(i18n.t("Deactivate"));
|
|
|
|
row.removeClass("deactivated_user");
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.update_user_data = function (user_id, new_data) {
|
|
|
|
if (!meta.loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-14 12:43:04 +02:00
|
|
|
var user_row = get_user_info_row(user_id);
|
2017-04-08 20:08:35 +02:00
|
|
|
|
|
|
|
if (new_data.full_name !== undefined) {
|
|
|
|
// Update the full name in the table
|
|
|
|
user_row.find(".user_name").text(new_data.full_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_data.owner !== undefined) {
|
|
|
|
// Update the bot owner in the table
|
|
|
|
user_row.find(".owner").text(new_data.owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_data.is_active !== undefined) {
|
|
|
|
if (new_data.is_active === false) {
|
|
|
|
// Deactivate the bot in the table
|
|
|
|
update_view_on_deactivate(user_row);
|
|
|
|
} else {
|
|
|
|
// Reactivate the bot in the table
|
|
|
|
update_view_on_reactivate(user_row);
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 16:09:12 +02:00
|
|
|
|
|
|
|
if (new_data.is_admin !== undefined) {
|
|
|
|
if (new_data.is_admin) {
|
|
|
|
user_row.find('#admin_icon').show();
|
|
|
|
} else {
|
|
|
|
user_row.find('#admin_icon').hide();
|
|
|
|
}
|
|
|
|
}
|
2017-04-08 20:08:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function failed_listing_users(xhr) {
|
|
|
|
loading.destroy_indicator($('#subs_page_loading_indicator'));
|
|
|
|
ui_report.error(i18n.t("Error listing users or bots"), xhr, $("#organization-status"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function populate_users(realm_people_data) {
|
|
|
|
var active_users = [];
|
|
|
|
var deactivated_users = [];
|
|
|
|
var bots = [];
|
|
|
|
_.each(realm_people_data.members, function (user) {
|
|
|
|
user.is_active_human = user.is_active && !user.is_bot;
|
|
|
|
if (user.is_bot) {
|
2017-06-10 15:29:01 +02:00
|
|
|
// Convert bot type id to string for viewing to the users.
|
|
|
|
user.bot_type = settings_bots.type_id_to_string(user.bot_type);
|
2017-04-08 20:08:35 +02:00
|
|
|
bots.push(user);
|
|
|
|
} else if (user.is_active) {
|
|
|
|
active_users.push(user);
|
|
|
|
} else {
|
|
|
|
deactivated_users.push(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
active_users = _.sortBy(active_users, 'full_name');
|
|
|
|
deactivated_users = _.sortBy(deactivated_users, 'full_name');
|
|
|
|
bots = _.sortBy(bots, 'full_name');
|
|
|
|
|
2017-09-26 22:16:52 +02:00
|
|
|
var update_scrollbar = function ($sel) {
|
|
|
|
return function () {
|
|
|
|
ui.update_scrollbar($sel);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-04-20 21:58:12 +02:00
|
|
|
var $bots_table = $("#admin_bots_table");
|
2018-06-20 18:32:39 +02:00
|
|
|
list_render.create($bots_table, bots, {
|
2017-04-20 21:58:12 +02:00
|
|
|
name: "admin_bot_list",
|
|
|
|
modifier: function (item) {
|
|
|
|
return templates.render("admin_user_list", { user: item, can_modify: page_params.is_admin });
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: $bots_table.closest(".settings-section").find(".search"),
|
|
|
|
callback: function (item, value) {
|
|
|
|
return (
|
2017-08-25 03:30:54 +02:00
|
|
|
item.full_name.toLowerCase().indexOf(value) >= 0 ||
|
|
|
|
item.email.toLowerCase().indexOf(value) >= 0
|
2017-04-20 21:58:12 +02:00
|
|
|
);
|
|
|
|
},
|
2017-09-26 22:16:52 +02:00
|
|
|
onupdate: update_scrollbar($bots_table),
|
2017-04-20 21:58:12 +02:00
|
|
|
},
|
|
|
|
}).init();
|
2017-04-08 20:08:35 +02:00
|
|
|
|
2017-04-20 21:58:46 +02:00
|
|
|
var $users_table = $("#admin_users_table");
|
2018-06-20 18:32:39 +02:00
|
|
|
list_render.create($users_table, active_users, {
|
2017-04-20 21:58:46 +02:00
|
|
|
name: "users_table_list",
|
|
|
|
modifier: function (item) {
|
|
|
|
var activity_rendered;
|
2017-05-18 21:18:11 +02:00
|
|
|
var today = new XDate();
|
2017-04-20 21:58:46 +02:00
|
|
|
if (people.is_current_user(item.email)) {
|
2017-05-18 21:18:11 +02:00
|
|
|
activity_rendered = timerender.render_date(today, undefined, today);
|
2017-04-20 21:58:46 +02:00
|
|
|
} else if (presence.presence_info[item.user_id]) {
|
|
|
|
// XDate takes number of milliseconds since UTC epoch.
|
|
|
|
var last_active = presence.presence_info[item.user_id].last_active * 1000;
|
2017-10-23 21:48:10 +02:00
|
|
|
|
|
|
|
if (!isNaN(last_active)) {
|
|
|
|
var last_active_date = new XDate(last_active);
|
|
|
|
activity_rendered = timerender.render_date(last_active_date, undefined, today);
|
|
|
|
} else {
|
|
|
|
activity_rendered = $("<span></span>").text(i18n.t("Never"));
|
|
|
|
}
|
2017-04-08 20:08:35 +02:00
|
|
|
} else {
|
|
|
|
activity_rendered = $("<span></span>").text(i18n.t("Unknown"));
|
|
|
|
}
|
2017-04-20 21:58:46 +02:00
|
|
|
|
|
|
|
var $row = $(templates.render("admin_user_list", {user: item, can_modify: page_params.is_admin}));
|
|
|
|
|
|
|
|
$row.find(".last_active").append(activity_rendered);
|
|
|
|
|
|
|
|
return $row;
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: $users_table.closest(".settings-section").find(".search"),
|
|
|
|
callback: function (item, value) {
|
|
|
|
return (
|
2017-08-25 03:30:54 +02:00
|
|
|
item.full_name.toLowerCase().indexOf(value) >= 0 ||
|
|
|
|
item.email.toLowerCase().indexOf(value) >= 0
|
2017-04-20 21:58:46 +02:00
|
|
|
);
|
|
|
|
},
|
2017-09-26 22:16:52 +02:00
|
|
|
onupdate: update_scrollbar($users_table),
|
2017-04-20 21:58:46 +02:00
|
|
|
},
|
|
|
|
}).init();
|
2017-04-08 20:08:35 +02:00
|
|
|
|
2017-04-20 21:47:14 +02:00
|
|
|
var $deactivated_users_table = $("#admin_deactivated_users_table");
|
2018-06-20 18:32:39 +02:00
|
|
|
list_render.create($deactivated_users_table, deactivated_users, {
|
2017-04-20 21:47:14 +02:00
|
|
|
name: "deactivated_users_table_list",
|
|
|
|
modifier: function (item) {
|
|
|
|
return templates.render("admin_user_list", { user: item, can_modify: page_params.is_admin });
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: $deactivated_users_table.closest(".settings-section").find(".search"),
|
|
|
|
callback: function (item, value) {
|
|
|
|
return (
|
2017-08-25 03:30:54 +02:00
|
|
|
item.full_name.toLowerCase().indexOf(value) >= 0 ||
|
|
|
|
item.email.toLowerCase().indexOf(value) >= 0
|
2017-04-20 21:47:14 +02:00
|
|
|
);
|
|
|
|
},
|
2017-09-26 22:16:52 +02:00
|
|
|
onupdate: update_scrollbar($deactivated_users_table),
|
2017-04-20 21:47:14 +02:00
|
|
|
},
|
|
|
|
}).init();
|
2017-09-26 22:16:52 +02:00
|
|
|
|
|
|
|
[$bots_table, $users_table, $deactivated_users_table].forEach(function ($o) {
|
|
|
|
ui.set_up_scrollbar($o.closest(".progressive-table-wrapper"));
|
|
|
|
});
|
|
|
|
|
2017-04-08 20:08:35 +02:00
|
|
|
loading.destroy_indicator($('#admin_page_users_loading_indicator'));
|
|
|
|
loading.destroy_indicator($('#admin_page_bots_loading_indicator'));
|
|
|
|
loading.destroy_indicator($('#admin_page_deactivated_users_loading_indicator'));
|
2018-03-03 08:33:30 +01:00
|
|
|
$("#admin_deactivated_users_table").show();
|
|
|
|
$("#admin_users_table").show();
|
|
|
|
$("#admin_bots_table").show();
|
2017-04-08 20:08:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.set_up = function () {
|
2018-02-28 19:29:34 +01:00
|
|
|
loading.make_indicator($('#admin_page_users_loading_indicator'), {text: 'Loading...'});
|
|
|
|
loading.make_indicator($('#admin_page_bots_loading_indicator'), {text: 'Loading...'});
|
|
|
|
loading.make_indicator($('#admin_page_deactivated_users_loading_indicator'), {text: 'Loading...'});
|
2018-03-03 08:33:30 +01:00
|
|
|
$("#admin_deactivated_users_table").hide();
|
|
|
|
$("#admin_users_table").hide();
|
|
|
|
$("#admin_bots_table").hide();
|
2017-04-08 20:08:35 +02:00
|
|
|
|
|
|
|
// Populate users and bots tables
|
|
|
|
channel.get({
|
|
|
|
url: '/json/users',
|
|
|
|
idempotent: true,
|
2018-06-04 21:13:07 +02:00
|
|
|
timeout: 10 * 1000,
|
2017-04-08 20:08:35 +02:00
|
|
|
success: exports.on_load_success,
|
|
|
|
error: failed_listing_users,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.on_load_success = function (realm_people_data) {
|
|
|
|
meta.loaded = true;
|
|
|
|
|
|
|
|
populate_users(realm_people_data);
|
|
|
|
|
|
|
|
// Setup click handlers
|
|
|
|
$(".admin_user_table").on("click", ".deactivate", function (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-04-08 20:08:35 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
var row = $(e.target).closest(".user_row");
|
|
|
|
|
|
|
|
var user_name = row.find('.user_name').text();
|
2018-05-17 22:22:32 +02:00
|
|
|
var email = row.attr("data-email");
|
2017-04-08 20:08:35 +02:00
|
|
|
|
|
|
|
$("#deactivation_user_modal .email").text(email);
|
|
|
|
$("#deactivation_user_modal .user_name").text(user_name);
|
|
|
|
$("#deactivation_user_modal").modal("show");
|
|
|
|
|
|
|
|
meta.current_deactivate_user_modal_row = row;
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#do_deactivate_user_button").expectOne().click(function () {
|
2018-05-17 19:36:33 +02:00
|
|
|
var email = meta.current_deactivate_user_modal_row.attr("data-email");
|
|
|
|
var user_id = meta.current_deactivate_user_modal_row.attr("data-user-id");
|
2017-04-08 20:08:35 +02:00
|
|
|
|
|
|
|
if ($("#deactivation_user_modal .email").html() !== email) {
|
|
|
|
blueslip.error("User deactivation canceled due to non-matching fields.");
|
2017-11-04 15:42:38 +01:00
|
|
|
ui_report.message(i18n.t("Deactivation encountered an error. Please reload and try again."),
|
2018-05-06 21:43:17 +02:00
|
|
|
$("#home-error"), 'alert-error');
|
2017-04-08 20:08:35 +02:00
|
|
|
}
|
|
|
|
$("#deactivation_user_modal").modal("hide");
|
|
|
|
meta.current_deactivate_user_modal_row.find("button").eq(0).prop("disabled", true).text(i18n.t("Working…"));
|
|
|
|
channel.del({
|
2018-05-17 19:36:33 +02:00
|
|
|
url: '/json/users/' + encodeURIComponent(user_id),
|
2017-04-08 20:08:35 +02:00
|
|
|
error: function (xhr) {
|
2018-02-04 06:54:31 +01:00
|
|
|
var status = $("#organization-status").expectOne();
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, status);
|
|
|
|
var button = meta.current_deactivate_user_modal_row.find("button.deactivate");
|
|
|
|
button.text(i18n.t("Deactivate"));
|
2017-04-08 20:08:35 +02:00
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
var button = meta.current_deactivate_user_modal_row.find("button.deactivate");
|
|
|
|
button.prop("disabled", false);
|
|
|
|
button.addClass("btn-warning reactivate").removeClass("btn-danger deactivate");
|
|
|
|
button.text(i18n.t("Reactivate"));
|
|
|
|
meta.current_deactivate_user_modal_row.addClass("deactivated_user");
|
|
|
|
meta.current_deactivate_user_modal_row.find('button.open-user-form').hide();
|
|
|
|
meta.current_deactivate_user_modal_row.find(".user-admin-settings").hide();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".admin_bot_table").on("click", ".deactivate", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
var row = $(e.target).closest(".user_row");
|
|
|
|
|
2018-05-15 15:26:04 +02:00
|
|
|
var bot_id = row.attr("data-user-id");
|
2017-04-08 20:08:35 +02:00
|
|
|
|
|
|
|
channel.del({
|
2018-05-15 15:26:04 +02:00
|
|
|
url: '/json/bots/' + encodeURIComponent(bot_id),
|
2017-04-08 20:08:35 +02:00
|
|
|
error: function (xhr) {
|
2018-03-25 11:12:06 +02:00
|
|
|
ui_report.generic_row_button_error(xhr, $(e.target));
|
2017-04-08 20:08:35 +02:00
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
update_view_on_deactivate(row);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".admin_user_table, .admin_bot_table").on("click", ".reactivate", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// Go up the tree until we find the user row, then grab the email element
|
|
|
|
var row = $(e.target).closest(".user_row");
|
2018-05-17 19:45:13 +02:00
|
|
|
var user_id = row.attr("data-user-id");
|
2017-04-08 20:08:35 +02:00
|
|
|
|
|
|
|
channel.post({
|
2018-05-17 19:45:13 +02:00
|
|
|
url: '/json/users/' + encodeURIComponent(user_id) + "/reactivate",
|
2017-04-08 20:08:35 +02:00
|
|
|
error: function (xhr) {
|
2018-03-25 11:12:06 +02:00
|
|
|
ui_report.generic_row_button_error(xhr, $(e.target));
|
2017-04-08 20:08:35 +02:00
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
update_view_on_reactivate(row);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-31 13:30:15 +02:00
|
|
|
function open_user_info_form_modal(person) {
|
2018-07-14 12:43:04 +02:00
|
|
|
var html = templates.render('user-info-form-modal', {
|
2018-07-31 13:30:15 +02:00
|
|
|
user_id: person.user_id,
|
|
|
|
full_name: people.get_full_name(person.user_id),
|
2018-08-01 16:27:13 +02:00
|
|
|
is_admin: person.is_admin,
|
2018-07-31 13:30:15 +02:00
|
|
|
is_bot: person.is_bot,
|
2018-07-14 12:43:04 +02:00
|
|
|
});
|
|
|
|
var user_info_form_modal = $(html);
|
|
|
|
var modal_container = $('#user-info-form-modal-container');
|
|
|
|
modal_container.empty().append(user_info_form_modal);
|
|
|
|
overlays.open_modal('user-info-form-modal');
|
|
|
|
|
2018-07-31 13:30:15 +02:00
|
|
|
if (person.is_bot) {
|
2017-04-08 20:08:35 +02:00
|
|
|
// Dynamically add the owner select control in order to
|
|
|
|
// avoid performance issues in case of large number of users.
|
2018-07-30 09:30:51 +02:00
|
|
|
var users_list = people.get_active_human_persons();
|
|
|
|
var owner_select = $(templates.render("bot_owner_select", {users_list: users_list}));
|
2018-07-31 13:30:15 +02:00
|
|
|
owner_select.val(bot_data.get(person.user_id).owner || "");
|
2018-07-14 12:43:04 +02:00
|
|
|
modal_container.find(".edit_bot_owner_container").append(owner_select);
|
2017-04-08 20:08:35 +02:00
|
|
|
}
|
2018-07-30 09:30:51 +02:00
|
|
|
|
2018-07-30 09:22:57 +02:00
|
|
|
return user_info_form_modal;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(".admin_user_table, .admin_bot_table").on("click", ".open-user-form", function (e) {
|
|
|
|
var user_id = $(e.currentTarget).attr("data-user-id");
|
|
|
|
var person = people.get_person_from_user_id(user_id);
|
|
|
|
|
|
|
|
if (!person) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-31 13:30:15 +02:00
|
|
|
var user_info_form_modal = open_user_info_form_modal(person);
|
2017-04-08 20:08:35 +02:00
|
|
|
|
2018-07-14 12:43:04 +02:00
|
|
|
var url;
|
|
|
|
var data;
|
|
|
|
var admin_status = $('#organization-status').expectOne();
|
|
|
|
var full_name = user_info_form_modal.find("input[name='full_name']");
|
2017-04-08 20:08:35 +02:00
|
|
|
|
2018-08-02 17:33:49 +02:00
|
|
|
user_info_form_modal.find('.submit_user_info_change').on("click", function (e) {
|
2017-04-08 20:08:35 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2018-08-02 17:34:20 +02:00
|
|
|
var user_role_select_value = user_info_form_modal.find('#user-role-select').val();
|
|
|
|
|
2018-04-03 03:46:53 +02:00
|
|
|
if (person.is_bot) {
|
2018-07-30 09:22:57 +02:00
|
|
|
url = "/json/bots/" + encodeURIComponent(user_id);
|
2018-04-03 03:46:53 +02:00
|
|
|
data = {
|
|
|
|
full_name: full_name.val(),
|
|
|
|
};
|
2018-07-30 09:22:57 +02:00
|
|
|
var owner_select_value = user_info_form_modal.find('.bot_owner_select').val();
|
|
|
|
if (owner_select_value) {
|
|
|
|
data.bot_owner_id = people.get_by_email(owner_select_value).user_id;
|
2018-04-03 03:46:53 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-30 09:22:57 +02:00
|
|
|
url = "/json/users/" + encodeURIComponent(user_id);
|
2018-04-03 03:46:53 +02:00
|
|
|
data = {
|
|
|
|
full_name: JSON.stringify(full_name.val()),
|
2018-08-02 17:34:20 +02:00
|
|
|
is_admin: JSON.stringify(user_role_select_value === 'admin'),
|
2018-04-03 03:46:53 +02:00
|
|
|
};
|
2017-04-08 20:08:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
channel.patch({
|
|
|
|
url: url,
|
|
|
|
data: data,
|
|
|
|
success: function () {
|
|
|
|
ui_report.success(i18n.t('Updated successfully!'), admin_status);
|
2018-07-14 12:43:04 +02:00
|
|
|
overlays.close_modal('user-info-form-modal');
|
2017-04-08 20:08:35 +02:00
|
|
|
},
|
2018-02-04 06:30:18 +01:00
|
|
|
error: function (xhr) {
|
|
|
|
ui_report.error(i18n.t('Failed'), xhr, admin_status);
|
2018-07-14 12:43:04 +02:00
|
|
|
overlays.close_modal('user-info-form-modal');
|
2017-04-08 20:08:35 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = settings_users;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.settings_users = settings_users;
|