2015-12-18 02:40:50 +08:00
|
|
|
/**
|
|
|
|
A form to create an IP address that will be blocked or whitelisted.
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
{{screened-ip-address-form action="recordAdded"}}
|
|
|
|
|
|
|
|
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.
|
|
|
|
**/
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
import ScreenedIpAddress from "admin/models/screened-ip-address";
|
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
|
|
|
import { on } from "ember-addons/ember-computed-decorators";
|
2015-12-18 02:40:50 +08:00
|
|
|
|
|
|
|
export default Ember.Component.extend({
|
2018-06-15 23:03:24 +08:00
|
|
|
classNames: ["screened-ip-address-form"],
|
2015-12-18 02:40:50 +08:00
|
|
|
formSubmitted: false,
|
2018-06-15 23:03:24 +08:00
|
|
|
actionName: "block",
|
2015-12-18 02:40:50 +08:00
|
|
|
|
|
|
|
@computed
|
|
|
|
adminWhitelistEnabled() {
|
|
|
|
return Discourse.SiteSettings.use_admin_ip_whitelist;
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("adminWhitelistEnabled")
|
|
|
|
actionNames(adminWhitelistEnabled) {
|
|
|
|
if (adminWhitelistEnabled) {
|
|
|
|
return [
|
2018-06-15 23:03:24 +08:00
|
|
|
{ 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")
|
|
|
|
}
|
2015-12-18 02:40:50 +08:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return [
|
2018-06-15 23:03:24 +08:00
|
|
|
{ id: "block", name: I18n.t("admin.logs.screened_ips.actions.block") },
|
|
|
|
{
|
|
|
|
id: "do_nothing",
|
|
|
|
name: I18n.t("admin.logs.screened_ips.actions.do_nothing")
|
|
|
|
}
|
2015-12-18 02:40:50 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
submit() {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (!this.get("formSubmitted")) {
|
|
|
|
this.set("formSubmitted", true);
|
2015-12-18 02:40:50 +08:00
|
|
|
const screenedIpAddress = ScreenedIpAddress.create({
|
2018-06-15 23:03:24 +08:00
|
|
|
ip_address: this.get("ip_address"),
|
|
|
|
action_name: this.get("actionName")
|
2015-12-18 02:40:50 +08:00
|
|
|
});
|
2018-06-15 23:03:24 +08:00
|
|
|
screenedIpAddress
|
|
|
|
.save()
|
|
|
|
.then(result => {
|
2018-08-20 23:37:30 +08:00
|
|
|
this.setProperties({ ip_address: "", formSubmitted: false });
|
|
|
|
this.sendAction(
|
|
|
|
"action",
|
|
|
|
ScreenedIpAddress.create(result.screened_ip_address)
|
|
|
|
);
|
|
|
|
Ember.run.schedule("afterRender", () =>
|
|
|
|
this.$(".ip-address-input").focus()
|
|
|
|
);
|
2018-06-15 23:03:24 +08:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
this.set("formSubmitted", false);
|
|
|
|
const msg =
|
|
|
|
e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors
|
|
|
|
? I18n.t("generic_error_with_reason", {
|
|
|
|
error: e.jqXHR.responseJSON.errors.join(". ")
|
|
|
|
})
|
|
|
|
: I18n.t("generic_error");
|
|
|
|
bootbox.alert(msg, () => this.$(".ip-address-input").focus());
|
|
|
|
});
|
2015-12-18 02:40:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@on("didInsertElement")
|
|
|
|
_init() {
|
2018-06-15 23:03:24 +08:00
|
|
|
Ember.run.schedule("afterRender", () => {
|
|
|
|
this.$(".ip-address-input").keydown(e => {
|
2015-12-18 02:40:50 +08:00
|
|
|
if (e.keyCode === 13) {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.send("submit");
|
2015-12-18 02:40:50 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|