FIX: save registration IP address for invited users

FIX: save registration IP address for invited users

diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb
index 30e06c2..1fcde9e 100644
--- a/app/controllers/invites_controller.rb
+++ b/app/controllers/invites_controller.rb
@@ -45,7 +45,7 @@ class InvitesController < ApplicationController
 
     if invite.present?
       begin
-        user = invite.redeem(username: params[:username], name: params[:name], password: params[:password], user_custom_fields: params[:user_custom_fields])
+        user = invite.redeem(username: params[:username], name: params[:name], password: params[:password], user_custom_fields: params[:user_custom_fields], ip_address: request.remote_ip)
         if user.present?
           log_on_user(user) if user.active?
           post_process_invite(user)
diff --git a/app/models/invite.rb b/app/models/invite.rb
index e845247..8374026 100644
--- a/app/models/invite.rb
+++ b/app/models/invite.rb
@@ -53,8 +53,8 @@ class Invite < ActiveRecord::Base
     invalidated_at.nil?
   end
 
-  def redeem(username: nil, name: nil, password: nil, user_custom_fields: nil)
-    InviteRedeemer.new(self, username, name, password, user_custom_fields).redeem unless expired? || destroyed? || !link_valid?
+  def redeem(username: nil, name: nil, password: nil, user_custom_fields: nil, ip_address: nil)
+    InviteRedeemer.new(self, username, name, password, user_custom_fields, ip_address).redeem unless expired? || destroyed? || !link_valid?
   end
 
   def self.invite_by_email(email, invited_by, topic = nil, group_ids = nil, custom_message = nil)
diff --git a/app/models/invite_redeemer.rb b/app/models/invite_redeemer.rb
index d43ad9a..66d2352 100644
--- a/app/models/invite_redeemer.rb
+++ b/app/models/invite_redeemer.rb
@@ -1,4 +1,4 @@
-InviteRedeemer = Struct.new(:invite, :username, :name, :password, :user_custom_fields) do
+InviteRedeemer = Struct.new(:invite, :username, :name, :password, :user_custom_fields, :ip_address) do
 
   def redeem
     Invite.transaction do
@@ -12,7 +12,7 @@ InviteRedeemer = Struct.new(:invite, :username, :name, :password, :user_custom_f
   end
 
   # extracted from User cause it is very specific to invites
-  def self.create_user_from_invite(invite, username, name, password = nil, user_custom_fields = nil)
+  def self.create_user_from_invite(invite, username, name, password = nil, user_custom_fields = nil, ip_address = nil)
     if username && UsernameValidator.new(username).valid_format? && User.username_available?(username)
       available_username = username
     else
@@ -25,7 +25,9 @@ InviteRedeemer = Struct.new(:invite, :username, :name, :password, :user_custom_f
       username: available_username,
       name: available_name,
       active: false,
-      trust_level: SiteSetting.default_invitee_trust_level
+      trust_level: SiteSetting.default_invitee_trust_level,
+      ip_address: ip_address,
+      registration_ip_address: ip_address
     }
 
     user = User.unstage(user_params)
@@ -92,7 +94,7 @@ InviteRedeemer = Struct.new(:invite, :username, :name, :password, :user_custom_f
 
   def get_invited_user
     result = get_existing_user
-    result ||= InviteRedeemer.create_user_from_invite(invite, username, name, password, user_custom_fields)
+    result ||= InviteRedeemer.create_user_from_invite(invite, username, name, password, user_custom_fields, ip_address)
     result.send_welcome_message = false
     result
   end
diff --git a/spec/models/invite_redeemer_spec.rb b/spec/models/invite_redeemer_spec.rb
index 870d2b1..45291b6 100644
--- a/spec/models/invite_redeemer_spec.rb
+++ b/spec/models/invite_redeemer_spec.rb
@@ -12,12 +12,15 @@ describe InviteRedeemer do
       expect(user.approved).to eq(true)
     end
 
-    it "can set the password too" do
+    it "can set the password and ip_address" do
       password = 's3cure5tpasSw0rD'
-      user = InviteRedeemer.create_user_from_invite(Fabricate(:invite, email: 'walter.white@email.com'), 'walter', 'Walter White', password)
+      ip_address = '192.168.1.1'
+      user = InviteRedeemer.create_user_from_invite(Fabricate(:invite, email: 'walter.white@email.com'), 'walter', 'Walter White', password, nil, ip_address)
       expect(user).to have_password
       expect(user.confirm_password?(password)).to eq(true)
       expect(user.approved).to eq(true)
+      expect(user.ip_address).to eq(ip_address)
+      expect(user.registration_ip_address).to eq(ip_address)
     end
 
     it "raises exception with record and errors" do
diff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rb
index 2737d10..22e301e 100644
--- a/spec/requests/invites_controller_spec.rb
+++ b/spec/requests/invites_controller_spec.rb
@@ -276,6 +276,9 @@ describe InvitesController do
             expect(response.status).to eq(200)
             expect(session[:current_user_id]).to eq(invite.user_id)
             expect(invite.redeemed?).to be_truthy
+            user = User.find(invite.user_id)
+            expect(user.ip_address).to be_present
+            expect(user.registration_ip_address).to be_present
           end
 
           it 'redirects to the first topic the user was invited to' do

GitHub sha: 95ada3f1

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: