Log badge grant/revoke to the staff actions log.
This commit is contained in:
parent
0f9ea25010
commit
dcaa069bb5
|
@ -29,7 +29,7 @@ class UserBadgesController < ApplicationController
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
BadgeGranter.revoke(user_badge)
|
BadgeGranter.revoke(user_badge, revoked_by: current_user)
|
||||||
render json: success_json
|
render json: success_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,9 @@ class UserHistory < ActiveRecord::Base
|
||||||
:notified_about_dominating_topic,
|
:notified_about_dominating_topic,
|
||||||
:suspend_user,
|
:suspend_user,
|
||||||
:unsuspend_user,
|
:unsuspend_user,
|
||||||
:facebook_no_email)
|
:facebook_no_email,
|
||||||
|
:grant_badge,
|
||||||
|
:revoke_badge)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Staff actions is a subset of all actions, used to audit actions taken by staff users.
|
# Staff actions is a subset of all actions, used to audit actions taken by staff users.
|
||||||
|
@ -34,7 +36,9 @@ class UserHistory < ActiveRecord::Base
|
||||||
:change_site_customization,
|
:change_site_customization,
|
||||||
:delete_site_customization,
|
:delete_site_customization,
|
||||||
:suspend_user,
|
:suspend_user,
|
||||||
:unsuspend_user]
|
:unsuspend_user,
|
||||||
|
:grant_badge,
|
||||||
|
:revoke_badge]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.staff_action_ids
|
def self.staff_action_ids
|
||||||
|
|
|
@ -19,15 +19,21 @@ class BadgeGranter
|
||||||
granted_by: @granted_by, granted_at: Time.now)
|
granted_by: @granted_by, granted_at: Time.now)
|
||||||
|
|
||||||
Badge.increment_counter 'grant_count', @badge.id
|
Badge.increment_counter 'grant_count', @badge.id
|
||||||
|
if @granted_by != Discourse.system_user
|
||||||
|
StaffActionLogger.new(@granted_by).log_badge_grant(user_badge)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
user_badge
|
user_badge
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.revoke(user_badge)
|
def self.revoke(user_badge, options={})
|
||||||
UserBadge.transaction do
|
UserBadge.transaction do
|
||||||
user_badge.destroy!
|
user_badge.destroy!
|
||||||
Badge.decrement_counter 'grant_count', user_badge.badge.id
|
Badge.decrement_counter 'grant_count', user_badge.badge.id
|
||||||
|
if options[:revoked_by]
|
||||||
|
StaffActionLogger.new(options[:revoked_by]).log_badge_revoke(user_badge)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -74,6 +74,24 @@ class StaffActionLogger
|
||||||
}))
|
}))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def log_badge_grant(user_badge, opts={})
|
||||||
|
raise Discourse::InvalidParameters.new('user_badge is nil') unless user_badge
|
||||||
|
UserHistory.create( params(opts).merge({
|
||||||
|
action: UserHistory.actions[:grant_badge],
|
||||||
|
target_user_id: user_badge.user_id,
|
||||||
|
details: user_badge.badge.name
|
||||||
|
}))
|
||||||
|
end
|
||||||
|
|
||||||
|
def log_badge_revoke(user_badge, opts={})
|
||||||
|
raise Discourse::InvalidParameters.new('user_badge is nil') unless user_badge
|
||||||
|
UserHistory.create( params(opts).merge({
|
||||||
|
action: UserHistory.actions[:revoke_badge],
|
||||||
|
target_user_id: user_badge.user_id,
|
||||||
|
details: user_badge.badge.name
|
||||||
|
}))
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def params(opts)
|
def params(opts)
|
||||||
|
|
|
@ -1484,6 +1484,8 @@ en:
|
||||||
delete_site_customization: "delete site customization"
|
delete_site_customization: "delete site customization"
|
||||||
suspend_user: "suspend user"
|
suspend_user: "suspend user"
|
||||||
unsuspend_user: "unsuspend user"
|
unsuspend_user: "unsuspend user"
|
||||||
|
grant_badge: "grant badge"
|
||||||
|
revoke_badge: "revoke badge"
|
||||||
screened_emails:
|
screened_emails:
|
||||||
title: "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."
|
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."
|
||||||
|
|
|
@ -36,6 +36,7 @@ describe UserBadgesController do
|
||||||
it 'grants badges from staff' do
|
it 'grants badges from staff' do
|
||||||
admin = Fabricate(:admin)
|
admin = Fabricate(:admin)
|
||||||
log_in_user admin
|
log_in_user admin
|
||||||
|
StaffActionLogger.any_instance.expects(:log_badge_grant).once
|
||||||
xhr :post, :create, badge_id: badge.id, username: user.username
|
xhr :post, :create, badge_id: badge.id, username: user.username
|
||||||
response.status.should == 200
|
response.status.should == 200
|
||||||
user_badge = UserBadge.where(user: user, badge: badge).first
|
user_badge = UserBadge.where(user: user, badge: badge).first
|
||||||
|
@ -51,6 +52,7 @@ describe UserBadgesController do
|
||||||
|
|
||||||
it 'grants badges from master api calls' do
|
it 'grants badges from master api calls' do
|
||||||
api_key = Fabricate(:api_key)
|
api_key = Fabricate(:api_key)
|
||||||
|
StaffActionLogger.any_instance.expects(:log_badge_grant).never
|
||||||
xhr :post, :create, badge_id: badge.id, username: user.username, api_key: api_key.key
|
xhr :post, :create, badge_id: badge.id, username: user.username, api_key: api_key.key
|
||||||
response.status.should == 200
|
response.status.should == 200
|
||||||
user_badge = UserBadge.where(user: user, badge: badge).first
|
user_badge = UserBadge.where(user: user, badge: badge).first
|
||||||
|
@ -71,6 +73,7 @@ describe UserBadgesController do
|
||||||
|
|
||||||
it 'revokes the badge' do
|
it 'revokes the badge' do
|
||||||
log_in :admin
|
log_in :admin
|
||||||
|
StaffActionLogger.any_instance.expects(:log_badge_revoke).once
|
||||||
xhr :delete, :destroy, id: @user_badge.id
|
xhr :delete, :destroy, id: @user_badge.id
|
||||||
response.status.should == 200
|
response.status.should == 200
|
||||||
UserBadge.where(id: @user_badge.id).first.should be_nil
|
UserBadge.where(id: @user_badge.id).first.should be_nil
|
||||||
|
|
|
@ -24,11 +24,13 @@ describe BadgeGranter do
|
||||||
|
|
||||||
it 'sets granted_by if the option is present' do
|
it 'sets granted_by if the option is present' do
|
||||||
admin = Fabricate(:admin)
|
admin = Fabricate(:admin)
|
||||||
|
StaffActionLogger.any_instance.expects(:log_badge_grant).once
|
||||||
user_badge = BadgeGranter.grant(badge, user, granted_by: admin)
|
user_badge = BadgeGranter.grant(badge, user, granted_by: admin)
|
||||||
user_badge.granted_by.should eq(admin)
|
user_badge.granted_by.should eq(admin)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'defaults granted_by to the system user' do
|
it 'defaults granted_by to the system user' do
|
||||||
|
StaffActionLogger.any_instance.expects(:log_badge_grant).never
|
||||||
user_badge = BadgeGranter.grant(badge, user)
|
user_badge = BadgeGranter.grant(badge, user)
|
||||||
user_badge.granted_by_id.should eq(Discourse.system_user.id)
|
user_badge.granted_by_id.should eq(Discourse.system_user.id)
|
||||||
end
|
end
|
||||||
|
@ -47,11 +49,13 @@ describe BadgeGranter do
|
||||||
|
|
||||||
describe 'revoke' do
|
describe 'revoke' do
|
||||||
|
|
||||||
|
let(:admin) { Fabricate(:admin) }
|
||||||
let!(:user_badge) { BadgeGranter.grant(badge, user) }
|
let!(:user_badge) { BadgeGranter.grant(badge, user) }
|
||||||
|
|
||||||
it 'revokes the badge and decrements grant_count' do
|
it 'revokes the badge and decrements grant_count' do
|
||||||
badge.reload.grant_count.should eq(1)
|
badge.reload.grant_count.should eq(1)
|
||||||
BadgeGranter.revoke(user_badge)
|
StaffActionLogger.any_instance.expects(:log_badge_revoke).with(user_badge)
|
||||||
|
BadgeGranter.revoke(user_badge, revoked_by: admin)
|
||||||
UserBadge.where(user: user, badge: badge).first.should_not be_present
|
UserBadge.where(user: user, badge: badge).first.should_not be_present
|
||||||
badge.reload.grant_count.should eq(0)
|
badge.reload.grant_count.should eq(0)
|
||||||
end
|
end
|
||||||
|
|
|
@ -150,4 +150,38 @@ describe StaffActionLogger do
|
||||||
log_record.target_user.should == user
|
log_record.target_user.should == user
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "log_badge_grant" do
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
let(:badge) { Fabricate(:badge) }
|
||||||
|
let(:user_badge) { BadgeGranter.grant(badge, user) }
|
||||||
|
|
||||||
|
it "raises an error when argument is missing" do
|
||||||
|
expect { logger.log_badge_grant(nil) }.to raise_error(Discourse::InvalidParameters)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "creates a new UserHistory record" do
|
||||||
|
log_record = logger.log_badge_grant(user_badge)
|
||||||
|
log_record.should be_valid
|
||||||
|
log_record.target_user.should == user
|
||||||
|
log_record.details.should == badge.name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "log_badge_revoke" do
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
let(:badge) { Fabricate(:badge) }
|
||||||
|
let(:user_badge) { BadgeGranter.grant(badge, user) }
|
||||||
|
|
||||||
|
it "raises an error when argument is missing" do
|
||||||
|
expect { logger.log_badge_revoke(nil) }.to raise_error(Discourse::InvalidParameters)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "creates a new UserHistory record" do
|
||||||
|
log_record = logger.log_badge_revoke(user_badge)
|
||||||
|
log_record.should be_valid
|
||||||
|
log_record.target_user.should == user
|
||||||
|
log_record.details.should == badge.name
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue