2014-03-05 20:52:20 +08:00
|
|
|
class BadgeGranter
|
|
|
|
|
|
|
|
def initialize(badge, user, opts={})
|
|
|
|
@badge, @user, @opts = badge, user, opts
|
|
|
|
@granted_by = opts[:granted_by] || Discourse.system_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.grant(badge, user, opts={})
|
|
|
|
BadgeGranter.new(badge, user, opts).grant
|
|
|
|
end
|
|
|
|
|
|
|
|
def grant
|
|
|
|
return if @granted_by and !Guardian.new(@granted_by).can_grant_badges?(@user)
|
|
|
|
|
2014-04-14 13:58:27 +08:00
|
|
|
user_badge = UserBadge.find_by(badge_id: @badge.id, user_id: @user.id)
|
2014-03-05 20:52:20 +08:00
|
|
|
|
2014-04-14 13:58:27 +08:00
|
|
|
unless user_badge
|
|
|
|
UserBadge.transaction do
|
|
|
|
user_badge = UserBadge.create!(badge: @badge, user: @user,
|
|
|
|
granted_by: @granted_by, granted_at: Time.now)
|
2014-03-05 20:52:20 +08:00
|
|
|
|
2014-04-14 13:58:27 +08:00
|
|
|
Badge.increment_counter 'grant_count', @badge.id
|
|
|
|
if @granted_by != Discourse.system_user
|
|
|
|
StaffActionLogger.new(@granted_by).log_badge_grant(user_badge)
|
|
|
|
end
|
2014-03-20 03:30:12 +08:00
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
user_badge
|
|
|
|
end
|
|
|
|
|
2014-03-20 03:30:12 +08:00
|
|
|
def self.revoke(user_badge, options={})
|
2014-03-05 20:52:20 +08:00
|
|
|
UserBadge.transaction do
|
|
|
|
user_badge.destroy!
|
|
|
|
Badge.decrement_counter 'grant_count', user_badge.badge.id
|
2014-03-20 03:30:12 +08:00
|
|
|
if options[:revoked_by]
|
|
|
|
StaffActionLogger.new(options[:revoked_by]).log_badge_revoke(user_badge)
|
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|