diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb index bd0fd609ca6..e046dd032cf 100644 --- a/app/models/invite_redeemer.rb +++ b/app/models/invite_redeemer.rb @@ -40,7 +40,9 @@ InviteRedeemer = Struct.new(:invite, :email, :username, :name, :password, :user_ registration_ip_address: ip_address } - if SiteSetting.must_approve_users? && EmailValidator.can_auto_approve_user?(user.email) + if (!SiteSetting.must_approve_users && SiteSetting.invite_only) || + (SiteSetting.must_approve_users? && EmailValidator.can_auto_approve_user?(user.email)) + ReviewableUser.set_approved_fields!(user, Discourse.system_user) end @@ -79,7 +81,6 @@ InviteRedeemer = Struct.new(:invite, :email, :username, :name, :password, :user_ authenticator.finish if invite.emailed_status != Invite.emailed_status_types[:not_required] && email == invite.email && invite.email_token.present? && email_token == invite.email_token - user.email_tokens.create!(email: user.email, scope: EmailToken.scopes[:signup]) user.activate end diff --git a/spec/models/invite_redeemer_spec.rb b/spec/models/invite_redeemer_spec.rb index e782f84bdce..1cfc43127c7 100644 --- a/spec/models/invite_redeemer_spec.rb +++ b/spec/models/invite_redeemer_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true describe InviteRedeemer do + fab!(:admin) { Fabricate(:admin) } describe '.create_user_from_invite' do it "should be created correctly" do @@ -83,6 +84,32 @@ describe InviteRedeemer do expect(user.approved).to eq(false) expect(user.active).to eq(false) end + + it "approves and actives user when redeeming an invite with email token and SiteSetting.invite_only is enabled" do + SiteSetting.invite_only = true + Jobs.run_immediately! + + invite = Fabricate(:invite, + invited_by: admin, + email: 'walter.white@email.com', + emailed_status: Invite.emailed_status_types[:sent], + ) + + user = InviteRedeemer.create_user_from_invite( + invite: invite, + email: invite.email, + email_token: invite.email_token, + username: 'walter', + name: 'Walter White' + ) + + expect(user.name).to eq("Walter White") + expect(user.username).to eq("walter") + expect(user.email).to eq("walter.white@email.com") + expect(user.approved).to eq(true) + expect(user.active).to eq(true) + expect(ReviewableUser.count).to eq(0) + end end describe "#redeem" do