From d2fb6ec53f3233d7c8ba040b5728611273c49c73 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 1 Aug 2013 21:30:13 -0400 Subject: [PATCH] Blocked Emails list in admin --- .../admin_logs_blocked_emails_controller.js | 21 +++++++++++ .../javascripts/admin/models/blocked_email.js | 26 ++++++++++++++ .../admin/routes/admin_logs_routes.js | 35 +++++++++++++++++++ .../javascripts/admin/routes/admin_routes.js | 4 +++ .../admin/templates/admin.js.handlebars | 1 + .../admin/templates/logs.js.handlebars | 9 +++++ .../logs/blocked_emails.js.handlebars | 29 +++++++++++++++ app/assets/stylesheets/admin/admin_base.scss | 8 +++++ .../admin/blocked_emails_controller.rb | 8 +++++ app/models/blocked_email.rb | 4 +++ app/serializers/blocked_email_serializer.rb | 11 ++++++ config/locales/client.en.yml | 12 +++++++ config/routes.rb | 4 +++ .../admin/blocked_emails_controller_spec.rb | 22 ++++++++++++ 14 files changed, 194 insertions(+) create mode 100644 app/assets/javascripts/admin/controllers/admin_logs_blocked_emails_controller.js create mode 100644 app/assets/javascripts/admin/models/blocked_email.js create mode 100644 app/assets/javascripts/admin/routes/admin_logs_routes.js create mode 100644 app/assets/javascripts/admin/templates/logs.js.handlebars create mode 100644 app/assets/javascripts/admin/templates/logs/blocked_emails.js.handlebars create mode 100644 app/controllers/admin/blocked_emails_controller.rb create mode 100644 app/serializers/blocked_email_serializer.rb create mode 100644 spec/controllers/admin/blocked_emails_controller_spec.rb diff --git a/app/assets/javascripts/admin/controllers/admin_logs_blocked_emails_controller.js b/app/assets/javascripts/admin/controllers/admin_logs_blocked_emails_controller.js new file mode 100644 index 00000000000..6a340eaf023 --- /dev/null +++ b/app/assets/javascripts/admin/controllers/admin_logs_blocked_emails_controller.js @@ -0,0 +1,21 @@ +/** + This controller supports the interface for listing blocked email addresses in the admin section. + + @class AdminLogsBlockedEmailsController + @extends Ember.ArrayController + @namespace Discourse + @module Discourse +**/ +Discourse.AdminLogsBlockedEmailsController = Ember.ArrayController.extend(Discourse.Presence, { + loading: false, + + show: function() { + var self = this; + this.set('loading', true); + Discourse.BlockedEmail.findAll().then(function(result) { + console.log('findAll done'); + self.set('content', result); + self.set('loading', false); + }); + } +}); diff --git a/app/assets/javascripts/admin/models/blocked_email.js b/app/assets/javascripts/admin/models/blocked_email.js new file mode 100644 index 00000000000..762102c93f6 --- /dev/null +++ b/app/assets/javascripts/admin/models/blocked_email.js @@ -0,0 +1,26 @@ +/** + Represents an email address that is watched for during account registration, + and an action is taken. + + @class BlockedEmail + @extends Discourse.Model + @namespace Discourse + @module Discourse +**/ +Discourse.BlockedEmail = Discourse.Model.extend({ + actionName: function() { + return I18n.t("admin.logs.actions." + this.get('action')); + }.property('action') +}); + +Discourse.BlockedEmail.reopenClass({ + findAll: function(filter) { + return Discourse.ajax("/admin/logs/blocked_emails.json").then(function(blocked_emails) { + return blocked_emails.map(function(b) { + return Discourse.BlockedEmail.create(b); + }) + }); + } +}); + + diff --git a/app/assets/javascripts/admin/routes/admin_logs_routes.js b/app/assets/javascripts/admin/routes/admin_logs_routes.js new file mode 100644 index 00000000000..3301fc46dc1 --- /dev/null +++ b/app/assets/javascripts/admin/routes/admin_logs_routes.js @@ -0,0 +1,35 @@ +/** + Index redirects to a default logs index. + + @class AdminLogsIndexRoute + @extends Discourse.Route + @namespace Discourse + @module Discourse +**/ +Discourse.AdminLogsIndexRoute = Discourse.Route.extend({ + redirect: function() { + this.transitionTo('adminLogs.blockedEmails'); + } +}); + +/** + The route that lists blocked email addresses. + + @class AdminLogsBlockedEmailsRoute + @extends Discourse.Route + @namespace Discourse + @module Discourse +**/ +Discourse.AdminLogsBlockedEmailsRoute = Discourse.Route.extend({ + // model: function() { + // return Discourse.BlockedEmail.findAll(); + // }, + + renderTemplate: function() { + this.render('admin/templates/logs/blocked_emails', {into: 'adminLogs'}); + }, + + setupController: function() { + return this.controllerFor('adminLogsBlockedEmails').show(); + } +}); \ No newline at end of file diff --git a/app/assets/javascripts/admin/routes/admin_routes.js b/app/assets/javascripts/admin/routes/admin_routes.js index 41ce0efc384..5e3133739bf 100644 --- a/app/assets/javascripts/admin/routes/admin_routes.js +++ b/app/assets/javascripts/admin/routes/admin_routes.js @@ -29,6 +29,10 @@ Discourse.Route.buildRoutes(function() { this.route('old', { path: '/old' }); }); + this.resource('adminLogs', { path: '/logs' }, function() { + this.route('blockedEmails', { path: '/blocked_emails' }); + }); + this.route('groups', {path: '/groups'}); this.resource('adminUsers', { path: '/users' }, function() { diff --git a/app/assets/javascripts/admin/templates/admin.js.handlebars b/app/assets/javascripts/admin/templates/admin.js.handlebars index 97ac7c58ed1..105b66316fd 100644 --- a/app/assets/javascripts/admin/templates/admin.js.handlebars +++ b/app/assets/javascripts/admin/templates/admin.js.handlebars @@ -14,6 +14,7 @@ {{/if}}
  • {{#linkTo 'adminEmail'}}{{i18n admin.email.title}}{{/linkTo}}
  • {{#linkTo 'adminFlags'}}{{i18n admin.flags.title}}{{/linkTo}}
  • +
  • {{#linkTo 'adminLogs'}}{{i18n admin.logs.title}}{{/linkTo}}
  • {{#if currentUser.admin}}
  • {{#linkTo 'admin.customize'}}{{i18n admin.customize.title}}{{/linkTo}}
  • {{#linkTo 'admin.api'}}{{i18n admin.api.title}}{{/linkTo}}
  • diff --git a/app/assets/javascripts/admin/templates/logs.js.handlebars b/app/assets/javascripts/admin/templates/logs.js.handlebars new file mode 100644 index 00000000000..7d033a1a68a --- /dev/null +++ b/app/assets/javascripts/admin/templates/logs.js.handlebars @@ -0,0 +1,9 @@ +
    +
    + +
    +
    + +{{outlet}} \ No newline at end of file diff --git a/app/assets/javascripts/admin/templates/logs/blocked_emails.js.handlebars b/app/assets/javascripts/admin/templates/logs/blocked_emails.js.handlebars new file mode 100644 index 00000000000..30f78bf45c6 --- /dev/null +++ b/app/assets/javascripts/admin/templates/logs/blocked_emails.js.handlebars @@ -0,0 +1,29 @@ +{{#if loading}} +
    {{i18n loading}}
    +{{else}} + {{#if model.length}} + + + + + + + + + + + {{#each model}} + + + + + + + + {{/each}} + +
    {{i18n admin.logs.action}}{{i18n admin.logs.match_count}}{{i18n admin.logs.last_match_at}}{{i18n admin.logs.created_at}}
    {{actionName}}{{match_count}}{{unboundAgeWithTooltip last_match_at}}{{unboundAgeWithTooltip created_at}}
    + {{else}} + No results. + {{/if}} +{{/if}} \ No newline at end of file diff --git a/app/assets/stylesheets/admin/admin_base.scss b/app/assets/stylesheets/admin/admin_base.scss index c3580bc0690..0ee4dd4a4b8 100644 --- a/app/assets/stylesheets/admin/admin_base.scss +++ b/app/assets/stylesheets/admin/admin_base.scss @@ -693,3 +693,11 @@ table { width: 600px; } } + +/* Logs */ + +.blocked-emails { + .match_count, .last_match_at, .created_at { + text-align: center; + } +} \ No newline at end of file diff --git a/app/controllers/admin/blocked_emails_controller.rb b/app/controllers/admin/blocked_emails_controller.rb new file mode 100644 index 00000000000..b121fdad71d --- /dev/null +++ b/app/controllers/admin/blocked_emails_controller.rb @@ -0,0 +1,8 @@ +class Admin::BlockedEmailsController < Admin::AdminController + + def index + blocked_emails = BlockedEmail.limit(50).order('created_at desc').to_a + render_serialized(blocked_emails, BlockedEmailSerializer) + end + +end diff --git a/app/models/blocked_email.rb b/app/models/blocked_email.rb index 0fc451a1223..b9a3b0ac475 100644 --- a/app/models/blocked_email.rb +++ b/app/models/blocked_email.rb @@ -1,3 +1,7 @@ +# A BlockedEmail record represents an email address that is being watched, +# typically when creating a new User account. If the email of the signup form +# (or some other form) matches a BlockedEmail record, an action can be +# performed based on the action_type. class BlockedEmail < ActiveRecord::Base before_validation :set_defaults diff --git a/app/serializers/blocked_email_serializer.rb b/app/serializers/blocked_email_serializer.rb new file mode 100644 index 00000000000..3d8686c0784 --- /dev/null +++ b/app/serializers/blocked_email_serializer.rb @@ -0,0 +1,11 @@ +class BlockedEmailSerializer < ApplicationSerializer + attributes :email, + :action, + :match_count, + :last_match_at, + :created_at + + def action + BlockedEmail.actions.key(object.action_type).to_s + end +end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index b27888153fb..88595615eed 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1160,6 +1160,18 @@ en: last_seen_user: "Last Seen User:" reply_key: "Reply Key" + logs: + title: "Logs" + blocked_emails: "Blocked Emails" + email: "Email Address" + action: "Action" + last_match_at: "Last Matched" + match_count: "Matches" + created_at: "Created" + actions: + block: "block" + do_nothing: "do nothing" + impersonate: title: "Impersonate User" username_or_email: "Username or Email of User" diff --git a/config/routes.rb b/config/routes.rb index 71961ebbb97..61017e772f6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -62,6 +62,10 @@ Discourse::Application.routes.draw do end end + scope '/logs' do + resources :blocked_emails, only: [:index, :create, :update, :destroy] + end + get 'customize' => 'site_customizations#index', constraints: AdminConstraint.new get 'flags' => 'flags#index' get 'flags/:filter' => 'flags#index' diff --git a/spec/controllers/admin/blocked_emails_controller_spec.rb b/spec/controllers/admin/blocked_emails_controller_spec.rb new file mode 100644 index 00000000000..0ee48d423e6 --- /dev/null +++ b/spec/controllers/admin/blocked_emails_controller_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe Admin::BlockedEmailsController do + it "is a subclass of AdminController" do + (Admin::BlockedEmailsController < Admin::AdminController).should be_true + end + + let!(:user) { log_in(:admin) } + + context '.index' do + before do + xhr :get, :index + end + + subject { response } + it { should be_success } + + it 'returns JSON' do + ::JSON.parse(subject.body).should be_a(Array) + end + end +end