From 1704a362efef5f050db2f3ba212d68a2f3815eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 17 Dec 2015 19:40:50 +0100 Subject: [PATCH] FIX: manually blocking/whitelisting an IP address wasn't working --- .../screened-ip-address-form-component.js.es6 | 79 ------------------- .../screened-ip-address-form.js.es6 | 75 ++++++++++++++++++ .../components/screened-ip-address-form.hbs | 2 +- .../templates/logs/screened_ip_addresses.hbs | 11 ++- 4 files changed, 83 insertions(+), 84 deletions(-) delete mode 100644 app/assets/javascripts/admin/components/screened-ip-address-form-component.js.es6 create mode 100644 app/assets/javascripts/admin/components/screened-ip-address-form.js.es6 rename app/assets/javascripts/{discourse => admin}/templates/components/screened-ip-address-form.hbs (65%) diff --git a/app/assets/javascripts/admin/components/screened-ip-address-form-component.js.es6 b/app/assets/javascripts/admin/components/screened-ip-address-form-component.js.es6 deleted file mode 100644 index 8702dbb2f9e..00000000000 --- a/app/assets/javascripts/admin/components/screened-ip-address-form-component.js.es6 +++ /dev/null @@ -1,79 +0,0 @@ -import ScreenedIpAddress from 'admin/models/screened-ip-address'; -/** - 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. - - @class ScreenedIpAddressFormComponent - @extends Ember.Component - @namespace Discourse - @module Discourse -**/ -const ScreenedIpAddressFormComponent = Ember.Component.extend({ - classNames: ['screened-ip-address-form'], - formSubmitted: false, - actionName: 'block', - - adminWhitelistEnabled: function() { - return Discourse.SiteSettings.use_admin_ip_whitelist; - }.property(), - - actionNames: function() { - if (this.get('adminWhitelistEnabled')) { - 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')} - ]; - } - }.property('adminWhitelistEnabled'), - - actions: { - submit: function() { - if (!this.get('formSubmitted')) { - var self = this; - this.set('formSubmitted', true); - var screenedIpAddress = ScreenedIpAddress.create({ip_address: this.get('ip_address'), action_name: this.get('actionName')}); - screenedIpAddress.save().then(function(result) { - self.set('ip_address', ''); - self.set('formSubmitted', false); - self.sendAction('action', ScreenedIpAddress.create(result.screened_ip_address)); - Em.run.schedule('afterRender', function() { self.$('.ip-address-input').focus(); }); - }, function(e) { - self.set('formSubmitted', false); - var msg; - if (e.responseJSON && e.responseJSON.errors) { - msg = I18n.t("generic_error_with_reason", {error: e.responseJSON.errors.join('. ')}); - } else { - msg = I18n.t("generic_error"); - } - bootbox.alert(msg, function() { self.$('.ip-address-input').focus(); }); - }); - } - } - }, - - didInsertElement: function() { - var self = this; - this._super(); - Em.run.schedule('afterRender', function() { - self.$('.ip-address-input').keydown(function(e) { - if (e.keyCode === 13) { // enter key - self.send('submit'); - } - }); - }); - } -}); - -export default ScreenedIpAddressFormComponent; diff --git a/app/assets/javascripts/admin/components/screened-ip-address-form.js.es6 b/app/assets/javascripts/admin/components/screened-ip-address-form.js.es6 new file mode 100644 index 00000000000..0f8a29ba8c3 --- /dev/null +++ b/app/assets/javascripts/admin/components/screened-ip-address-form.js.es6 @@ -0,0 +1,75 @@ +/** + 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. +**/ + +import ScreenedIpAddress from 'admin/models/screened-ip-address'; +import computed from 'ember-addons/ember-computed-decorators'; +import { on } from 'ember-addons/ember-computed-decorators'; + +export default Ember.Component.extend({ + classNames: ['screened-ip-address-form'], + formSubmitted: false, + actionName: 'block', + + @computed + adminWhitelistEnabled() { + return Discourse.SiteSettings.use_admin_ip_whitelist; + }, + + @computed("adminWhitelistEnabled") + actionNames(adminWhitelistEnabled) { + if (adminWhitelistEnabled) { + 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() { + if (!this.get('formSubmitted')) { + this.set('formSubmitted', true); + const screenedIpAddress = ScreenedIpAddress.create({ + ip_address: this.get('ip_address'), + action_name: this.get('actionName') + }); + screenedIpAddress.save().then(result => { + this.setProperties({ ip_address: '', formSubmitted: false }); + this.sendAction('action', ScreenedIpAddress.create(result.screened_ip_address)); + Ember.run.schedule('afterRender', () => this.$('.ip-address-input').focus()); + }).catch(e => { + this.set('formSubmitted', false); + const msg = (e.responseJSON && e.responseJSON.errors) ? + I18n.t("generic_error_with_reason", {error: e.responseJSON.errors.join('. ')}) : + I18n.t("generic_error"); + bootbox.alert(msg, () => this.$('.ip-address-input').focus()); + }); + } + } + }, + + @on("didInsertElement") + _init() { + Ember.run.schedule('afterRender', () => { + this.$('.ip-address-input').keydown(e => { + if (e.keyCode === 13) { + this.send('submit'); + } + }); + }); + } +}); diff --git a/app/assets/javascripts/discourse/templates/components/screened-ip-address-form.hbs b/app/assets/javascripts/admin/templates/components/screened-ip-address-form.hbs similarity index 65% rename from app/assets/javascripts/discourse/templates/components/screened-ip-address-form.hbs rename to app/assets/javascripts/admin/templates/components/screened-ip-address-form.hbs index 5f58a282c40..e846b1c6527 100644 --- a/app/assets/javascripts/discourse/templates/components/screened-ip-address-form.hbs +++ b/app/assets/javascripts/admin/templates/components/screened-ip-address-form.hbs @@ -1,4 +1,4 @@ {{i18n 'admin.logs.screened_ips.form.label'}} {{text-field value=ip_address disabled=formSubmitted class="ip-address-input" placeholderKey="admin.logs.screened_ips.form.ip_address" autocorrect="off" autocapitalize="off"}} {{combo-box content=actionNames value=actionName}} - +{{d-button action="submit" disabled=formSubmitted label="admin.logs.screened_ips.form.add"}} diff --git a/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.hbs b/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.hbs index 515c22a3a0b..5aaada9ea2f 100644 --- a/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.hbs +++ b/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.hbs @@ -1,11 +1,14 @@

{{i18n 'admin.logs.screened_ips.description'}}

+
{{text-field value=filter class="ip-address-input" placeholderKey="admin.logs.screened_ips.form.filter" autocorrect="off" autocapitalize="off"}} - - + {{d-button action="rollUp" title="admin.logs.screened_ips.roll_up.title" label="admin.logs.screened_ips.roll_up.text"}} + {{d-button action="exportScreenedIpList" icon="download" title="admin.export_csv.button_title.screened_ip" label="admin.export_csv.button_text"}} +
+ +
+ {{screened-ip-address-form action="recordAdded"}}
-{{screened-ip-address-form action="recordAdded"}} -
{{#conditional-loading-spinner condition=loading}} {{#if model.length}}