2019-11-08 05:38:28 +08:00
|
|
|
import discourseComputed, { on } from "discourse-common/utils/decorators";
|
2019-10-24 00:30:52 +08:00
|
|
|
import Component from "@ember/component";
|
2020-08-27 00:57:13 +08:00
|
|
|
import I18n from "I18n";
|
2015-12-18 02:40:50 +08:00
|
|
|
import ScreenedIpAddress from "admin/models/screened-ip-address";
|
2020-08-27 00:57:13 +08:00
|
|
|
import bootbox from "bootbox";
|
2019-10-30 21:48:24 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
2020-08-27 00:57:13 +08:00
|
|
|
|
2015-12-18 02:40:50 +08:00
|
|
|
/**
|
2020-07-27 08:23:54 +08:00
|
|
|
A form to create an IP address that will be blocked or allowed.
|
2015-12-18 02:40:50 +08:00
|
|
|
Example usage:
|
|
|
|
|
2019-01-10 18:06:01 +08:00
|
|
|
{{screened-ip-address-form action=(action "recordAdded")}}
|
2015-12-18 02:40:50 +08:00
|
|
|
|
|
|
|
where action is a callback on the controller or route that will get called after
|
|
|
|
the new record is successfully saved. It is called with the new ScreenedIpAddress record
|
|
|
|
as an argument.
|
|
|
|
**/
|
|
|
|
|
2019-10-24 00:30:52 +08:00
|
|
|
export default Component.extend({
|
2015-12-18 02:40:50 +08:00
|
|
|
classNames: ["screened-ip-address-form"],
|
|
|
|
formSubmitted: false,
|
|
|
|
actionName: "block",
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
@discourseComputed("siteSettings.use_admin_ip_allowlist")
|
|
|
|
actionNames(adminAllowlistEnabled) {
|
|
|
|
if (adminAllowlistEnabled) {
|
2015-12-18 02:40:50 +08:00
|
|
|
return [
|
|
|
|
{ id: "block", name: I18n.t("admin.logs.screened_ips.actions.block") },
|
|
|
|
{
|
|
|
|
id: "do_nothing",
|
|
|
|
name: I18n.t("admin.logs.screened_ips.actions.do_nothing"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "allow_admin",
|
|
|
|
name: I18n.t("admin.logs.screened_ips.actions.allow_admin"),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return [
|
|
|
|
{ id: "block", name: I18n.t("admin.logs.screened_ips.actions.block") },
|
|
|
|
{
|
|
|
|
id: "do_nothing",
|
|
|
|
name: I18n.t("admin.logs.screened_ips.actions.do_nothing"),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
submit() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (!this.formSubmitted) {
|
2015-12-18 02:40:50 +08:00
|
|
|
this.set("formSubmitted", true);
|
|
|
|
const screenedIpAddress = ScreenedIpAddress.create({
|
2019-05-27 16:15:39 +08:00
|
|
|
ip_address: this.ip_address,
|
|
|
|
action_name: this.actionName,
|
2015-12-18 02:40:50 +08:00
|
|
|
});
|
|
|
|
screenedIpAddress
|
2018-06-15 23:03:24 +08:00
|
|
|
.save()
|
2015-12-18 02:40:50 +08:00
|
|
|
.then((result) => {
|
2018-08-20 23:37:30 +08:00
|
|
|
this.setProperties({ ip_address: "", formSubmitted: false });
|
2019-01-10 18:06:01 +08:00
|
|
|
this.action(ScreenedIpAddress.create(result.screened_ip_address));
|
2019-10-30 21:48:24 +08:00
|
|
|
schedule("afterRender", () =>
|
2019-07-16 18:45:15 +08:00
|
|
|
this.element.querySelector(".ip-address-input").focus()
|
2018-08-20 23:37:30 +08:00
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
})
|
2015-12-18 02:40:50 +08:00
|
|
|
.catch((e) => {
|
|
|
|
this.set("formSubmitted", false);
|
2018-03-20 02:34:43 +08:00
|
|
|
const msg =
|
|
|
|
e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors
|
|
|
|
? I18n.t("generic_error_with_reason", {
|
|
|
|
error: e.jqXHR.responseJSON.errors.join(". "),
|
2018-06-15 23:03:24 +08:00
|
|
|
})
|
2015-12-18 02:40:50 +08:00
|
|
|
: I18n.t("generic_error");
|
2019-07-16 18:45:15 +08:00
|
|
|
bootbox.alert(msg, () =>
|
|
|
|
this.element.querySelector(".ip-address-input").focus()
|
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
});
|
2015-12-18 02:40:50 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
@on("didInsertElement")
|
|
|
|
_init() {
|
2019-10-30 21:48:24 +08:00
|
|
|
schedule("afterRender", () => {
|
2019-07-16 18:45:15 +08:00
|
|
|
$(this.element.querySelector(".ip-address-input")).keydown((e) => {
|
2015-12-18 02:40:50 +08:00
|
|
|
if (e.keyCode === 13) {
|
|
|
|
this.send("submit");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|