discourse/app/controllers/admin/badges_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

232 lines
6.1 KiB
Ruby
Raw Permalink Normal View History

2019-05-02 14:57:12 +08:00
# frozen_string_literal: true
require "csv"
2014-03-05 20:52:20 +08:00
class Admin::BadgesController < Admin::AdminController
MAX_CSV_LINES = 50_000
BATCH_SIZE = 200
def index
data = {
2014-07-27 16:22:01 +08:00
badge_types: BadgeType.all.order(:id).to_a,
badge_groupings: BadgeGrouping.all.order(:position).to_a,
badges:
Badge
.includes(:badge_grouping)
.includes(:badge_type, :image_upload)
.references(:badge_grouping)
.order("enabled DESC", "badge_groupings.position, badge_type_id, badges.name")
.to_a,
protected_system_fields: Badge.protected_system_fields,
triggers: Badge.trigger_hash,
}
render_serialized(OpenStruct.new(data), AdminBadgesSerializer)
end
def preview
return render json: "preview not allowed", status: 403 unless SiteSetting.enable_badge_sql
render json:
BadgeGranter.preview(
params[:sql],
target_posts: params[:target_posts] == "true",
explain: params[:explain] == "true",
trigger: params[:trigger].to_i,
)
end
def new
end
def show
end
def award
end
def mass_award
csv_file = params.permit(:file).fetch(:file, nil)
badge = Badge.find_by(id: params[:badge_id])
raise Discourse::InvalidParameters if csv_file.try(:tempfile).nil? || badge.nil?
if !badge.enabled?
render_json_error(
I18n.t("badges.mass_award.errors.badge_disabled", badge_name: badge.display_name),
status: 422,
)
return
end
replace_badge_owners = params[:replace_badge_owners] == "true"
ensure_users_have_badge_once = params[:grant_existing_holders] != "true"
if !ensure_users_have_badge_once && !badge.multiple_grant?
render_json_error(
I18n.t(
"badges.mass_award.errors.cant_grant_multiple_times",
badge_name: badge.display_name,
),
status: 422,
)
return
end
line_number = 1
usernames = []
emails = []
File.open(csv_file) do |csv|
csv.each_line do |line|
line = CSV.parse_line(line).first&.strip
line_number += 1
if line.present?
if line.include?("@")
emails << line
else
usernames << line
end
end
if emails.size + usernames.size > MAX_CSV_LINES
return(
render_json_error I18n.t(
"badges.mass_award.errors.too_many_csv_entries",
count: MAX_CSV_LINES,
),
status: 400
)
end
end
end
BadgeGranter.revoke_all(badge) if replace_badge_owners
results =
BadgeGranter.enqueue_mass_grant_for_users(
badge,
emails: emails,
usernames: usernames,
ensure_users_have_badge_once: ensure_users_have_badge_once,
)
render json: {
unmatched_entries: results[:unmatched_entries].first(100),
matched_users_count: results[:matched_users_count],
unmatched_entries_count: results[:unmatched_entries_count],
},
status: :ok
rescue CSV::MalformedCSVError
render_json_error I18n.t("badges.mass_award.errors.invalid_csv", line_number: line_number),
status: 400
end
2014-03-05 20:52:20 +08:00
def badge_types
badge_types = BadgeType.all.to_a
render_serialized(badge_types, BadgeTypeSerializer, root: "badge_types")
end
2014-07-27 16:22:01 +08:00
def save_badge_groupings
badge_groupings = BadgeGrouping.all.order(:position).to_a
ids = params[:ids].map(&:to_i)
params[:names].each_with_index do |name, index|
id = ids[index].to_i
group = badge_groupings.find { |b| b.id == id } || BadgeGrouping.new
2014-07-27 16:22:01 +08:00
group.name = name
group.position = index
group.save
end
badge_groupings.each { |g| g.destroy unless g.system? || ids.include?(g.id) }
badge_groupings = BadgeGrouping.all.order(:position).to_a
render_serialized(badge_groupings, BadgeGroupingSerializer, root: "badge_groupings")
end
2014-03-05 20:52:20 +08:00
def create
badge = Badge.new
errors = update_badge_from_params(badge, new: true)
if errors.present?
render_json_error errors
else
StaffActionLogger.new(current_user).log_badge_creation(badge)
render_serialized(badge, AdminBadgeSerializer, root: "badge")
end
2014-03-05 20:52:20 +08:00
end
def update
badge = find_badge
errors = update_badge_from_params(badge)
if errors.present?
render_json_error errors
else
StaffActionLogger.new(current_user).log_badge_change(badge)
render_serialized(badge, AdminBadgeSerializer, root: "badge")
end
2014-03-05 20:52:20 +08:00
end
def destroy
Badge.transaction do
badge = find_badge
StaffActionLogger.new(current_user).log_badge_deletion(badge)
badge.clear_user_titles!
badge.destroy!
end
render body: nil
2014-03-05 20:52:20 +08:00
end
private
2014-03-05 20:52:20 +08:00
def find_badge
params.require(:id)
Badge.find(params[:id])
end
# Options:
# :new - reset the badge id to nil before saving
def update_badge_from_params(badge, opts = {})
errors = []
Badge.transaction do
allowed = Badge.column_names.map(&:to_sym)
allowed -= %i[id created_at updated_at grant_count]
allowed -= Badge.protected_system_fields if badge.system?
allowed -= [:query] unless SiteSetting.enable_badge_sql
params.permit(*allowed)
2019-05-07 09:27:05 +08:00
allowed.each { |key| badge.public_send("#{key}=", params[key]) if params[key] }
# Badge query contract checks
begin
if SiteSetting.enable_badge_sql
BadgeGranter.contract_checks!(
badge.query,
target_posts: badge.target_posts,
trigger: badge.trigger,
)
end
rescue => e
errors << e.message
raise ActiveRecord::Rollback
end
badge.id = nil if opts[:new]
badge.save!
2014-03-05 20:52:20 +08:00
end
2018-06-07 13:28:18 +08:00
FIX: Badge and user title interaction fixes (#8282) * Fix user title logic when badge name customized * Fix an issue where a user's title was not considered a badge granted title when the user used a badge for their title and the badge name was customized. this affected the effectiveness of revoke_ungranted_titles! which only operates on badge_granted_titles. * When a user's title is set now it is considered a badge_granted_title if the badge name OR the badge custom name from TranslationOverride is the same as the title * When a user's badge is revoked we now also revoke their title if the user's title matches the badge name OR the badge custom name from TranslationOverride * Add a user history log when the title is revoked to remove confusion about why titles are revoked * Add granted_title_badge_id to user_profile, now when we set badge_granted_title on a user profile when updating a user's title based on a badge, we also remember which badge matched the title * When badge name (or custom text) changes update titles of users in a background job * When the name of a badge changes, or in the case of system badges when their custom translation text changes, then we need to update the title of all corresponding users who have a badge_granted_title and matching granted_title_badge_id. In the case of system badges we need to first get the proper badge ID based on the translation key e.g. badges.regular.name * Add migration to backfill all granted_title_badge_ids for both normal badge name titles and titles using custom badge text.
2019-11-08 13:34:24 +08:00
if opts[:new].blank?
Jobs.enqueue(
:bulk_user_title_update,
new_title: badge.name,
granted_badge_id: badge.id,
action: Jobs::BulkUserTitleUpdate::UPDATE_ACTION,
)
end
2018-06-07 13:28:18 +08:00
errors
rescue ActiveRecord::RecordInvalid
errors.push(*badge.errors.full_messages)
2018-06-07 13:28:18 +08:00
errors
end
2014-03-05 20:52:20 +08:00
end