discourse/app/models/concerns/roleable.rb

52 lines
1.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-06-07 06:07:59 +08:00
module Roleable
extend ActiveSupport::Concern
2013-06-08 00:26:06 +08:00
included do
scope :admins, -> { where(admin: true) }
scope :moderators, -> { where(moderator: true) }
scope :staff, -> { where("moderator or admin ") }
end
2013-06-07 06:07:59 +08:00
# 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)
enqueue_staff_welcome_message(:moderator)
2013-06-07 06:07:59 +08:00
end
def revoke_moderation!
set_permission('moderator', false)
end
def grant_admin!
set_permission('admin', true)
enqueue_staff_welcome_message(:admin)
2013-06-07 06:07:59 +08:00
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)
2019-05-07 09:27:05 +08:00
self.public_send("#{permission_name}=", value)
2013-06-07 06:07:59 +08:00
save_and_refresh_staff_groups!
end
end