2013-08-12 23:31:23 +02:00
|
|
|
var admin = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-11-16 17:07:55 +01:00
|
|
|
function populate_users (realm_people_data) {
|
2013-11-16 16:36:45 +01:00
|
|
|
var users_table = $("#admin_users_table");
|
2013-11-16 17:07:55 +01:00
|
|
|
var deactivated_users_table = $("#admin_deactivated_users_table");
|
2013-11-16 16:36:45 +01:00
|
|
|
var bots_table = $("#admin_bots_table");
|
2013-11-16 17:04:50 +01:00
|
|
|
// Clear table rows, but not the table headers
|
|
|
|
users_table.find("tr.user_row").remove();
|
|
|
|
deactivated_users_table.find("tr.user_row").remove();
|
|
|
|
bots_table.find("tr.user_row").remove();
|
2013-08-12 23:31:23 +02:00
|
|
|
|
2013-11-16 17:07:55 +01:00
|
|
|
var active_users = [];
|
|
|
|
var deactivated_users = [];
|
2013-11-15 20:34:23 +01:00
|
|
|
var bots = [];
|
2013-11-16 17:07:55 +01:00
|
|
|
_.each(realm_people_data.members, function (user) {
|
2013-11-15 20:34:23 +01:00
|
|
|
if (user.is_bot) {
|
|
|
|
bots.push(user);
|
2013-11-16 17:07:55 +01:00
|
|
|
} else if (user.is_active) {
|
|
|
|
active_users.push(user);
|
2013-11-15 20:34:23 +01:00
|
|
|
} else {
|
2013-11-16 17:07:55 +01:00
|
|
|
deactivated_users.push(user);
|
2013-08-12 23:31:23 +02:00
|
|
|
}
|
|
|
|
});
|
2013-11-15 20:34:23 +01:00
|
|
|
|
2013-11-16 17:07:55 +01:00
|
|
|
active_users = _.sortBy(active_users, 'full_name');
|
|
|
|
deactivated_users = _.sortBy(deactivated_users, 'full_name');
|
2013-11-15 20:34:23 +01:00
|
|
|
bots = _.sortBy(bots, 'full_name');
|
|
|
|
|
2013-11-16 16:36:45 +01:00
|
|
|
_.each(bots, function (user) {
|
|
|
|
bots_table.append(templates.render("admin_user_list", {user: user}));
|
|
|
|
});
|
2013-11-16 17:07:55 +01:00
|
|
|
_.each(active_users, function (user) {
|
2013-11-16 16:36:45 +01:00
|
|
|
users_table.append(templates.render("admin_user_list", {user: user}));
|
2013-11-15 20:34:23 +01:00
|
|
|
});
|
2013-11-16 17:07:55 +01:00
|
|
|
_.each(deactivated_users, function (user) {
|
|
|
|
deactivated_users_table.append(templates.render("admin_user_list", {user: user}));
|
|
|
|
});
|
2013-08-12 23:31:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.setup_page = function () {
|
2013-11-16 17:07:55 +01:00
|
|
|
function failed_listing(xhr, error) {
|
|
|
|
ui.report_error("Error listing streams or subscriptions", xhr, $("#subscriptions-status"));
|
|
|
|
}
|
|
|
|
|
|
|
|
var req = $.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url: '/json/users',
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: 10*1000,
|
|
|
|
success: populate_users,
|
|
|
|
error: failed_listing
|
|
|
|
});
|
2013-08-12 23:31:23 +02:00
|
|
|
|
2013-11-16 16:36:45 +01:00
|
|
|
$(".admin_user_table").on("click", ".deactivate", function (e) {
|
2013-08-12 23:31:23 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
$(".active_user_row").removeClass("active_user_row");
|
|
|
|
|
|
|
|
// Go up the tree until we find the user row, then grab the email element
|
|
|
|
$(e.target).closest(".user_row").addClass("active_user_row");
|
|
|
|
|
|
|
|
var user_name = $(".active_user_row").find('.user_name').text();
|
|
|
|
var email = $(".active_user_row").find('.email').text();
|
|
|
|
|
|
|
|
$("#deactivation_modal .email").text(email);
|
|
|
|
$("#deactivation_modal .user_name").text(user_name);
|
|
|
|
$("#deactivation_modal").modal("show");
|
|
|
|
});
|
|
|
|
|
2013-11-16 16:36:45 +01:00
|
|
|
$(".admin_user_table").on("click", ".reactivate", function (e) {
|
2013-11-16 16:26:40 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// Go up the tree until we find the user row, then grab the email element
|
|
|
|
$(e.target).closest(".user_row").addClass("active_user_row");
|
|
|
|
|
|
|
|
var email = $(".active_user_row").find('.email').text();
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/users/' + $(".active_user_row").find('.email').text() + "/reactivate",
|
|
|
|
error: function (xhr, error_type) {
|
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
|
|
$(".active_user_row button").closest("td").html(
|
|
|
|
$("<p>").addClass("text-error").text($.parseJSON(xhr.responseText).msg)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$(".active_user_row button").text("Failed!");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
var row = $(".active_user_row");
|
|
|
|
var button = $(".active_user_row button");
|
|
|
|
button.addClass("btn-danger");
|
|
|
|
button.removeClass("btn-warning");
|
|
|
|
button.addClass("deactivate");
|
|
|
|
button.removeClass("reactivate");
|
|
|
|
button.text("Deactivate");
|
|
|
|
row.removeClass("inactive_user_row");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-08-12 23:31:23 +02:00
|
|
|
$("#do_deactivate_button").click(function (e) {
|
|
|
|
if ($("#deactivation_modal .email").html() !== $(".active_user_row").find('.email').text()) {
|
|
|
|
blueslip.error("User deactivation canceled due to non-matching fields.");
|
|
|
|
ui.report_message("Deactivation encountered an error. Please reload and try again.",
|
|
|
|
$("#home-error"), 'alert-error');
|
|
|
|
}
|
|
|
|
$("#deactivation_modal").modal("hide");
|
|
|
|
$(".active_user_row button").prop("disabled", true).text("Working…");
|
|
|
|
$.ajax({
|
|
|
|
type: 'DELETE',
|
|
|
|
url: '/json/users/' + $(".active_user_row").find('.email').text(),
|
|
|
|
error: function (xhr, error_type) {
|
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
|
|
|
$(".active_user_row button").closest("td").html(
|
|
|
|
$("<p>").addClass("text-error").text($.parseJSON(xhr.responseText).msg)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$(".active_user_row button").text("Failed!");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
success: function () {
|
2013-11-16 16:26:40 +01:00
|
|
|
var row = $(".active_user_row");
|
|
|
|
var button = $(".active_user_row button");
|
|
|
|
button.prop("disabled", false);
|
|
|
|
button.addClass("btn-warning");
|
|
|
|
button.removeClass("btn-danger");
|
|
|
|
button.addClass("reactivate");
|
|
|
|
button.removeClass("deactivate");
|
|
|
|
button.text("Reactivate");
|
|
|
|
row.addClass("inactive_user_row");
|
2013-08-12 23:31:23 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|