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

[#42874] UI for storage authorization state (Authorize, Error, Connected)

Andreas Pfohlfbe03a6df7f9
diff --git a/docs/api/apiv3/components/schemas/storage_model.yml b/docs/api/apiv3/components/schemas/storage_model.yml
index ade3b220bb0..1809279e9ee 100644
--- a/docs/api/apiv3/components/schemas/storage_model.yml
+++ b/docs/api/apiv3/components/schemas/storage_model.yml
@@ -51,12 +51,12 @@ properties:
allOf:
- $ref: './link.yml'
- description: |-
- Web uri of the storage instance
+ Web uri of the storage instance.
**Resource**: N/A
authorizationState:
allOf:
- - $ref: "./link.yml"
+ - $ref: './link.yml'
- description: |-
The urn of the storage connection state. Can be one of:
@@ -64,6 +64,19 @@ properties:
- urn:openproject-org:api:v3:storages:authorization:FailedAuthorization
- urn:openproject-org:api:v3:storages:authorization:Error
+ **Resource**: N/A
+ authorize:
+ allOf:
+ - $ref: './link.yml'
+ - description: |-
+ The link to the starting point of the authorization cycle for a configured storage provider.
+
+ # Conditions
+
+ `authorizationState` is:
+
+ - urn:openproject-org:api:v3:storages:authorization:FailedAuthorization
+
**Resource**: N/A
example:
id: 1337
@@ -80,6 +93,9 @@ example:
title: Nextcloud
origin:
href: https://nextcloud.deathstar.rocks/
- connectionState:
- href: urn:openproject-org:api:v3:storages:connection:FailedAuthentication
- title: Failed Authentication
+ authorizationState:
+ href: urn:openproject-org:api:v3:storages:authorization:FailedAuthorization
+ title: Failed Authorization
+ authorize:
+ href: https://nextcloud.deathstar.rocks/authorize/
+ title: Authorize
diff --git a/modules/storages/lib/api/v3/storages/storage_authorizer.rb b/modules/storages/lib/api/v3/storages/storage_authorizer.rb
deleted file mode 100644
index 1b77c0b5fb0..00000000000
--- a/modules/storages/lib/api/v3/storages/storage_authorizer.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2022 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 API
- module V3
- module Storages
- class StorageAuthorizer
- class << self
- def authorize(storage)
- ::OAuthClients::ConnectionManager
- .new(user: User.current, oauth_client: storage.oauth_client)
- .authorization_state
- end
- end
- end
- end
- end
-end
diff --git a/modules/storages/lib/api/v3/storages/storage_representer.rb b/modules/storages/lib/api/v3/storages/storage_representer.rb
index ca3f3528ea9..4e68f06ac42 100644
--- a/modules/storages/lib/api/v3/storages/storage_representer.rb
+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb
@@ -42,6 +42,13 @@ module API
include API::Decorators::LinkedResource
include API::Decorators::DateProperty
+ def initialize(model, current_user:, embed_links: nil)
+ @connection_manager =
+ ::OAuthClients::ConnectionManager.new(user: current_user, oauth_client: model.oauth_client)
+
+ super
+ end
+
property :id
property :name
@@ -64,8 +71,7 @@ module API
end
link :authorizationState do
- state = ::API::V3::Storages::StorageAuthorizer.authorize represented
-
+ state = @connection_manager.authorization_state
urn = case state
when :connected
URN_CONNECTION_CONNECTED
@@ -79,6 +85,12 @@ module API
{ href: urn, title: }
end
+ link :authorize do
+ next unless @connection_manager.authorization_state == :failed_authorization
+
+ { href: @connection_manager.redirect_to_oauth_authorize, title: 'Authorize' }
+ end
+
def _type
'Storage'
end
diff --git a/modules/storages/spec/features/show_file_links_spec.rb b/modules/storages/spec/features/show_file_links_spec.rb
index 1ee03fd2db3..f4796170d6f 100644
--- a/modules/storages/spec/features/show_file_links_spec.rb
+++ b/modules/storages/spec/features/show_file_links_spec.rb
@@ -39,10 +39,11 @@ describe 'Showing of file links in work package', with_flag: { storages_module_a
let(:project_storage) { create(:project_storage, project:, storage:) }
let(:file_link) { create(:file_link, container: work_package, storage:) }
let(:wp_page) { ::Pages::FullWorkPackage.new(work_package, project) }
+ let(:connection_manager) { instance_double(::OAuthClients::ConnectionManager) }
before do
- allow(::API::V3::Storages::StorageAuthorizer)
- .to receive(:authorize).and_return(:connected)
+ allow(connection_manager).to receive(:authorization_state).and_return(:connected)
+ allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
project_storage
file_link
diff --git a/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb b/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb
index 9245a757785..c2b9833441d 100644
--- a/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb
+++ b/modules/storages/spec/lib/api/v3/storages/storages_representer_rendering_spec.rb
@@ -36,15 +36,16 @@ describe ::API::V3::Storages::StorageRepresenter, 'rendering' do
subject(:generated) { representer.to_json }
- describe '_links' do
- before do
- # Mock the ConnectionManager to return :connected
- allow(::OAuthClients::ConnectionManager)
- .to receive(:new).and_return(connection_manager)
- allow(connection_manager)
- .to receive(:authorization_state).and_return(:connected)
- end
+ before do
+ allow(::OAuthClients::ConnectionManager)
+ .to receive(:new).and_return(connection_manager)
+ allow(connection_manager)
+ .to receive(:authorization_state).and_return(:connected)
+ allow(connection_manager)
+ .to receive(:redirect_to_oauth_authorize).and_return('https://example.com/authorize')
+ end
+ describe '_links' do
describe 'self' do
it_behaves_like 'has a titled link' do
let(:link) { 'self' }
diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
index b37898731c0..427f834bf63 100644
--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
@@ -60,9 +60,17 @@ describe 'API v3 file links resource', with_flag: { storages_module_active: true
create(:file_link, creator: current_user, container: work_package, storage: another_storage)
end
+ let(:connection_manager) { instance_double(::OAuthClients::ConnectionManager) }
+
subject(:response) { last_response }
before do
+ allow(::OAuthClients::ConnectionManager)
+ .to receive(:new).and_return(connection_manager)
+ allow(connection_manager)
+ .to receive(:authorization_state).and_return(:connected)
+ allow(connection_manager)
+ .to receive(:redirect_to_oauth_authorize).and_return('https://example.com/authorize')
login_as current_user
end
diff --git a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
index 6d4906db6cd..aef4b1d0da0 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
@@ -47,11 +47,17 @@ describe 'API v3 storages resource', with_flag: { storages_module_active: true }
create(:project_storage, project:, storage:)
end
+ let(:authorize_url) { 'https://example.com/authorize' }
+ let(:connection_manager) { instance_double(::OAuthClients::ConnectionManager) }
+
subject(:last_response) do
get path
end
before do
+ allow(connection_manager).to receive(:redirect_to_oauth_authorize).and_return(authorize_url)
+ allow(connection_manager).to receive(:authorization_state).and_return(:connected)
+ allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
project_storage
login_as current_user
end
@@ -105,34 +111,48 @@ describe 'API v3 storages resource', with_flag: { storages_module_active: true }
end
context 'when OAuth authorization server is involved' do
- shared_examples 'a storage authorization result' do |authorization_state:, expected:|
- let(:connection_manager) { instance_double(::OAuthClients::ConnectionManager) }
-
+ shared_examples 'a storage authorization result' do |expected:, has_authorize_link:|
subject { last_response.body }
before do
allow(connection_manager).to receive(:authorization_state).and_return(authorization_state)
- allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
end
it "returns #{expected}" do
expect(subject).to be_json_eql(expected.to_json).at_path('_links/authorizationState/href')
end
+
+ it "has #{has_authorize_link ? '' : 'no'} authorize link" do
+ if has_authorize_link
+ expect(subject).to be_json_eql(authorize_url.to_json).at_path('_links/authorize/href')
+ else
+ expect(subject).not_to have_json_path('_links/authorize/href')
+ end
+ end
end
context 'when authorization succeeds and storage is connected' do
+ let(:authorization_state) { :connected }
+
include_examples 'a storage authorization result',
- authorization_state: :connected, expected: ::API::V3::Storages::URN_CONNECTION_CONNECTED
+ expected: ::API::V3::Storages::URN_CONNECTION_CONNECTED,
+ has_authorize_link: false
end
context 'when authorization fails' do
+ let(:authorization_state) { :failed_authorization }
+
include_examples 'a storage authorization result',
- authorization_state: :failed_authorization, expected: ::API::V3::Storages::URN_CONNECTION_AUTH_FAILED
+ expected: ::API::V3::Storages::URN_CONNECTION_AUTH_FAILED,
+ has_authorize_link: true
end
context 'when authorization fails with an error' do
+ let(:authorization_state) { :error }
+
include_examples 'a storage authorization result',
- authorization_state: :error, expected: ::API::V3::Storages::URN_CONNECTION_ERROR
+ expected: ::API::V3::Storages::URN_CONNECTION_ERROR,
+ has_authorize_link: false
end
end
end