2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-06 17:07:59 -05:00
|
|
|
module Roleable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2013-06-07 11:26:06 -05:00
|
|
|
included do
|
|
|
|
scope :admins, -> { where(admin: true) }
|
|
|
|
scope :moderators, -> { where(moderator: true) }
|
|
|
|
scope :staff, -> { where("moderator or admin ") }
|
|
|
|
end
|
|
|
|
|
2013-06-06 17:07:59 -05:00
|
|
|
# any user that is either a moderator or an admin
|
|
|
|
def staff?
|
|
|
|
admin || moderator
|
|
|
|
end
|
|
|
|
|
|
|
|
def regular?
|
|
|
|
!staff?
|
|
|
|
end
|
|
|
|
|
2022-06-29 19:18:12 -05:00
|
|
|
def whisperer?
|
2023-01-09 06:20:10 -06:00
|
|
|
@whisperer ||=
|
|
|
|
begin
|
2024-02-07 17:43:34 -06:00
|
|
|
return false if SiteSetting.whispers_allowed_groups_map.empty?
|
2023-01-09 06:20:10 -06:00
|
|
|
return true if admin
|
2024-02-07 17:43:34 -06:00
|
|
|
return true if SiteSetting.whispers_allowed_groups_map.include?(primary_group_id)
|
|
|
|
group_users&.exists?(group_id: SiteSetting.whispers_allowed_groups_map)
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2022-06-29 19:18:12 -05:00
|
|
|
end
|
|
|
|
|
2013-06-06 17:07:59 -05:00
|
|
|
def grant_moderation!
|
2020-06-17 06:12:55 -05:00
|
|
|
return if moderator
|
2023-01-09 06:20:10 -06:00
|
|
|
set_permission("moderator", true)
|
2019-12-12 13:26:38 -06:00
|
|
|
auto_approve_user
|
2019-11-05 06:45:55 -06:00
|
|
|
enqueue_staff_welcome_message(:moderator)
|
2020-08-06 11:27:27 -05:00
|
|
|
set_default_notification_levels(:moderators)
|
2013-06-06 17:07:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_moderation!
|
2023-01-09 06:20:10 -06:00
|
|
|
set_permission("moderator", false)
|
2013-06-06 17:07:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def grant_admin!
|
2020-06-17 06:12:55 -05:00
|
|
|
return if admin
|
2023-01-09 06:20:10 -06:00
|
|
|
set_permission("admin", true)
|
2019-12-12 13:26:38 -06:00
|
|
|
auto_approve_user
|
2019-11-05 06:45:55 -06:00
|
|
|
enqueue_staff_welcome_message(:admin)
|
2020-08-06 11:27:27 -05:00
|
|
|
set_default_notification_levels(:admins)
|
2013-06-06 17:07:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_admin!
|
2023-01-09 06:20:10 -06:00
|
|
|
set_permission("admin", false)
|
2013-06-06 17:07:59 -05:00
|
|
|
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 20:27:05 -05:00
|
|
|
self.public_send("#{permission_name}=", value)
|
2013-06-06 17:07:59 -05:00
|
|
|
save_and_refresh_staff_groups!
|
|
|
|
end
|
|
|
|
|
2020-08-06 11:27:27 -05:00
|
|
|
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
|
|
|
|
|
2022-06-29 19:18:12 -05:00
|
|
|
def reload(options = nil)
|
|
|
|
@whisperer = nil
|
|
|
|
super(options)
|
|
|
|
end
|
|
|
|
|
2019-12-12 13:26:38 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def auto_approve_user
|
2021-12-08 11:12:24 -06:00
|
|
|
if reviewable = ReviewableUser.pending.find_by(target: self)
|
2019-12-12 13:26:38 -06:00
|
|
|
reviewable.perform(Discourse.system_user, :approve_user, send_email: false)
|
|
|
|
else
|
|
|
|
ReviewableUser.set_approved_fields!(self, Discourse.system_user)
|
|
|
|
self.save!
|
|
|
|
end
|
|
|
|
end
|
2015-04-24 12:10:43 -05:00
|
|
|
end
|