OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[38706] Make login lookups in auth source sso case-insensitive
Oliver Güntherb8bb7546b06f
diff --git a/app/controllers/concerns/auth_source_sso.rb b/app/controllers/concerns/auth_source_sso.rbindex d43efcb4690..443bacb2237 100644--- a/app/controllers/concerns/auth_source_sso.rb+++ b/app/controllers/concerns/auth_source_sso.rb@@ -61,11 +61,11 @@ module AuthSourceSSOenddef header_name- sso_config && sso_config[:header]+ sso_config && sso_config[:header].to_senddef header_secret- sso_config && sso_config[:secret]+ sso_config && sso_config[:secret].to_senddef header_optional?@@ -104,7 +104,10 @@ module AuthSourceSSOenddef find_user_from_auth_source(login)- User.where(login: login).where.not(auth_source_id: nil).first+ User+ .by_login(login)+ .where.not(auth_source_id: nil)+ .firstenddef create_user_from_auth_source(login)diff --git a/app/models/user.rb b/app/models/user.rbindex b98adbb4b0d..ec06f3b1658 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -380,15 +380,6 @@ class User < Principalpref.comments_in_reverse_order?end- # Find a user account by matching the exact login and then a case-insensitive- # version. Exact matches will be given priority.- def self.find_by_login(login)- # First look for an exact match- user = find_by(login: login)- # Fail over to case-insensitive if none was found- user || where(["LOWER(login) = ?", login.to_s.downcase]).first- end-def self.find_by_rss_key(key)return nil unless Setting.feeds_enabled?diff --git a/app/models/users/scopes/find_by_login.rb b/app/models/users/scopes/find_by_login.rbindex b047cd1d692..851bb3e25d9 100644--- a/app/models/users/scopes/find_by_login.rb+++ b/app/models/users/scopes/find_by_login.rb@@ -34,8 +34,17 @@ module Users::Scopesextend ActiveSupport::Concernclass_methods do- def self.find_by_login(login)- where(["LOWER(login) = ?", login.to_s.downcase]).first+ def by_login(login)+ where(["LOWER(login) = ?", login.to_s.downcase])+ end++ # Find a user scope by matching the exact login and then a case-insensitive+ # version. Exact matches will be given priority.+ def find_by_login(login)+ # First look for an exact match+ user = find_by(login: login)+ # Fail over to case-insensitive if none was found+ user || by_login(login).firstendendenddiff --git a/spec/controllers/concerns/auth_source_sso_spec.rb b/spec/controllers/concerns/auth_source_sso_spec.rbindex 1a0d3153108..a00ab41a031 100644--- a/spec/controllers/concerns/auth_source_sso_spec.rb+++ b/spec/controllers/concerns/auth_source_sso_spec.rb@@ -45,12 +45,20 @@ describe MyController, type: :controller dolet!(:user) { FactoryBot.create :user, login: login, auth_source_id: auth_source.id, last_login_on: 5.days.ago }let(:login) { "h.wurst" }+ shared_examples 'should log in the user' do+ it "logs in given user" do+ expect(response).to redirect_to my_page_path+ expect(user.reload.last_login_on).to be_within(10.seconds).of(Time.current)+ expect(session[:user_id]).to eq user.id+ end+ end+shared_examples "auth source sso failure" dodef attrs(user)user.attributes.slice(:login, :mail, :auth_source_id)end- it "should redirect to AccountController#sso to show the error" do+ it "redirects to AccountController#sso to show the error" doexpect(response).to redirect_to "/sso"failure = session[:auth_source_sso_failure]@@ -94,19 +102,24 @@ describe MyController, type: :controller doget :accountend- it "should log in given user" do- expect(response).to redirect_to my_page_path- expect(session[:user_id]).to eq user.id- end+ it_behaves_like 'should log in the user'context 'when the secret being null' dolet(:secret) { nil }- it "should log in given user" do- expect(response).to redirect_to my_page_path- expect(user.reload.last_login_on).to be_within(10.seconds).of(Time.now)- expect(session[:user_id]).to eq user.id- end+ it_behaves_like 'should log in the user'+ end++ context 'when the secret is a number' do+ let(:secret) { 42 }++ it_behaves_like 'should log in the user'+ end++ context 'when the header values does not match the case' do+ let(:login) { 'H.wUrSt' }++ it_behaves_like 'should log in the user'endcontext 'when the user is invited' do@@ -167,7 +180,7 @@ describe MyController, type: :controller doexpect(service).to have_received(:call).with(other_user)expect(response).to redirect_to my_page_path- expect(user.reload.last_login_on).to be_within(10.seconds).of(Time.now.utc)+ expect(user.reload.last_login_on).to be_within(10.seconds).of(Time.current)expect(session[:user_id]).to eq user.idexpect(session[:updated_at]).to be > session_update_timeend