A form to add ip addresses to be blocked or whitelisted

This commit is contained in:
Neil Lalonde 2013-10-24 17:18:10 -04:00
parent 61468f6f27
commit 017efdece5
10 changed files with 113 additions and 15 deletions

View File

@ -0,0 +1,64 @@
/**
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
**/
Discourse.ScreenedIpAddressFormComponent = Ember.Component.extend({
classNames: ['screened-ip-address-form'],
formSubmitted: false,
actionName: 'block',
actionNames: function() {
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(),
actions: {
submit: function() {
if (!this.get('formSubmitted')) {
var self = this;
this.set('formSubmitted', true);
var screenedIpAddress = Discourse.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', Discourse.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(e) {
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');
}
});
});
}
});

View File

@ -18,6 +18,12 @@ Discourse.AdminLogsScreenedIpAddressesController = Ember.ArrayController.extend(
self.set('content', result);
self.set('loading', false);
});
},
actions: {
recordAdded: function(arg) {
this.get("content").unshiftObject(arg);
}
}
});
@ -27,12 +33,12 @@ Discourse.AdminLogsScreenedIpAddressController = Ember.ObjectController.extend({
actions: {
allow: function(record) {
record.set('action', 'do_nothing');
record.set('action_name', 'do_nothing');
this.send('save', record);
},
block: function(record) {
record.set('action', 'block');
record.set('action_name', 'block');
this.send('save', record);
},

View File

@ -9,20 +9,20 @@
**/
Discourse.ScreenedIpAddress = Discourse.Model.extend({
actionName: function() {
return I18n.t("admin.logs.screened_ips.actions." + this.get('action'));
}.property('action'),
return I18n.t("admin.logs.screened_ips.actions." + this.get('action_name'));
}.property('action_name'),
isBlocked: function() {
return (this.get('action') === 'block');
}.property('action'),
return (this.get('action_name') === 'block');
}.property('action_name'),
actionIcon: function() {
if (this.get('action') === 'block') {
if (this.get('action_name') === 'block') {
return this.get('blockIcon');
} else {
return this.get('doNothingIcon');
}
}.property('action'),
}.property('action_name'),
blockIcon: function() {
return 'icon-ban-circle';
@ -33,9 +33,9 @@ Discourse.ScreenedIpAddress = Discourse.Model.extend({
}.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')}
return Discourse.ajax("/admin/logs/screened_ip_addresses" + (this.id ? '/' + this.id : '') + ".json", {
type: this.id ? 'PUT' : 'POST',
data: {ip_address: this.get('ip_address'), action_name: this.get('action_name')}
});
},

View File

@ -1,5 +1,8 @@
<p>{{i18n admin.logs.screened_ips.description}}</p>
{{screened-ip-address-form action="recordAdded"}}
<br/>
{{#if loading}}
<div class='admin-loading'>{{i18n loading}}</div>
{{else}}

View File

@ -0,0 +1,4 @@
<b>{{i18n admin.logs.screened_ips.form.label}}</b>
{{textField value=ip_address disabled=formSubmitted class="ip-address-input" placeholderKey="admin.logs.screened_ips.form.ip_address" autocorrect="off" autocapitalize="off"}}
{{combobox content=actionNames value=actionName}}
<button class="btn btn-small" {{action submit target="view"}} {{bindAttr disabled="formSubmitted"}}>{{i18n admin.logs.screened_ips.form.add}}</button>

View File

@ -752,6 +752,14 @@ table.api-keys {
}
}
.screened-ip-address-form {
margin-left: 6px;
.combobox {
width: 130px;
top: 10px;
}
}
.screened-emails, .screened-urls {
.ip_address {
width: 110px;

View File

@ -7,6 +7,15 @@ class Admin::ScreenedIpAddressesController < Admin::AdminController
render_serialized(screened_ip_addresses, ScreenedIpAddressSerializer)
end
def create
screened_ip_address = ScreenedIpAddress.new(allowed_params)
if screened_ip_address.save
render_serialized(screened_ip_address, ScreenedIpAddressSerializer)
else
render_json_error(screened_ip_address)
end
end
def update
if @screened_ip_address.update_attributes(allowed_params)
render json: success_json

View File

@ -1,12 +1,12 @@
class ScreenedIpAddressSerializer < ApplicationSerializer
attributes :id,
:ip_address,
:action,
:action_name,
:match_count,
:last_match_at,
:created_at
def action
def action_name
ScreenedIpAddress.actions.key(object.action_type).to_s
end

View File

@ -1268,11 +1268,15 @@ en:
url: "URL"
screened_ips:
title: "Screened IPs"
description: "IP addresses that are being watched."
description: 'IP addresses that are being watched. Use "Allow" to whitelist IP addresses.'
delete_confirm: "Are you sure you want to remove the rule for %{ip_address}?"
actions:
block: "Block"
do_nothing: "Allow"
form:
label: "New:"
ip_address: "IP address"
add: "Add"
impersonate:
title: "Impersonate User"

View File

@ -69,7 +69,7 @@ Discourse::Application.routes.draw do
scope '/logs' do
resources :staff_action_logs, only: [:index]
resources :screened_emails, only: [:index]
resources :screened_ip_addresses, only: [:index, :update, :destroy]
resources :screened_ip_addresses, only: [:index, :create, :update, :destroy]
resources :screened_urls, only: [:index]
end