From 63954c1b3355f595b7a88f9a3d41eaf4ed921059 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 16 Jan 2017 16:40:05 +0800 Subject: [PATCH] FIX: Same user record being saved twice causing validation to fail. --- app/models/user.rb | 11 +++++++---- spec/models/user_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index a2c3095a41c..192dcdcf71a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -90,13 +90,13 @@ class User < ActiveRecord::Base after_create :create_user_option after_create :create_user_profile after_create :ensure_in_trust_level_group - after_create :automatic_group_membership after_create :set_default_categories_preferences after_create :trigger_user_created_event before_save :update_username_lower before_save :ensure_password_is_hashed + after_save :automatic_group_membership after_save :clear_global_notice_if_needed after_save :refresh_avatar after_save :badge_grant @@ -935,10 +935,13 @@ class User < ActiveRecord::Base Group.where(automatic: false) .where("LENGTH(COALESCE(automatic_membership_email_domains, '')) > 0") .each do |group| + domains = group.automatic_membership_email_domains.gsub('.', '\.') - if self.email =~ Regexp.new("@(#{domains})$", true) && !group.users.include?(self) - group.add(self) - GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(self) + user = User.find(self.id) + + if user.reload.email =~ Regexp.new("@(#{domains})$", true) && !group.users.include?(user) + group.add(user) + GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(user) end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2ee4a3e94e9..52cf4ea8415 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1196,6 +1196,28 @@ describe User do expect(group_history.target_user).to eq(user) end + it "get attributes from the group" do + group = Fabricate(:group, + automatic_membership_email_domains: "bar.com|wat.com", + grant_trust_level: 1, + title: "bars and wats", + primary_group: true + ) + + user = Fabricate.build(:user, + trust_level: 0, + email: "foo@bar.com", + password: "strongpassword4Uguys" + ) + + user.password_required! + user.save! + user.reload + + expect(user.title).to eq("bars and wats") + expect(user.trust_level).to eq(1) + expect(user.trust_level_locked).to eq(true) + end end describe "number_of_flags_given" do