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

Move use_graph_api and self_registration OIDC options into configuration

Oliver Günther47b32dd5cce2
diff --git a/app/services/users/register_user_service.rb b/app/services/users/register_user_service.rb
index dbac71737aa..59cd783227c 100644
--- a/app/services/users/register_user_service.rb
+++ b/app/services/users/register_user_service.rb
@@ -39,6 +39,7 @@ module Users
ensure_user_limit_not_reached!
register_invited_user
register_ldap_user
+ ensure_provider_not_limited!
register_omniauth_user
ensure_registration_allowed!
register_by_email_activation
@@ -56,6 +57,16 @@ module Users
private
+ ##
+ # Check whether the associated single sign-on providers
+ # allows for automatic activation of new users
+ def ensure_provider_not_limited!
+ if limited_provider?(user)
+ name = provider_name(user)
+ ServiceResult.failure(result: user, message: I18n.t('account.error_self_registration_disabled_provider', name:))
+ end
+ end
+
##
# Check whether the system allows registration
# for non-invited users
@@ -117,6 +128,17 @@ module Users
user.identity_url.blank?
end
+ def limited_provider?(user)
+ provider = provider_name(user)
+ return false if provider.blank?
+
+ OpenProject::Plugins::AuthPlugin.limit_self_registration?(provider:)
+ end
+
+ def provider_name(user)
+ user.authentication_provider&.downcase
+ end
+
def register_by_email_activation
return unless Setting::SelfRegistration.by_email?
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 0535ab58a8f..ccb2fbf960a 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -494,6 +494,9 @@ en:
error_self_registration_disabled: >
User registration is disabled on this system. Please ask an administrator to create an
account for you.
+ error_self_registration_disabled_provider: >
+ User registration is disabled for the Single sign-on provider '%{name}'. Please ask an administrator to activate the
+ account for you or change the self registration setting for this provider.
login_with_auth_provider: "or sign in with your existing account"
signup_with_auth_provider: "or sign up using"
auth_source_login: Please login as <em>%{login}</em> to activate your account.
diff --git a/modules/auth_plugins/lib/open_project/auth_plugins/engine.rb b/modules/auth_plugins/lib/open_project/auth_plugins/engine.rb
index c13fc9b40bb..0af40ed036d 100644
--- a/modules/auth_plugins/lib/open_project/auth_plugins/engine.rb
+++ b/modules/auth_plugins/lib/open_project/auth_plugins/engine.rb
@@ -38,8 +38,6 @@ module OpenProject::AuthPlugins
author_url: 'https://www.openproject.org',
bundled: true
- patch_with_namespace :Users, :RegisterUserService
-
config.to_prepare do
OpenProject::AuthPlugins::Hooks
end
diff --git a/modules/auth_plugins/lib/open_project/auth_plugins/patches/register_user_service_patch.rb b/modules/auth_plugins/lib/open_project/auth_plugins/patches/register_user_service_patch.rb
deleted file mode 100644
index f39ca3332c6..00000000000
--- a/modules/auth_plugins/lib/open_project/auth_plugins/patches/register_user_service_patch.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2023 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-
-module OpenProject::AuthPlugins::Patches::RegisterUserServicePatch
- def self.included(base) # :nodoc:
- base.prepend InstanceMethods
- end
-
- module InstanceMethods
- def skip_omniauth_user?
- super || limit_self_registration?(user)
- end
-
- def limit_self_registration?(user)
- provider = user.authentication_provider.downcase
-
- OpenProject::Plugins::AuthPlugin.limit_self_registration? provider:
- end
- end
-end
diff --git a/modules/openid_connect/app/models/openid_connect/provider.rb b/modules/openid_connect/app/models/openid_connect/provider.rb
index c97bd8d3438..c21c0edcbf2 100644
--- a/modules/openid_connect/app/models/openid_connect/provider.rb
+++ b/modules/openid_connect/app/models/openid_connect/provider.rb
@@ -4,7 +4,7 @@ module OpenIDConnect
class NewProvider < OpenStruct
def to_h
- @table.dup.delete_if { |_k, v| v.blank? }
+ @table.compact
end
end
@@ -22,29 +22,33 @@ module OpenIDConnect
delegate :to_h, to: :omniauth_provider, allow_nil: false
delegate :tenant, to: :omniauth_provider, allow_nil: false
+ delegate :configuration, to: :omniauth_provider, allow_nil: false
delegate :use_graph_api, to: :omniauth_provider, allow_nil: false
- ##
- # Controls whether or not self registration shall be limited for this provider.
- #
- # See also:
- # - OpenProject::Plugins::AuthPlugin.limit_self_registration?
- # - OpenProject::AuthPlugins::Patches::RegisterUserServicePatch
- attr_reader :limit_self_registration
-
def initialize(omniauth_provider)
@omniauth_provider = omniauth_provider
@errors = ActiveModel::Errors.new(self)
@display_name = omniauth_provider.to_h[:display_name]
- @limit_self_registration = initial_value_for_limit_self_registration
end
def self.initialize_with(params)
- do_limit = params[:limit_self_registration]
+ normalized = normalized_params(params)
+
+ # We want all providers to be limited by the self registration setting by default
+ normalized.reverse_merge!(limit_self_registration: true)
- new(NewProvider.new(params.except(:limit_self_registration))).tap do |p|
- p.limit_self_registration = String(do_limit).to_bool unless do_limit.nil?
+ new(NewProvider.new(normalized))
+ end
+
+ def self.normalized_params(params)
+ transformed = %i[limit_self_registration use_graph_api].filter_map do |key|
+ if params.key?(key)
+ value = params[key]
+ [key, ActiveRecord::Type::Boolean.new.deserialize(value)]
+ end
end
+
+ params.merge(transformed.to_h)
end
def new_record?
@@ -55,22 +59,14 @@ module OpenIDConnect
omniauth_provider.is_a?(OmniAuth::OpenIDConnect::Provider)
end
- def limit_self_registration?
- @limit_self_registration
- end
-
- def limit_self_registration=(value)
- @limit_self_registration = value
+ def limit_self_registration
+ configuration.fetch(:limit_self_registration, true)
end
def to_h
return {} if omniauth_provider.nil?
- omniauth_provider.to_h.merge(limit_self_registration: limit_self_registration?)
- end
-
- def limit_self_registration_default
- name == "google" # limit by default only for Google since anyone can sign in
+ omniauth_provider.to_h
end
def id
@@ -129,15 +125,5 @@ module OpenIDConnect
def read_attribute_for_validation(attr)
send(attr)
end
-
- private
-
- def initial_value_for_limit_self_registration
- if omniauth_provider.configuration&.has_key? :limit_self_registration
- omniauth_provider.configuration[:limit_self_registration]
- else
- limit_self_registration_default
- end
- end
end
end
diff --git a/modules/openid_connect/lib/open_project/openid_connect/engine.rb b/modules/openid_connect/lib/open_project/openid_connect/engine.rb
index 50874fb18c4..976944b5967 100644
--- a/modules/openid_connect/lib/open_project/openid_connect/engine.rb
+++ b/modules/openid_connect/lib/open_project/openid_connect/engine.rb
@@ -31,6 +31,7 @@ module OpenProject::OpenIDConnect
OmniAuth::OpenIDConnect::Providers.configure custom_options: %i[
display_name? icon? sso? issuer?
check_session_iframe? end_session_endpoint?
+ limit_self_registration? use_graph_api?
]
strategy :openid_connect do
diff --git a/modules/openid_connect/spec/models/provider_spec.rb b/modules/openid_connect/spec/models/openid_connect/provider_spec.rb
similarity index 86%
rename from modules/openid_connect/spec/models/provider_spec.rb
rename to modules/openid_connect/spec/models/openid_connect/provider_spec.rb
index 5c0357d6e07..24436814075 100644
--- a/modules/openid_connect/spec/models/provider_spec.rb
+++ b/modules/openid_connect/spec/models/openid_connect/provider_spec.rb
@@ -29,8 +29,11 @@
require 'spec_helper'
RSpec.describe OpenIDConnect::Provider do
+ let(:params) do
+ {}
+ end
let(:provider) do
- described_class.initialize_with name: "azure", identifier: "id", secret: "secret"
+ described_class.initialize_with({ name: "azure", identifier: "id", secret: "secret" }.merge(params))
end
def auth_plugin
@@ -44,13 +47,14 @@ RSpec.describe OpenIDConnect::Provider do
end
context 'with no limited providers' do
- it "shows the provider as unlimited" do
- expect(auth_plugin).not_to be_limit_self_registration provider: provider.name
+ it "shows the provider as limited" do
+ provider.save
+ expect(auth_plugin).to be_limit_self_registration provider: provider.name
end
context 'when set to true' do
- before do
- provider.limit_self_registration = true
+ let(:params) do
+ { limit_self_registration: true }
end
it "saving the provider makes it limited" do
@@ -61,8 +65,8 @@ RSpec.describe OpenIDConnect::Provider do
end
context 'when set to false' do
- before do
- provider.limit_self_registration = false
+ let(:params) do
+ { limit_self_registration: false }
end
it "saving the provider does nothing" do
diff --git a/spec/services/users/register_user_service_spec.rb b/spec/services/users/register_user_service_spec.rb
index 0e93116f2bd..37ee66a6ad9 100644
--- a/spec/services/users/register_user_service_spec.rb
+++ b/spec/services/users/register_user_service_spec.rb
@@ -113,7 +113,7 @@ RSpec.describe Users::RegisterUserService do
it 'fails to activate due to disabled self registration' do
expect(call).not_to be_success
expect(call.result).to eq user
- expect(call.message).to eq I18n.t('account.error_self_registration_disabled')
+ expect(call.message).to eq I18n.t('account.error_self_registration_disabled_provider', name: 'azure')
end
end
end