2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-01-23 12:25:43 -05:00
|
|
|
module Jobs
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
class AutomaticGroupMembership < ::Jobs::Base
|
2015-01-23 12:25:43 -05:00
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
group_id = args[:group_id]
|
|
|
|
raise Discourse::InvalidParameters.new(:group_id) if group_id.blank?
|
|
|
|
|
2018-05-14 19:48:30 -04:00
|
|
|
group = Group.find_by(id: group_id)
|
|
|
|
raise Discourse::InvalidParameters.new(:group_id) if group.nil?
|
2015-01-23 12:25:43 -05:00
|
|
|
|
|
|
|
return unless group.automatic_membership_retroactive
|
|
|
|
|
|
|
|
domains = group.automatic_membership_email_domains.gsub('.', '\.')
|
|
|
|
|
2017-04-26 14:47:36 -04:00
|
|
|
User.joins(:user_emails)
|
2017-07-27 21:20:09 -04: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-14 13:20:18 -04:00
|
|
|
next unless user.email_confirmed?
|
2019-09-10 12:58:08 -04:00
|
|
|
group.add(user, automatic: true)
|
2017-06-14 13:20:18 -04:00
|
|
|
GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(user)
|
2015-01-23 12:25:43 -05:00
|
|
|
end
|
2016-04-04 11:03:18 -04:00
|
|
|
|
|
|
|
Group.reset_counters(group.id, :group_users)
|
2015-01-23 12:25:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|