discourse/app/jobs/regular/automatic_group_membership.rb
Régis Hanol d6c63cc5b2 FIX: user's default group should only be set once
Setting a user's default groups based on their email address should only be done once, ie. when they confirm their email address.
Previously we were doing this everytime we'd save a user record 🤷
2017-06-14 19:20:18 +02:00

32 lines
839 B
Ruby

module Jobs
class AutomaticGroupMembership < Jobs::Base
def execute(args)
group_id = args[:group_id]
raise Discourse::InvalidParameters.new(:group_id) if group_id.blank?
group = Group.find(group_id)
return unless group.automatic_membership_retroactive
domains = group.automatic_membership_email_domains.gsub('.', '\.')
User.where("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|
next unless user.email_confirmed?
group.add(user)
GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(user)
end
Group.reset_counters(group.id, :group_users)
end
end
end