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
Allow omniauth integrations to map admin flag, update on login
Oliver Günther35bbac8ff4dc
diff --git a/app/services/authentication/omniauth_service.rb b/app/services/authentication/omniauth_service.rbindex 4769ba8847a..f1c992f54e9 100644--- a/app/services/authentication/omniauth_service.rb+++ b/app/services/authentication/omniauth_service.rb@@ -66,10 +66,13 @@ module Authenticationupdate_user_from_omniauth!(assignable_params)# If we have a new or invited user, we still need to register them- activation_call = activate_user!+ call = activate_user!++ # Update the admin flag when present successful+ call = update_admin_flag(call) if call.success?# The user should be logged in now- tap_service_result activation_call+ tap_service_result callendprivate@@ -181,15 +184,43 @@ module Authentication::Users::SetAttributesService.new(user: User.system, model: user, contract_class: ::Users::UpdateContract).call(user_attributes)- .resultelse- # Update the user, but never change the admin flag+ # Update the user, but do not change the admin flag+ # as this call is not validated.+ # we do this separately in +update_admin_flag+::Users::UpdateService.new(user: User.system, model: user).call(user_attributes.except(:admin))endend+ def update_admin_flag(call)+ return call unless user_attributes.key?(:admin)++ new_admin = ActiveRecord::Type::Boolean.new.cast(user_attributes[:admin])+ return call if user.admin == new_admin++ ::Users::UpdateService+ .new(user: User.system, model: user)+ .call(admin: new_admin)+ .on_failure { |res| update_admin_flag_failure(res) }+ .on_success { update_admin_flag_success(new_admin) }+ end++ def update_admin_flag_success(new_admin)+ if new_admin+ OpenProject.logger.info { "[OmniAuth strategy #{strategy.name}] Granted user##{update.result.id} admin permissions" }+ else+ OpenProject.logger.info { "[OmniAuth strategy #{strategy.name}] Revoked user##{update.result.id} admin permissions" }+ end+ end++ def update_admin_flag_failure(call)+ OpenProject.logger.error do+ "[OmniAuth strategy #{strategy.name}] Failed to update admin user permissions: #{call.message}"+ end+ end+def activate_user!if activatable?::Users::RegisterUserService@@ -217,13 +248,16 @@ module Authenticationinfo = auth_hash[:info]attribute_map = {- login: info[:email],+ login: info[:login] || info[:email],mail: info[:email],firstname: info[:first_name] || info[:name],lastname: info[:last_name],- identity_url: identity_url_from_omniauth+ identity_url: identity_url_from_omniauth,}+ # Map the admin attribute if provided in an attribute mapping+ attribute_map[:admin] = ActiveRecord::Type::Boolean.new.cast(info[:admin]) if info.key?(:admin)+# Allow strategies to override mappingif strategy.respond_to?(:omniauth_hash_to_user_attributes)attribute_map.merge!(strategy.omniauth_hash_to_user_attributes(auth_hash))@@ -231,7 +265,7 @@ module Authentication# Remove any nil values to avoid# overriding existing attributes- attribute_map.compact_blank!+ attribute_map.reject! { |_, value| value.nil? || value == '' }Rails.logger.debug { "Mapped auth_hash user attributes #{attribute_map.inspect}" }attribute_mapdiff --git a/modules/auth_saml/lib/open_project/auth_saml/engine.rb b/modules/auth_saml/lib/open_project/auth_saml/engine.rbindex d00354b8f2c..895fb5efbc6 100644--- a/modules/auth_saml/lib/open_project/auth_saml/engine.rb+++ b/modules/auth_saml/lib/open_project/auth_saml/engine.rb@@ -73,12 +73,6 @@ module OpenProjectredirect_to omniauth_start_path(h[:name]) + "/spslo"end- h[:openproject_attribute_map] = Proc.new do |auth|- {}.tap do |additional|- additional[:login] = auth.info[:login] if auth.info.key? :login- additional[:admin] = auth.info[:admin] if auth.info.key? :admin- end- endh.symbolize_keysendenddiff --git a/modules/openid_connect/lib/open_project/openid_connect/engine.rb b/modules/openid_connect/lib/open_project/openid_connect/engine.rbindex 976944b5967..832e3807258 100644--- a/modules/openid_connect/lib/open_project/openid_connect/engine.rb+++ b/modules/openid_connect/lib/open_project/openid_connect/engine.rb@@ -49,14 +49,6 @@ module OpenProject::OpenIDConnect::OpenProject::OpenIDConnect::SessionMapper.handle_logout(logout_token)end- # Allow username mapping from custom 'login' claim- h[:openproject_attribute_map] = Proc.new do |auth|- {}.tap do |additional|- mapped_login = auth.dig(:info, :login)- additional[:login] = mapped_login if mapped_login.present?- end- end-hendenddiff --git a/spec/services/authentication/omniauth_service_spec.rb b/spec/services/authentication/omniauth_service_spec.rbindex 6242137ef2d..7ea75cbea94 100644--- a/spec/services/authentication/omniauth_service_spec.rb+++ b/spec/services/authentication/omniauth_service_spec.rb@@ -29,14 +29,19 @@ require 'spec_helper'RSpec.describe Authentication::OmniauthService dolet(:strategy) { double('Omniauth Strategy', name: 'saml') }+ let(:additional_info) do+ {}+ endlet(:auth_hash) doOmniAuth::AuthHash.new(provider: 'google',uid: '123545',- info: { name: 'foo',- email: auth_email,- first_name: 'foo',- last_name: 'bar' }+ info: {+ name: 'foo',+ email: auth_email,+ first_name: 'foo',+ last_name: 'bar'+ }.merge(additional_info))endlet(:auth_email) { '[email protected]' }@@ -88,7 +93,7 @@ RSpec.describe Authentication::OmniauthService dolet(:call) { instance.call }context 'with an active found user' do- let!(:user) { create(:user, login: '[email protected]', identity_url: 'google:123545') }+ shared_let(:user) { create(:user, login: '[email protected]', identity_url: 'google:123545') }it 'does not call register user service and logs in the user' doallow(Users::RegisterUserService).to receive(:new)@@ -105,6 +110,58 @@ RSpec.describe Authentication::OmniauthService doexpect(Users::RegisterUserService).not_to have_received(:new)end++ context 'if the user is to become admin' do+ let(:additional_info) { { admin: true } }++ it 'updates the user' do+ expect(user).not_to be_admin+ expect(call).to be_success+ expect(call.result).to eq user+ expect(call.result).to be_admin+ end++ context 'if the user cannot be updated for some reason' do+ let(:stub) { instance_double(Users::UpdateService) }++ it 'fails the request' do+ allow(Users::UpdateService).to receive(:new).and_return(stub)+ allow(stub).to receive(:call).and_return(ServiceResult.failure(message: 'Oh noes!'))++ expect(call).not_to be_success+ expect(call.result).to be_nil+ expect(user.reload).not_to be_admin+ end+ end+ end++ context 'if the user is the last admin and is about to be revoked' do+ let(:additional_info) { { admin: false } }++ it 'does not revoke admin status' do+ user.update!(admin: true)+ expect(user).to be_admin++ expect(call).not_to be_success+ expect(call.result).to eq user+ expect(user.reload).to be_admin+ expect(call.errors.details[:base]).to contain_exactly({ error: :one_must_be_active })+ end+ end++ context 'if the user is not the last admin and is about to be revoked' do+ let!(:other_admin) { create(:admin) }+ let(:additional_info) { { admin: false } }++ it 'revokes admin status' do+ user.update!(admin: true)+ expect(user).to be_admin++ expect(call).to be_success+ expect(call.result).to eq user+ expect(user.reload).not_to be_admin+ end+ endendcontext 'without remapping allowed',