discourse/app/assets/javascripts/admin/controllers/admin-logs-screened-ip-addr...

134 lines
3.7 KiB
Plaintext
Raw Normal View History

import Controller from "@ember/controller";
import discourseDebounce from "discourse/lib/debounce";
2018-06-15 11:03:24 -04:00
import { outputExportResult } from "discourse/lib/export-result";
import { exportEntity } from "discourse/lib/export-csv";
import ScreenedIpAddress from "admin/models/screened-ip-address";
2014-12-06 23:15:22 -05:00
export default Controller.extend({
loading: false,
2015-02-10 13:38:59 -05:00
filter: null,
savedIpAddress: null,
show: discourseDebounce(function() {
2018-06-15 11:03:24 -04:00
this.set("loading", true);
ScreenedIpAddress.findAll(this.filter).then(result => {
this.setProperties({ model: result, loading: false });
2018-06-15 11:03:24 -04:00
});
2015-02-10 13:38:59 -05:00
}, 250).observes("filter"),
actions: {
allow(record) {
2018-06-15 11:03:24 -04:00
record.set("action_name", "do_nothing");
record.save();
},
block(record) {
2018-06-15 11:03:24 -04:00
record.set("action_name", "block");
record.save();
},
edit(record) {
2018-06-15 11:03:24 -04:00
if (!record.get("editing")) {
this.set("savedIpAddress", record.get("ip_address"));
}
2018-06-15 11:03:24 -04:00
record.set("editing", true);
},
cancel(record) {
const savedIpAddress = this.savedIpAddress;
if (savedIpAddress && record.get("editing")) {
record.set("ip_address", savedIpAddress);
}
2018-06-15 11:03:24 -04:00
record.set("editing", false);
},
save(record) {
2018-06-15 11:03:24 -04:00
const wasEditing = record.get("editing");
record.set("editing", false);
record
.save()
.then(() => this.set("savedIpAddress", null))
2018-06-15 11:03:24 -04:00
.catch(e => {
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
2018-06-15 11:03:24 -04:00
bootbox.alert(
I18n.t("generic_error_with_reason", {
error: e.jqXHR.responseJSON.errors.join(". ")
2018-06-15 11:03:24 -04:00
})
);
} else {
bootbox.alert(I18n.t("generic_error"));
}
if (wasEditing) record.set("editing", true);
});
},
destroy(record) {
return bootbox.confirm(
2018-06-15 11:03:24 -04:00
I18n.t("admin.logs.screened_ips.delete_confirm", {
ip_address: record.get("ip_address")
}),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
if (result) {
2018-06-15 11:03:24 -04:00
record
.destroy()
.then(deleted => {
if (deleted) {
this.model.removeObject(record);
2018-06-15 11:03:24 -04:00
} else {
bootbox.alert(I18n.t("generic_error"));
}
})
.catch(e => {
bootbox.alert(
I18n.t("generic_error_with_reason", {
error: `http: ${e.status} - ${e.body}`
2018-06-15 11:03:24 -04:00
})
);
});
}
}
);
},
recordAdded(arg) {
this.model.unshiftObject(arg);
},
rollUp() {
2018-06-15 11:03:24 -04:00
return bootbox.confirm(
I18n.t("admin.logs.screened_ips.roll_up_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
confirmed => {
2018-06-15 11:03:24 -04:00
if (confirmed) {
this.set("loading", true);
return ScreenedIpAddress.rollUp().then(results => {
2018-06-15 11:03:24 -04:00
if (results && results.subnets) {
if (results.subnets.length > 0) {
this.send("show");
2018-06-15 11:03:24 -04:00
bootbox.alert(
I18n.t("admin.logs.screened_ips.rolled_up_some_subnets", {
subnets: results.subnets.join(", ")
})
);
} else {
this.set("loading", false);
2018-06-15 11:03:24 -04:00
bootbox.alert(
I18n.t("admin.logs.screened_ips.rolled_up_no_subnet")
);
}
}
2018-06-15 11:03:24 -04:00
});
}
}
2018-06-15 11:03:24 -04:00
);
2014-12-06 23:15:22 -05:00
},
exportScreenedIpList() {
2018-06-15 11:03:24 -04:00
exportEntity("screened_ip").then(outputExportResult);
}
}
});