discourse/app/models/concerns/roleable.rb

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

91 lines
2.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-06-06 18:07:59 -04:00
module Roleable
extend ActiveSupport::Concern
2013-06-07 12:26:06 -04:00
included do
scope :admins, -> { where(admin: true) }
scope :moderators, -> { where(moderator: true) }
scope :staff, -> { where("moderator or admin ") }
end
2013-06-06 18:07:59 -04:00
# any user that is either a moderator or an admin
def staff?
admin || moderator
end
def regular?
!staff?
end
def whisperer?
@whisperer ||= begin
return false if !SiteSetting.enable_whispers?
return true if staff?
whispers_allowed_group_ids = SiteSetting.whispers_allowed_group_ids
return false if whispers_allowed_group_ids.blank?
return true if whispers_allowed_group_ids.include?(primary_group_id)
group_users&.exists?(group_id: whispers_allowed_group_ids)
end
end
2013-06-06 18:07:59 -04:00
def grant_moderation!
return if moderator
2013-06-06 18:07:59 -04:00
set_permission('moderator', true)
auto_approve_user
enqueue_staff_welcome_message(:moderator)
set_default_notification_levels(:moderators)
2013-06-06 18:07:59 -04:00
end
def revoke_moderation!
set_permission('moderator', false)
end
def grant_admin!
return if admin
2013-06-06 18:07:59 -04:00
set_permission('admin', true)
auto_approve_user
enqueue_staff_welcome_message(:admin)
set_default_notification_levels(:admins)
2013-06-06 18:07:59 -04: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-06 21:27:05 -04:00
self.public_send("#{permission_name}=", value)
2013-06-06 18:07:59 -04:00
save_and_refresh_staff_groups!
end
def set_default_notification_levels(group_name)
Group.set_category_and_tag_default_notification_levels!(self, group_name)
if group_name == :admins || group_name == :moderators
Group.set_category_and_tag_default_notification_levels!(self, :staff)
end
end
def reload(options = nil)
@whisperer = nil
super(options)
end
private
def auto_approve_user
if reviewable = ReviewableUser.find_by(target: self, status: Reviewable.statuses[:pending])
reviewable.perform(Discourse.system_user, :approve_user, send_email: false)
else
ReviewableUser.set_approved_fields!(self, Discourse.system_user)
self.save!
end
end
end