Skip to content

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

Use SetAttributesService when retrieving user

Oliver Günther048e9476b3a8
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index abfd3e943c5..80b852b5ada 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -270,7 +270,7 @@ class AccountController < ApplicationController
def auth_source_sso_failed
failure = session.delete :auth_source_sso_failure
login = failure[:login]
- user = find_user_from_auth_source(login) || create_user_from_auth_source(login)
+ user = find_or_create_sso_user(login, save: false)
if user.try(:new_record?)
return onthefly_creation_failed user, login: user.login, auth_source_id: user.auth_source_id
diff --git a/app/controllers/concerns/auth_source_sso.rb b/app/controllers/concerns/auth_source_sso.rb
index 4be35a567ed..2b9b2dd06ba 100644
--- a/app/controllers/concerns/auth_source_sso.rb
+++ b/app/controllers/concerns/auth_source_sso.rb
@@ -33,7 +33,7 @@ module AuthSourceSSO
Rails.logger.debug { "Starting header-based auth source SSO for #{header_name}='#{op_auth_header_value}'" }
- user = find_user_from_auth_source(login) || create_user_from_auth_source(login)
+ user = find_or_create_sso_user(login, save: true)
handle_sso_for! user, login
end
@@ -106,6 +106,10 @@ module AuthSourceSSO
end
end
+ def find_or_create_sso_user(login, save: false)
+ find_user_from_auth_source(login) || create_user_from_auth_source(login, save: save)
+ end
+
def find_user_from_auth_source(login)
User
.by_login(login)
@@ -113,13 +117,22 @@ module AuthSourceSSO
.first
end
- def create_user_from_auth_source(login)
+ def create_user_from_auth_source(login, save:)
attrs = AuthSource.find_user(login)
return unless attrs
- call = Users::CreateService
- .new(user: User.system)
- .call(attrs.merge(login: login))
+ attrs[:login] = login
+
+ call =
+ if save
+ Users::CreateService
+ .new(user: User.system)
+ .call(attrs)
+ else
+ Users::SetAttributesService
+ .new(model: User.new, user: User.system, contract_class: Users::CreateContract)
+ .call(attrs)
+ end
user = call.result
diff --git a/spec/controllers/account_controller_spec.rb b/spec/controllers/account_controller_spec.rb
index 158603d6e34..0cf2e7d2318 100644
--- a/spec/controllers/account_controller_spec.rb
+++ b/spec/controllers/account_controller_spec.rb
@@ -1010,7 +1010,7 @@ describe AccountController, type: :controller do
allow(AuthSource).to receive(:find_user).and_return attrs
end
- it "should show the account creation form with an error" do
+ it "shows the account creation form with an error" do
get :auth_source_sso_failed
expect(session[:auth_source_sso_failure]).not_to be_present
@@ -1031,7 +1031,7 @@ describe AccountController, type: :controller do
allow(AuthSource).to receive(:find_user).and_return attrs
end
- it "should show the account creation form with an error" do
+ it "shows the account creation form with an error" do
get :auth_source_sso_failed
expect(session[:auth_source_sso_failure]).not_to be_present