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

Also replace other try_to_create use

Oliver Günther4a16df14affe
diff --git a/app/models/user.rb b/app/models/user.rb
index d55895b716d..158b6fa8afb 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -234,7 +234,22 @@ class User < Principal
return nil if OpenProject::Configuration.disable_password_login?
attrs = AuthSource.authenticate(login, password)
- try_to_create(attrs) if attrs
+ return unless attrs
+
+ call = Users::CreateService
+ .new(user: User.system)
+ .call(attrs)
+
+ user = call.result
+
+ call.on_failure do |result|
+ Rails.logger.error "Failed to auto-create user from auth-source: #{result.message}"
+
+ # TODO We have no way to pass back the contract errors in this place
+ user.errors.merge! call.errors
+ end
+
+ user
end
# Returns the user who matches the given autologin +key+ or nil
diff --git a/spec/controllers/account_controller_spec.rb b/spec/controllers/account_controller_spec.rb
index c6c4d6cf90e..fb4eeafa596 100644
--- a/spec/controllers/account_controller_spec.rb
+++ b/spec/controllers/account_controller_spec.rb
@@ -188,21 +188,25 @@ describe AccountController, type: :controller do
expect(response).to redirect_to my_page_path
end
- it 'should create users on the fly' do
- allow(Setting).to receive(:self_registration).and_return('0')
- allow(Setting).to receive(:self_registration?).and_return(false)
- allow(AuthSource).to receive(:authenticate).and_return(login: 'foo',
- firstname: 'Foo',
- lastname: 'Smith',
- auth_source_id: 66)
- post :login, params: { username: 'foo', password: 'bar' }
+ context 'with an auth source' do
+ let(:auth_source) { FactoryBot.create :ldap_auth_source }
+
+ it 'creates the user on the fly' do
+ allow(Setting).to receive(:self_registration).and_return('0')
+ allow(Setting).to receive(:self_registration?).and_return(false)
+ allow(AuthSource).to receive(:authenticate).and_return(login: 'foo',
+ firstname: 'Foo',
+ lastname: 'Smith',
+ auth_source_id: auth_source.id)
+ post :login, params: { username: 'foo', password: 'bar' }
- expect(response).to redirect_to home_url(first_time_user: true)
- user = User.find_by_login('foo')
- expect(user).to be_an_instance_of User
- expect(user.auth_source_id).to eq(66)
- expect(user.current_password).to be_nil
+ expect(response).to redirect_to home_url(first_time_user: true)
+ user = User.find_by(login: 'foo')
+ expect(user).to be_an_instance_of User
+ expect(user.auth_source_id).to eq(auth_source.id)
+ expect(user.current_password).to be_nil
+ end
end
context 'with a relative url root' do
@@ -401,7 +405,7 @@ describe AccountController, type: :controller do
end
context 'with an auth source' do
- let(:auth_source_id) { 42 }
+ let(:auth_source) { FactoryBot.create :ldap_auth_source }
let(:user_attributes) do
{
@@ -409,7 +413,7 @@ describe AccountController, type: :controller do
firstname: 'Scarlet',
lastname: 'Scallywag',
- auth_source_id: auth_source_id
+ auth_source_id: auth_source.id
}
end
@@ -434,7 +438,7 @@ describe AccountController, type: :controller do
end
it 'shows the user limit error' do
- expect(response.body).to have_text "user limit reached"
+ expect(response.body).to have_text "User limit reached"
end
it 'renders the register form' do