The personal finance app for everyone (by everyone)
Ruby
fix(sso): preserve user-edited name across OIDC logins (#1777)
plind6402f1dd0892
diff --git a/app/models/oidc_identity.rb b/app/models/oidc_identity.rbindex fe85a1fdf1..a95daf10e8 100644--- a/app/models/oidc_identity.rb+++ b/app/models/oidc_identity.rb@@ -25,10 +25,13 @@ class OidcIdentity < ApplicationRecordgroups: groups})- # Sync name to user if provided (keep existing if IdP doesn't provide)+ # Sync name to user only when Sure has nothing on file (first link, or an+ # admin blanked the field). Edits made inside Sure must survive subsequent+ # SSO logins — previously the IdP value won unconditionally and clobbered+ # any manually-edited name on every login (#1103).user.update!(- first_name: auth.info&.first_name.presence || user.first_name,- last_name: auth.info&.last_name.presence || user.last_name+ first_name: user.first_name.presence || auth.info&.first_name.presence,+ last_name: user.last_name.presence || auth.info&.last_name.presence)# Apply role mapping based on group membershipdiff --git a/test/models/oidc_identity_test.rb b/test/models/oidc_identity_test.rbindex 43342964b6..52a807a8ff 100644--- a/test/models/oidc_identity_test.rb+++ b/test/models/oidc_identity_test.rb@@ -57,6 +57,41 @@ class OidcIdentityTest < ActiveSupport::TestCaseendend+ test "sync_user_attributes! preserves user-edited first and last name" do+ # Regression for #1103: every SSO login used to overwrite user.first_name+ # with the IdP value, clobbering edits the user made inside Sure.+ @user.update!(first_name: "Adam", last_name: "Smith")+ auth = OmniAuth::AuthHash.new(+ provider: @oidc_identity.provider,+ uid: @oidc_identity.uid,+ info: { email: @user.email, first_name: "Ben", last_name: "Jones" }+ )++ @oidc_identity.sync_user_attributes!(auth)++ @user.reload+ assert_equal "Adam", @user.first_name+ assert_equal "Smith", @user.last_name+ # Identity record itself still mirrors the IdP for audit / debugging.+ assert_equal "Ben", @oidc_identity.info["first_name"]+ assert_equal "Jones", @oidc_identity.info["last_name"]+ end++ test "sync_user_attributes! fills blank user names from the IdP" do+ @user.update!(first_name: nil, last_name: nil)+ auth = OmniAuth::AuthHash.new(+ provider: @oidc_identity.provider,+ uid: @oidc_identity.uid,+ info: { email: @user.email, first_name: "Ben", last_name: "Jones" }+ )++ @oidc_identity.sync_user_attributes!(auth)++ @user.reload+ assert_equal "Ben", @user.first_name+ assert_equal "Jones", @user.last_name+ end+test "creates from omniauth hash" doauth = OmniAuth::AuthHash.new({provider: "google_oauth2",