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

Introduced StorageAuthorizer for storages

Andreas Pfohlae6f42d34d0d
diff --git a/frontend/src/app/core/state/storages/storage.model.ts b/frontend/src/app/core/state/storages/storage.model.ts
index e96e5aff8e1..f9dd815d6a6 100644
--- a/frontend/src/app/core/state/storages/storage.model.ts
+++ b/frontend/src/app/core/state/storages/storage.model.ts
@@ -33,7 +33,7 @@ export interface IStorageHalResourceLinks extends IHalResourceLinks {
self:IHalResourceLink;
type:IHalResourceLink;
origin:IHalResourceLink;
- connectionState:IHalResourceLink;
+ authorizationState:IHalResourceLink;
}
export interface IStorage {
diff --git a/frontend/src/app/shared/components/file-links/file-link-list/file-link-list.component.ts b/frontend/src/app/shared/components/file-links/file-link-list/file-link-list.component.ts
index 94b59a5236f..1d961fa8c26 100644
--- a/frontend/src/app/shared/components/file-links/file-link-list/file-link-list.component.ts
+++ b/frontend/src/app/shared/components/file-links/file-link-list/file-link-list.component.ts
@@ -64,6 +64,9 @@ export class FileLinkListComponent extends UntilDestroyedMixin implements OnInit
private readonly storageTypeMap:{ [urn:string]:string; } = {
'urn:openproject-org:api:v3:storages:Nextcloud': 'Nextcloud',
+ 'urn:openproject-org:api:v3:storages:authorization:FailedAuthentication': 'FailedAuthentication',
+ 'urn:openproject-org:api:v3:storages:authorization:Error': 'Error',
+ 'urn:openproject-org:api:v3:storages:authorization:Connected': 'Connected',
};
private text:{
@@ -112,14 +115,14 @@ export class FileLinkListComponent extends UntilDestroyedMixin implements OnInit
}
private deriveStorageInformation(fileLinkCount:number):void {
- switch (this.storage._links?.connectionState.href) {
- case 'urn:openproject-org:api:v3:storages:connection:FailedAuthentication':
+ switch (this.storage._links?.authorizationState.href) {
+ case 'urn:openproject-org:api:v3:storages:authorization:FailedAuthentication':
this.setAuthenticationFailureState();
break;
- case 'urn:openproject-org:api:v3:storages:connection:Error':
+ case 'urn:openproject-org:api:v3:storages:authorization:Error':
this.setConnectionErrorState();
break;
- case 'urn:openproject-org:api:v3:storages:connection:Connected':
+ case 'urn:openproject-org:api:v3:storages:authorization:Connected':
if (fileLinkCount === 0) {
this.setEmptyFileLinkListState();
} else {
diff --git a/modules/storages/lib/api/v3/storages/storage_authorizer.rb b/modules/storages/lib/api/v3/storages/storage_authorizer.rb
new file mode 100644
index 00000000000..3c4c82d52c6
--- /dev/null
+++ b/modules/storages/lib/api/v3/storages/storage_authorizer.rb
@@ -0,0 +1,66 @@
+#-- 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.
+#++
+
+# This class provides definitions for API routes and endpoints for the storages namespace. It inherits the
+# functionality from the Grape REST API framework. It is mounted in lib/api/v3/root.rb.
+# `modules/storages/lib/` is a defined root directory for grape, providing a root level look up for the namespaces.
+# Hence, the modules of the class have to be represented in the directory structure.
+module API
+ module V3
+ module Storages
+ URN_CONNECTION_CONNECTED = "#{::API::V3::URN_PREFIX}storages:authorization:Connected".freeze
+ URN_CONNECTION_AUTH_FAILED = "#{::API::V3::URN_PREFIX}storages:authorization:FailedAuthorization".freeze
+ URN_CONNECTION_ERROR = "#{::API::V3::URN_PREFIX}storages:authorization:Error".freeze
+
+ class StorageAuthorizer
+ def authorize(storage)
+ throw TypeError unless storage.is_a? Storage.class
+
+ oauth_client = storage.oauth_client
+ connection_manager = ::OAuthClients::ConnectionManager.new(user: User.current, oauth_client:)
+ state = connection_manager.authorization_state
+
+ to_urn(state)
+ end
+
+ private
+
+ def to_urn(state)
+ case state
+ when :connected
+ URN_CONNECTION_CONNECTED
+ when :failed_authorization
+ URN_CONNECTION_AUTH_FAILED
+ else
+ URN_CONNECTION_ERROR
+ 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 d41c1d1e489..bb847204452 100644
--- a/modules/storages/lib/api/v3/storages/storage_representer.rb
+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb
@@ -33,12 +33,6 @@
module API
module V3
module Storages
- URN_TYPE_NEXTCLOUD = "#{::API::V3::URN_PREFIX}storages:Nextcloud".freeze
-
- URN_CONNECTION_CONNECTED = "#{::API::V3::URN_PREFIX}storages:authorization:Connected".freeze
- URN_CONNECTION_AUTH_FAILED = "#{::API::V3::URN_PREFIX}storages:authorization:FailedAuthorization".freeze
- URN_CONNECTION_ERROR = "#{::API::V3::URN_PREFIX}storages:authorization:Error".freeze
-
class StorageRepresenter < ::API::Decorators::Single
# LinkedResource module defines helper methods to describe attributes
include API::Decorators::LinkedResource
@@ -52,32 +46,24 @@ module API
date_time_property :updated_at
- # A link back to the specific object ("represented")
self_link
link :type do
{
- href: URN_TYPE_NEXTCLOUD,
+ href: "#{::API::V3::URN_PREFIX}storages:Nextcloud",
title: 'Nextcloud'
}
end
link :origin do
- {
- href: represented.host
- }
+ { href: represented.host }
end
link :authorizationState do
- oauth_client = represented.oauth_client
- connection_manager = ::OAuthClients::ConnectionManager.new(user: User.current, oauth_client:)
- state = connection_manager.authorization_state.to_s
- state_urn = "#{::API::V3::URN_PREFIX}storages:authorization:#{state.camelize}"
- state_title = I18n.t(:"oauth_client.urn_connection_status.#{state}")
- {
- href: state_urn,
- title: state_title
- }
+ state = StorageAuthorizer.authorize represented
+ title = I18n.t(:"oauth_client.urn_connection_status.#{state.split(':').last}")
+
+ { href: state, title: }
end
def _type
diff --git a/modules/storages/spec/features/show_file_links_spec.rb b/modules/storages/spec/features/show_file_links_spec.rb
index 2da5d135f02..8d46776e906 100644
--- a/modules/storages/spec/features/show_file_links_spec.rb
+++ b/modules/storages/spec/features/show_file_links_spec.rb
@@ -41,6 +41,8 @@ describe 'Showing of file links in work package', :enable_storages, type: :featu
let(:wp_page) { ::Pages::FullWorkPackage.new(work_package, project) }
before do
+ allow(::API::V3::Storages::StorageAuthorizer)
+ .to receive(:authorize).and_return(::API::V3::Storages::URN_CONNECTION_CONNECTED)
project_storage
file_link