mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: should not be able to rename a system badge
This commit is contained in:
@@ -98,7 +98,7 @@ class Admin::BadgesController < Admin::AdminController
|
||||
def update_badge_from_params(badge, opts={})
|
||||
errors = []
|
||||
Badge.transaction do
|
||||
allowed = Badge.column_names.map(&:to_sym)
|
||||
allowed = Badge.column_names.map(&:to_sym)
|
||||
allowed -= [:id, :created_at, :updated_at, :grant_count]
|
||||
allowed -= Badge.protected_system_fields if badge.system?
|
||||
allowed -= [:query] unless SiteSetting.enable_badge_sql
|
||||
|
||||
@@ -4,6 +4,11 @@ class Badge < ActiveRecord::Base
|
||||
# NOTE: These badge ids are not in order! They are grouped logically.
|
||||
# When picking an id, *search* for it.
|
||||
|
||||
BasicUser = 1
|
||||
Member = 2
|
||||
Regular = 3
|
||||
Leader = 4
|
||||
|
||||
Welcome = 5
|
||||
NicePost = 6
|
||||
GoodPost = 7
|
||||
@@ -100,7 +105,7 @@ class Badge < ActiveRecord::Base
|
||||
# fields that can not be edited on system badges
|
||||
def self.protected_system_fields
|
||||
[
|
||||
:badge_type_id, :multiple_grant,
|
||||
:name, :badge_type_id, :multiple_grant,
|
||||
:target_posts, :show_posts, :query,
|
||||
:trigger, :auto_revoke, :listable
|
||||
]
|
||||
@@ -121,6 +126,18 @@ class Badge < ActiveRecord::Base
|
||||
}
|
||||
end
|
||||
|
||||
def self.ensure_consistency!
|
||||
exec_sql <<-SQL.squish
|
||||
DELETE FROM user_badges
|
||||
USING user_badges ub
|
||||
LEFT JOIN users u ON u.id = ub.user_id
|
||||
WHERE u.id IS NULL
|
||||
AND user_badges.id = ub.id
|
||||
SQL
|
||||
|
||||
Badge.find_each(&:reset_grant_count!)
|
||||
end
|
||||
|
||||
def awarded_for_trust_level?
|
||||
id <= 4
|
||||
end
|
||||
@@ -137,14 +154,10 @@ class Badge < ActiveRecord::Base
|
||||
def default_icon=(val)
|
||||
unless self.image
|
||||
self.icon ||= val
|
||||
self.icon = val if self.icon = "fa-certificate"
|
||||
self.icon = val if self.icon == "fa-certificate"
|
||||
end
|
||||
end
|
||||
|
||||
def default_name=(val)
|
||||
self.name ||= val
|
||||
end
|
||||
|
||||
def default_allow_title=(val)
|
||||
self.allow_title ||= val
|
||||
end
|
||||
@@ -156,17 +169,6 @@ class Badge < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def self.ensure_consistency!
|
||||
exec_sql <<SQL
|
||||
DELETE FROM user_badges
|
||||
USING user_badges ub
|
||||
LEFT JOIN users u ON u.id = ub.user_id
|
||||
WHERE u.id IS NULL AND user_badges.id = ub.id
|
||||
SQL
|
||||
|
||||
Badge.find_each(&:reset_grant_count!)
|
||||
end
|
||||
|
||||
def display_name
|
||||
key = "badges.#{i18n_name}.name"
|
||||
I18n.t(key, default: self.name)
|
||||
@@ -178,10 +180,7 @@ SQL
|
||||
end
|
||||
|
||||
def long_description=(val)
|
||||
if val != long_description
|
||||
self[:long_description] = val
|
||||
end
|
||||
|
||||
self[:long_description] = val if val != long_description
|
||||
val
|
||||
end
|
||||
|
||||
@@ -191,10 +190,7 @@ SQL
|
||||
end
|
||||
|
||||
def description=(val)
|
||||
if val != description
|
||||
self[:description] = val
|
||||
end
|
||||
|
||||
self[:description] = val if val != description
|
||||
val
|
||||
end
|
||||
|
||||
@@ -205,15 +201,13 @@ SQL
|
||||
|
||||
protected
|
||||
|
||||
def ensure_not_system
|
||||
unless id
|
||||
self.id = [Badge.maximum(:id) + 1, 100].max
|
||||
def ensure_not_system
|
||||
self.id = [Badge.maximum(:id) + 1, 100].max unless id
|
||||
end
|
||||
end
|
||||
|
||||
def i18n_name
|
||||
self.name.downcase.tr(' ', '_')
|
||||
end
|
||||
def i18n_name
|
||||
self.name.downcase.tr(' ', '_')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user