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

Refactor OAuthClientsController#ensure_access.

Pavel Balashoue6ded51db21f
diff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rb
index cf0a9b3a98b..1f1b7fc39e5 100644
--- a/app/controllers/oauth_clients_controller.rb
+++ b/app/controllers/oauth_clients_controller.rb
@@ -73,39 +73,42 @@ class OAuthClientsController < ApplicationController
client_id = params.fetch(:oauth_client_id)
storage_id = params.fetch(:storage_id)
oauth_client = OAuthClient.find_by(client_id:, integration_id: storage_id)
- if oauth_client.nil?
- flash[:error] = [I18n.t('oauth_client.errors.oauth_client_not_found'),
- I18n.t('oauth_client.errors.oauth_client_not_found_explanation')]
- if User.current.admin?
- redirect_to admin_settings_storages_path
- else
- redirect_to root_url
- end
+ handle_absent_oauth_client unless oauth_client
+
+ connection_manager = OAuthClients::ConnectionManager.new(
+ user: User.current,
+ configuration: oauth_client.integration.oauth_configuration
+ )
+
+ # check if the origin is the same
+ destination_url = if params.fetch(:destination_url, '').start_with?(root_url)
+ params[:destination_url]
+ else
+ root_url
+ end
+ if connection_manager.authorization_state == :connected
+ redirect_to(destination_url)
else
- connection_manager = OAuthClients::ConnectionManager.new(
- user: User.current,
- configuration: oauth_client.integration.oauth_configuration
- )
-
- # check if the origin is the same
- destination_url = if params[:destination_url].present? && params[:destination_url].start_with?(root_url)
- params[:destination_url]
- else
- root_url
- end
- if connection_manager.authorization_state == :connected
- redirect_to(destination_url)
- else
- nonce = SecureRandom.uuid
- cookies["oauth_state_#{nonce}"] = { value: { href: destination_url, storageId: storage_id }.to_json, expires: 1.hour }
- redirect_to(connection_manager.get_authorization_uri(state: nonce))
- end
+ nonce = SecureRandom.uuid
+ cookies["oauth_state_#{nonce}"] = { value: { href: destination_url, storageId: storage_id }.to_json, expires: 1.hour }
+ redirect_to(connection_manager.get_authorization_uri(state: nonce))
end
end
private
+ def handle_absent_oauth_client
+ flash[:error] = [I18n.t('oauth_client.errors.oauth_client_not_found'),
+ I18n.t('oauth_client.errors.oauth_client_not_found_explanation')]
+
+ if User.current.admin?
+ redirect_to admin_settings_storages_path
+ else
+ redirect_to root_url
+ end
+ end
+
def set_oauth_state
@oauth_state = params[:state]
end