2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-24 01:25:43 +08:00
|
|
|
module Jobs
|
|
|
|
|
|
|
|
class AutomaticGroupMembership < Jobs::Base
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
group_id = args[:group_id]
|
|
|
|
raise Discourse::InvalidParameters.new(:group_id) if group_id.blank?
|
|
|
|
|
2018-05-15 07:48:30 +08:00
|
|
|
group = Group.find_by(id: group_id)
|
|
|
|
raise Discourse::InvalidParameters.new(:group_id) if group.nil?
|
2015-01-24 01:25:43 +08:00
|
|
|
|
|
|
|
return unless group.automatic_membership_retroactive
|
|
|
|
|
|
|
|
domains = group.automatic_membership_email_domains.gsub('.', '\.')
|
|
|
|
|
2017-04-27 02:47:36 +08:00
|
|
|
User.joins(:user_emails)
|
2017-07-28 09:20:09 +08:00
|
|
|
.where("user_emails.email ~* '@(#{domains})$'")
|
|
|
|
.where("users.id NOT IN (SELECT user_id FROM group_users WHERE group_users.group_id = ?)", group_id)
|
|
|
|
.activated
|
|
|
|
.where(staged: false)
|
|
|
|
.find_each do |user|
|
2017-06-15 01:20:18 +08:00
|
|
|
next unless user.email_confirmed?
|
|
|
|
group.add(user)
|
|
|
|
GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(user)
|
2015-01-24 01:25:43 +08:00
|
|
|
end
|
2016-04-04 23:03:18 +08:00
|
|
|
|
|
|
|
Group.reset_counters(group.id, :group_users)
|
2015-01-24 01:25:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|