Log when a site customization is deleted

This commit is contained in:
Neil Lalonde 2013-08-21 12:03:21 -04:00
parent a95303fcd8
commit 47add6da70
11 changed files with 79 additions and 13 deletions

View File

@ -1,5 +1,6 @@
/**
The modal for viewing the details of a staff action log record.
The modal for viewing the details of a staff action log record
for when a site customization is created or changed.
@class ChangeSiteCustomizationDetailsController
@extends Discourse.Controller

View File

@ -0,0 +1,15 @@
/**
The modal for viewing the details of a staff action log record
for when a site customization is deleted.
@class DeleteSiteCustomizationDetailsController
@extends Discourse.Controller
@namespace Discourse
@uses Discourse.ModalFunctionality
@module Discourse
**/
Discourse.DeleteSiteCustomizationDetailsController = Discourse.ChangeSiteCustomizationDetailsController.extend({
onShow: function() {
this.selectPrevious();
}
});

View File

@ -33,7 +33,7 @@ Discourse.StaffActionLog = Discourse.Model.extend({
},
useModalForDetails: function() {
return (this.get('action_name') === 'change_site_customization');
return _.contains(['change_site_customization', 'delete_site_customization'], this.get('action_name'));
}.property('action_name')
});

View File

@ -9,9 +9,13 @@
</ul>
<div class="modal-body">
<div {{bindAttr class=":modal-tab :new-tab newSelected::invisible"}}>
{{#with new_value}}
{{partial "admin/templates/logs/site_customization_change_details"}}
{{/with}}
{{#if new_value}}
{{#with new_value}}
{{partial "admin/templates/logs/site_customization_change_details"}}
{{/with}}
{{else}}
{{i18n admin.logs.staff_actions.deleted}}
{{/if}}
</div>
<div {{bindAttr class=":modal-tab :previous-tab previousSelected::invisible"}}>
{{#if previous_value}}

View File

@ -1,5 +1,6 @@
/**
A modal view for details of a staff action log record in a modal.
A modal view for details of a staff action log record in a modal
for when a site customization is created or changed.
@class ChangeSiteCustomizationDetailsView
@extends Discourse.ModalBodyView

View File

@ -0,0 +1,13 @@
/**
A modal view for details of a staff action log record in a modal
for when a site customization is deleted.
@class DeleteSiteCustomizationDetailsView
@extends Discourse.ModalBodyView
@namespace Discourse
@module Discourse
**/
Discourse.DeleteSiteCustomizationDetailsView = Discourse.ModalBodyView.extend({
templateName: 'admin/templates/logs/site_customization_change_modal',
title: I18n.t('admin.logs.staff_actions.modal_title')
});

View File

@ -38,10 +38,9 @@ class Admin::SiteCustomizationsController < Admin::AdminController
def destroy
@site_customization = SiteCustomization.find(params[:id])
StaffActionLogger.new(current_user).log_site_customization_destroy(@site_customization)
@site_customization.destroy
# TODO: log this
respond_to do |format|
format.json { head :no_content }
end

View File

@ -9,7 +9,11 @@ class StaffActionLog < ActiveRecord::Base
validates_presence_of :action
def self.actions
@actions ||= Enum.new(:delete_user, :change_trust_level, :change_site_setting, :change_site_customization)
@actions ||= Enum.new( :delete_user,
:change_trust_level,
:change_site_setting,
:change_site_customization,
:delete_site_customization)
end
def self.with_filters(filters)
@ -27,7 +31,7 @@ class StaffActionLog < ActiveRecord::Base
end
def new_value_is_json?
action == StaffActionLog.actions[:change_site_customization]
[StaffActionLog.actions[:change_site_customization], StaffActionLog.actions[:delete_site_customization]].include?(action)
end
def previous_value_is_json?

View File

@ -37,14 +37,24 @@ class StaffActionLogger
}))
end
SITE_CUSTOMIZATION_LOGGED_ATTRS = ['stylesheet', 'header', 'position', 'enabled', 'key', 'override_default_style']
def log_site_customization_change(old_record, site_customization_params, opts={})
raise Discourse::InvalidParameters.new('site_customization_params is nil') unless site_customization_params
logged_attrs = ['stylesheet', 'header', 'position', 'enabled', 'key', 'override_default_style']
StaffActionLog.create( params(opts).merge({
action: StaffActionLog.actions[:change_site_customization],
subject: site_customization_params[:name],
previous_value: old_record ? old_record.attributes.slice(*logged_attrs).to_json : nil,
new_value: site_customization_params.slice(*(logged_attrs.map(&:to_sym))).to_json
previous_value: old_record ? old_record.attributes.slice(*SITE_CUSTOMIZATION_LOGGED_ATTRS).to_json : nil,
new_value: site_customization_params.slice(*(SITE_CUSTOMIZATION_LOGGED_ATTRS.map(&:to_sym))).to_json
}))
end
def log_site_customization_destroy(site_customization, opts={})
raise Discourse::InvalidParameters.new('site_customization is nil') unless site_customization
StaffActionLog.create( params(opts).merge({
action: StaffActionLog.actions[:delete_site_customization],
subject: site_customization.name,
previous_value: site_customization.attributes.slice(*SITE_CUSTOMIZATION_LOGGED_ATTRS).to_json
}))
end
@ -53,4 +63,5 @@ class StaffActionLogger
def params(opts)
{staff_user_id: @admin.id, context: opts[:context]}
end
end

View File

@ -1203,11 +1203,13 @@ en:
show: "Show"
modal_title: "Details"
no_previous: "There is no previous value."
deleted: "No new value. The record was deleted."
actions:
delete_user: "delete user"
change_trust_level: "change trust level"
change_site_setting: "change site setting"
change_site_customization: "change site customization"
delete_site_customization: "delete site customization"
screened_emails:
title: "Screened Emails"
description: "When someone tries to create a new account, the following email addresses will be checked and the registration will be blocked, or some other action performed."

View File

@ -100,4 +100,20 @@ describe StaffActionLogger do
json['header'].should == existing.header
end
end
describe "log_site_customization_destroy" do
it "raises an error when params are invalid" do
expect { logger.log_site_customization_destroy(nil) }.to raise_error(Discourse::InvalidParameters)
end
it "creates a new StaffActionLog record" do
site_customization = SiteCustomization.new(name: 'Banana', stylesheet: "body {color: yellow;}", header: "h1 {color: brown;}")
log_record = logger.log_site_customization_destroy(site_customization)
log_record.previous_value.should be_present
log_record.new_value.should be_nil
json = ::JSON.parse(log_record.previous_value)
json['stylesheet'].should == site_customization.stylesheet
json['header'].should == site_customization.header
end
end
end