2023-10-11 02:38:59 +08:00
|
|
|
import Component from "@ember/component";
|
2023-02-23 23:32:53 +08:00
|
|
|
import { action } from "@ember/object";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { schedule } from "@ember/runloop";
|
2023-02-23 23:32:53 +08:00
|
|
|
import { inject as service } from "@ember/service";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { classNames, tagName } from "@ember-decorators/component";
|
2022-07-14 08:59:33 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2023-10-18 18:07:09 +08:00
|
|
|
import I18n from "discourse-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
|
|
|
|
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.
|
|
|
|
**/
|
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@tagName("form")
|
|
|
|
@classNames("screened-ip-address-form", "inline-form")
|
|
|
|
export default class ScreenedIpAddressForm extends Component {
|
|
|
|
@service dialog;
|
|
|
|
|
|
|
|
formSubmitted = false;
|
|
|
|
actionName = "block";
|
2015-12-18 02:40:50 +08:00
|
|
|
|
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"),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2015-12-18 02:40:50 +08:00
|
|
|
|
2022-09-28 02:47:13 +08:00
|
|
|
focusInput() {
|
|
|
|
schedule("afterRender", () => {
|
|
|
|
this.element.querySelector("input").focus();
|
|
|
|
});
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2022-09-28 02:47:13 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@action
|
|
|
|
submitForm() {
|
|
|
|
if (!this.formSubmitted) {
|
|
|
|
this.set("formSubmitted", true);
|
|
|
|
const screenedIpAddress = ScreenedIpAddress.create({
|
|
|
|
ip_address: this.ip_address,
|
|
|
|
action_name: this.actionName,
|
|
|
|
});
|
|
|
|
screenedIpAddress
|
|
|
|
.save()
|
|
|
|
.then((result) => {
|
|
|
|
this.setProperties({ ip_address: "", formSubmitted: false });
|
|
|
|
this.action(ScreenedIpAddress.create(result.screened_ip_address));
|
|
|
|
this.focusInput();
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
this.set("formSubmitted", false);
|
|
|
|
const message = e.jqXHR.responseJSON?.errors
|
|
|
|
? I18n.t("generic_error_with_reason", {
|
|
|
|
error: e.jqXHR.responseJSON.errors.join(". "),
|
|
|
|
})
|
|
|
|
: I18n.t("generic_error");
|
|
|
|
this.dialog.alert({
|
|
|
|
message,
|
|
|
|
didConfirm: () => this.focusInput(),
|
|
|
|
didCancel: () => this.focusInput(),
|
2018-06-15 23:03:24 +08:00
|
|
|
});
|
2023-02-23 23:32:53 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|