mirror of
https://github.com/discourse/discourse.git
synced 2024-12-14 05:03:44 +08:00
30990006a9
This reduces chances of errors where consumers of strings mutate inputs and reduces memory usage of the app. Test suite passes now, but there may be some stuff left, so we will run a few sites on a branch prior to merging
50 lines
942 B
Ruby
50 lines
942 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Roleable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
scope :admins, -> { where(admin: true) }
|
|
scope :moderators, -> { where(moderator: true) }
|
|
scope :staff, -> { where("moderator or admin ") }
|
|
end
|
|
|
|
# any user that is either a moderator or an admin
|
|
def staff?
|
|
admin || moderator
|
|
end
|
|
|
|
def regular?
|
|
!staff?
|
|
end
|
|
|
|
def grant_moderation!
|
|
set_permission('moderator', true)
|
|
end
|
|
|
|
def revoke_moderation!
|
|
set_permission('moderator', false)
|
|
end
|
|
|
|
def grant_admin!
|
|
set_permission('admin', true)
|
|
end
|
|
|
|
def revoke_admin!
|
|
set_permission('admin', false)
|
|
end
|
|
|
|
def save_and_refresh_staff_groups!
|
|
transaction do
|
|
self.save!
|
|
Group.refresh_automatic_groups!(:admins, :moderators, :staff)
|
|
end
|
|
end
|
|
|
|
def set_permission(permission_name, value)
|
|
self.public_send("#{permission_name}=", value)
|
|
save_and_refresh_staff_groups!
|
|
end
|
|
|
|
end
|