diff --git a/app/assets/javascripts/admin/controllers/admin_logs_screened_ip_addresses_controller.js b/app/assets/javascripts/admin/controllers/admin_logs_screened_ip_addresses_controller.js index b733558aae3..303b7fb3434 100644 --- a/app/assets/javascripts/admin/controllers/admin_logs_screened_ip_addresses_controller.js +++ b/app/assets/javascripts/admin/controllers/admin_logs_screened_ip_addresses_controller.js @@ -9,6 +9,7 @@ Discourse.AdminLogsScreenedIpAddressesController = Ember.ArrayController.extend(Discourse.Presence, { loading: false, content: [], + itemController: 'adminLogsScreenedIpAddress', show: function() { var self = this; @@ -19,3 +20,72 @@ Discourse.AdminLogsScreenedIpAddressesController = Ember.ArrayController.extend( }); } }); + +Discourse.AdminLogsScreenedIpAddressController = Ember.ObjectController.extend({ + editing: false, + savedIpAddress: null, + + actions: { + allow: function(record) { + record.set('action', 'do_nothing'); + this.send('save', record); + }, + + block: function(record) { + record.set('action', 'block'); + this.send('save', record); + }, + + edit: function() { + if (!this.get('editing')) { + this.savedIpAddress = this.get('ip_address'); + } + this.set('editing', true); + }, + + cancel: function() { + if (this.get('savedIpAddress') && this.get('editing')) { + this.set('ip_address', this.get('savedIpAddress')); + } + this.set('editing', false); + }, + + save: function(record) { + var self = this; + var wasEditing = this.get('editing'); + this.set('editing', false); + record.save().then(function(saved){ + if (saved.success) { + self.set('savedIpAddress', null); + } else { + bootbox.alert(saved.errors); + if (wasEditing) self.set('editing', true); + } + }, function(e){ + if (e.responseJSON && e.responseJSON.errors) { + bootbox.alert(I18n.t("generic_error_with_reason", {error: e.responseJSON.errors.join('. ')})); + } else { + bootbox.alert(I18n.t("generic_error")); + } + if (wasEditing) self.set('editing', true); + }); + }, + + destroy: function(record) { + var self = this; + return bootbox.confirm(I18n.t("admin.logs.screened_ips.delete_confirm", {ip_address: record.get('ip_address')}), I18n.t("no_value"), I18n.t("yes_value"), function(result) { + if (result) { + record.destroy().then(function(deleted) { + if (deleted) { + self.get("parentController.content").removeObject(record); + } else { + bootbox.alert(I18n.t("generic_error")); + } + }, function(e){ + bootbox.alert(I18n.t("generic_error_with_reason", {error: "http: " + e.status + " - " + e.body})); + }); + } + }); + } + } +}); \ No newline at end of file diff --git a/app/assets/javascripts/admin/models/screened_ip_address.js b/app/assets/javascripts/admin/models/screened_ip_address.js index 1e81b61658e..8fe13582e13 100644 --- a/app/assets/javascripts/admin/models/screened_ip_address.js +++ b/app/assets/javascripts/admin/models/screened_ip_address.js @@ -8,14 +8,40 @@ @module Discourse **/ Discourse.ScreenedIpAddress = Discourse.Model.extend({ - // TODO: this is repeated in all 3 screened models. move it. actionName: function() { - if (this.get('action') === 'do_nothing') { - return I18n.t("admin.logs.screened_ips.allow"); + return I18n.t("admin.logs.screened_ips.actions." + this.get('action')); + }.property('action'), + + isBlocked: function() { + return (this.get('action') === 'block'); + }.property('action'), + + actionIcon: function() { + if (this.get('action') === 'block') { + return this.get('blockIcon'); } else { - return I18n.t("admin.logs.screened_actions." + this.get('action')); + return this.get('doNothingIcon'); } - }.property('action') + }.property('action'), + + blockIcon: function() { + return 'icon-remove'; + }.property(), + + doNothingIcon: function() { + return 'icon-ok'; + }.property(), + + save: function() { + return Discourse.ajax("/admin/logs/screened_ip_addresses/" + this.get('id') + ".json", { + type: 'PUT', + data: {ip_address: this.get('ip_address'), action_name: this.get('action')} + }); + }, + + destroy: function() { + return Discourse.ajax("/admin/logs/screened_ip_addresses/" + this.get('id') + ".json", {type: 'DELETE'}); + } }); Discourse.ScreenedIpAddress.reopenClass({ diff --git a/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.js.handlebars b/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.js.handlebars index ed6bc5521f4..b929daf340c 100644 --- a/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.js.handlebars +++ b/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.js.handlebars @@ -5,13 +5,14 @@ {{else}} {{#if model.length}} -