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

[#51305] Add Enterprise Banner and checks for OneDrive/SharePoint file storage integration (#14314)

dominic-braeunlein1591665f5d7f
diff --git a/app/services/authorization/enterprise_service.rb b/app/services/authorization/enterprise_service.rb
index 06bfbc87460..6d9431cf01d 100644
--- a/app/services/authorization/enterprise_service.rb
+++ b/app/services/authorization/enterprise_service.rb
@@ -47,6 +47,7 @@ class Authorization::EnterpriseService
two_factor_authentication
work_package_query_relation_columns
work_package_sharing
+ one_drive_sharepoint_file_storage
).freeze
def initialize(token)
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 33a79a1d5fa..a7f4dfe935b 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -3330,6 +3330,7 @@ en:
code_429: "Too many requests. Please try again later."
code_500: "An internal error has occurred."
code_500_outbound_request_failure: "An outbound request to another resource has failed with status code %{status_code}."
+ code_500_missing_enterprise_token: "The request can not be handled due to invalid or missing Enterprise token."
not_found:
work_package: "The work package you are looking for cannot be found or has been deleted."
expected:
diff --git a/docs/api/apiv3/paths/storage_files_prepare_upload.yml b/docs/api/apiv3/paths/storage_files_prepare_upload.yml
index 768ae5afdd9..f35232af33f 100644
--- a/docs/api/apiv3/paths/storage_files_prepare_upload.yml
+++ b/docs/api/apiv3/paths/storage_files_prepare_upload.yml
@@ -72,9 +72,22 @@ post:
application/hal+json:
schema:
$ref: '../components/schemas/error_response.yml'
- example:
- _type: Error
- errorIdentifier: urn:openproject-org:api:v3:errors:OutboundRequest:NotFound
- message: An outbound request to another resource has failed with status code 404.
+ examples:
+ 'outbound request failed':
+ description: |-
+ This error is returned, if the outbound request to the file storage failed with a 404, e.g. if the
+ upload location is not available.
+ value:
+ _type: Error
+ errorIdentifier: urn:openproject-org:api:v3:errors:OutboundRequest:NotFound
+ message: An outbound request to another resource has failed with status code 404.
+ 'enterprise token missing':
+ description: |-
+ This error is returned, if there is no Enterprise token available. The file upload to that storage is
+ only available in an Enterprise edition.
+ value:
+ _type: Error
+ errorIdentifier: urn:openproject-org:api:v3:errors:MissingEnterpriseToken
+ message: The request cannot be handled due to invalid or missing Enterprise token.
description: |-
Returned if the outbound request to the storage has failed with any reason.
diff --git a/frontend/src/app/features/hal/resources/error-resource.ts b/frontend/src/app/features/hal/resources/error-resource.ts
index 8d9c79297e2..195ca6a7aed 100644
--- a/frontend/src/app/features/hal/resources/error-resource.ts
+++ b/frontend/src/app/features/hal/resources/error-resource.ts
@@ -32,6 +32,7 @@ import { HttpErrorResponse } from '@angular/common/http';
export const v3ErrorIdentifierQueryInvalid = 'urn:openproject-org:api:v3:errors:InvalidQuery';
export const v3ErrorIdentifierMultipleErrors = 'urn:openproject-org:api:v3:errors:MultipleErrors';
export const v3ErrorIdentifierOutboundRequestForbidden = 'urn:openproject-org:api:v3:errors:OutboundRequest:Forbidden';
+export const v3ErrorIdentifierMissingEnterpriseToken = 'urn:openproject-org:api:v3:errors:MissingEnterpriseToken';
export interface IHalErrorBase {
_type:string;
diff --git a/frontend/src/app/shared/components/storages/storage/storage.component.ts b/frontend/src/app/shared/components/storages/storage/storage.component.ts
index 58b9d8b9167..df12601b0d1 100644
--- a/frontend/src/app/shared/components/storages/storage/storage.component.ts
+++ b/frontend/src/app/shared/components/storages/storage/storage.component.ts
@@ -100,6 +100,9 @@ import {
IStorageFileUploadResponse,
StorageUploadService,
} from 'core-app/shared/components/storages/upload/storage-upload.service';
+import {
+ IHalErrorBase, v3ErrorIdentifierMissingEnterpriseToken,
+} from 'core-app/features/hal/resources/error-resource';
@Component({
selector: 'op-storage',
@@ -418,6 +421,11 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
}
private handleUploadError(error:HttpErrorResponse, fileName:string):void {
+ if (error.status === 500 && (error.error as IHalErrorBase).errorIdentifier === v3ErrorIdentifierMissingEnterpriseToken) {
+ this.toastService.addError(error);
+ return;
+ }
+
switch (error.status) {
case 403:
this.toastService.addError(this.text.toast.uploadFailedForbidden(fileName));
diff --git a/lib/api/errors/enterprise_token_missing.rb b/lib/api/errors/enterprise_token_missing.rb
new file mode 100644
index 00000000000..e6727e12fe8
--- /dev/null
+++ b/lib/api/errors/enterprise_token_missing.rb
@@ -0,0 +1,40 @@
+#-- 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 API
+ module Errors
+ class EnterpriseTokenMissing < ErrorBase
+ identifier 'MissingEnterpriseToken'.freeze
+ code 500
+
+ def initialize
+ super I18n.t('api_v3.errors.code_500_missing_enterprise_token')
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_error_helper.rb b/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
index 2714db631a1..85c43746c15 100644
--- a/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
@@ -38,6 +38,8 @@ module Storages::Peripherals
raise API::Errors::BadRequest.new(error.log_message)
when :forbidden
raise API::Errors::OutboundRequestForbidden.new
+ when :missing_ee_token_for_one_drive
+ raise API::Errors::EnterpriseTokenMissing.new
else
raise API::Errors::InternalError.new(error.log_message)
end
diff --git a/modules/storages/app/components/storages/admin/new_storage_button_component.html.erb b/modules/storages/app/components/storages/admin/new_storage_button_component.html.erb
index 99a88c0282e..fd290f9dad0 100644
--- a/modules/storages/app/components/storages/admin/new_storage_button_component.html.erb
+++ b/modules/storages/app/components/storages/admin/new_storage_button_component.html.erb
@@ -11,7 +11,9 @@
menu.with_item(
label: I18n.t("storages.provider_types.#{short_provider_type}.name"),
href: select_provider_admin_settings_storages_path(provider: short_provider_type)
- )
+ ) do |item|
+ item.with_trailing_visual_icon(icon: "op-enterprise-addons", classes: "upsale-colored") if show_ee_icon?(provider_type:)
+ end
end
end
%>
diff --git a/modules/storages/app/components/storages/admin/new_storage_button_component.rb b/modules/storages/app/components/storages/admin/new_storage_button_component.rb
index d0f1afff7a6..d46c66385b8 100644
--- a/modules/storages/app/components/storages/admin/new_storage_button_component.rb
+++ b/modules/storages/app/components/storages/admin/new_storage_button_component.rb
@@ -42,5 +42,9 @@ module Storages::Admin
def label
I18n.t("storages.label_storage")
end
+
+ def show_ee_icon?(provider_type:)
+ ::Storages::Storage::one_drive_without_ee_token?(provider_type)
+ end
end
end
diff --git a/modules/storages/app/contracts/storages/file_links/create_contract.rb b/modules/storages/app/contracts/storages/file_links/create_contract.rb
index b7a96ae908e..d1fb3badadf 100644
--- a/modules/storages/app/contracts/storages/file_links/create_contract.rb
+++ b/modules/storages/app/contracts/storages/file_links/create_contract.rb
@@ -50,6 +50,7 @@ class Storages::FileLinks::CreateContract < ModelContract
validate :validate_storage_presence
validate :validate_user_allowed_to_manage
validate :validate_project_storage_link
+ validate :require_ee_token_for_one_drive
private
@@ -81,4 +82,10 @@ class Storages::FileLinks::CreateContract < ModelContract
errors.add(:storage, :not_linked_to_project)
end
+
+ def require_ee_token_for_one_drive
+ if model.storage && ::Storages::Storage.one_drive_without_ee_token?(model.storage.provider_type)
+ errors.add(:base, I18n.t('api_v3.errors.code_500_missing_enterprise_token'))
+ end
+ end
end
diff --git a/modules/storages/app/contracts/storages/storages/base_contract.rb b/modules/storages/app/contracts/storages/storages/base_contract.rb
index d5ac7880ef8..6170682b045 100644
--- a/modules/storages/app/contracts/storages/storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb
@@ -67,5 +67,11 @@ module Storages::Storages
contract.validate
end
end
+
+ def require_ee_token_for_one_drive
+ if ::Storages::Storage.one_drive_without_ee_token?(provider_type)
+ errors.add(:base, I18n.t('api_v3.errors.code_500_missing_enterprise_token'))
+ end
+ end
end
end
diff --git a/modules/storages/app/contracts/storages/storages/create_contract.rb b/modules/storages/app/contracts/storages/storages/create_contract.rb
index 27f799bb983..2dc320290ea 100644
--- a/modules/storages/app/contracts/storages/storages/create_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/create_contract.rb
@@ -32,6 +32,7 @@ module Storages::Storages
class CreateContract < ::Storages::Storages::BaseContract
attribute :creator
validate :creator_must_be_user
+ validate :require_ee_token_for_one_drive
private
diff --git a/modules/storages/app/controllers/storages/admin/storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages_controller.rb
index c3fd002888a..fc6c39cee05 100644
--- a/modules/storages/app/controllers/storages/admin/storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/storages_controller.rb
@@ -45,6 +45,7 @@ class Storages::Admin::StoragesController < ApplicationController
before_action :find_model_object,
only: %i[show show_oauth_application destroy edit edit_host confirm_destroy update replace_oauth_application]
before_action :ensure_valid_provider_type_selected, only: %i[select_provider]
+ before_action :require_ee_token_for_one_drive, only: %i[select_provider]
# menu_item is defined in the Redmine::MenuManager::MenuController
# module, included from ApplicationController.
@@ -80,6 +81,8 @@ class Storages::Admin::StoragesController < ApplicationController
end
end
+ def upsale; end
+
def select_provider
@object = Storages::Storage.new(provider_type: @provider_type)
service_result = ::Storages::Storages::SetAttributesService
@@ -231,4 +234,10 @@ class Storages::Admin::StoragesController < ApplicationController
:storages_storage
end
end
+
+ def require_ee_token_for_one_drive
+ if ::Storages::Storage::one_drive_without_ee_token?(@provider_type)
+ redirect_to action: :upsale
+ end
+ end
end
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index 3eb0e61ab32..1a607462b2f 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -95,6 +95,11 @@ module Storages
end
end
+ def self.one_drive_without_ee_token?(provider_type)
+ provider_type == ::Storages::Storage::PROVIDER_TYPE_ONE_DRIVE &&
+ !EnterpriseToken.allows_to?(:one_drive_sharepoint_file_storage)
+ end
+
def mark_as_unhealthy(reason: nil)
update(health_status: 'unhealthy', health_changed_at: Time.now.utc, health_reason: reason)
end
diff --git a/modules/storages/app/views/storages/admin/storages/upsale.html.erb b/modules/storages/app/views/storages/admin/storages/upsale.html.erb
new file mode 100644
index 00000000000..fc559b9738f
--- /dev/null
+++ b/modules/storages/app/views/storages/admin/storages/upsale.html.erb
@@ -0,0 +1,10 @@
+<% html_title(t(:label_administration), t('team_planner.upsale.title')) -%>
+
+<% local_assigns[:additional_breadcrumb] = t('storages.upsale.title') %>
+
+<%= render template: 'common/upsale',
+ locals: {
+ feature_title: t('storages.upsale.title'),
+ feature_description: t('storages.upsale.description'),
+ feature_reference: 'one_drive_sharepoint_file_storage',
+ } %>
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index 77595b6ba5b..6ca61b0d542 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -267,3 +267,6 @@ en:
label_error: "Error"
label_healthy: "Healthy"
checked: "Checked %{datetime}"
+ upsale:
+ title: "OneDrive/SharePoint integration"
+ description: 'Integrate your OneDrive/SharePoint as a file storage with OpenProject. Upload files and link them directly to work packages in a project.'
diff --git a/modules/storages/config/routes.rb b/modules/storages/config/routes.rb
index 56fa1c374fa..bb189de271e 100644
--- a/modules/storages/config/routes.rb
+++ b/modules/storages/config/routes.rb
@@ -47,6 +47,8 @@ OpenProject::Application.routes.draw do
get :confirm_destroy
delete :replace_oauth_application
end
+
+ get :upsale, on: :collection
end
end
end
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
index a1a00971d9a..2a6a2aaeea7 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
@@ -35,6 +35,32 @@ module API::V3::StorageFiles
Storages::Peripherals::StorageFileInfoConverter,
Storages::Peripherals::StorageParentFolderExtractor
+ helpers do
+ def validate_upload_request(body)
+ if Storages::Storage::one_drive_without_ee_token?(@storage.provider_type)
+ log_message = 'The request can not be handled due to invalid or missing Enterprise token.'
+ return ServiceResult.failure(errors: Storages::StorageError.new(code: :missing_ee_token_for_one_drive, log_message:))
+ end
+
+ case body.transform_keys(&:to_sym)
+ in { projectId: project_id, fileName: file_name, parent: parent }
+ authorize_in_project(:manage_file_links, project: Project.find(project_id))
+ ServiceResult.success(result: { file_name:, parent: }.transform_keys(&:to_s))
+ else
+ ServiceResult.failure(errors: Storages::StorageError.new(code: :bad_request,
+ log_message: 'Request body malformed!'))
+ end
+ end
+
+ def fetch_upload_link
+ ->(data) do
+ Storages::Peripherals::Registry
+ .resolve("queries.#{@storage.short_provider_type}.upload_link")
+ .call(storage: @storage, user: current_user, data:)
+ end
+ end
+ end
+
resources :files do
get do
Storages::Peripherals::Registry
@@ -65,25 +91,11 @@ module API::V3::StorageFiles
end
post :prepare_upload do
- validate = ->(_body) do
- case request_body.transform_keys(&:to_sym)
- in { projectId: project_id, fileName: file_name, parent: parent }
- authorize_in_project(:manage_file_links, project: Project.find(project_id))
- ServiceResult.success(result: { file_name:, parent: }.transform_keys(&:to_s))
- else
- ServiceResult.failure(errors: Storages::StorageError.new(code: :bad_request, log_message: 'Request body malformed!'))
- end
- end
-
- validate.call(request_body) >> ->(data) do
- Storages::Peripherals::Registry
- .resolve("queries.#{@storage.short_provider_type}.upload_link")
- .call(storage: @storage, user: current_user, data:)
- .match(
- on_success: ->(link) { API::V3::StorageFiles::StorageUploadLinkRepresenter.new(link, current_user:) },
- on_failure: ->(error) { raise_error(error) }
- )
- end
+ result = validate_upload_request(request_body) >> fetch_upload_link
+ result.match(
+ on_success: ->(link) { API::V3::StorageFiles::StorageUploadLinkRepresenter.new(link, current_user:) },
+ on_failure: ->(error) { raise_error(error) }
+ )
end
end
end
diff --git a/modules/storages/spec/contracts/storages/file_links/create_contract_spec.rb b/modules/storages/spec/contracts/storages/file_links/create_contract_spec.rb
index e1015e62b2e..3e30a610143 100644
--- a/modules/storages/spec/contracts/storages/file_links/create_contract_spec.rb
+++ b/modules/storages/spec/contracts/storages/file_links/create_contract_spec.rb
@@ -39,5 +39,17 @@ RSpec.describe Storages::FileLinks::CreateContract do
include_examples 'contract is invalid', creator: :invalid
end
+
+ context 'with one drive storage and missing ee token', with_ee: false do
+ let(:storage) { create(:one_drive_storage) }
+
+ include_examples 'contract is invalid', base: I18n.t('api_v3.errors.code_500_missing_enterprise_token')
+ end
+
+ context 'with one drive storage and valid ee token', with_ee: %i[one_drive_sharepoint_file_storage] do
+ let(:storage) { create(:one_drive_storage) }
+
+ include_examples 'contract is valid'
+ end
end
end
diff --git a/modules/storages/spec/contracts/storages/storages/one_drive_create_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/one_drive_create_contract_spec.rb
index e27e5db50ad..83bae78cbc0 100644
--- a/modules/storages/spec/contracts/storages/storages/one_drive_create_contract_spec.rb
+++ b/modules/storages/spec/contracts/storages/storages/one_drive_create_contract_spec.rb
@@ -30,7 +30,7 @@ require 'spec_helper'
require_module_spec_helper
require_relative 'shared_contract_examples'
-RSpec.describe Storages::Storages::CreateContract do
+RSpec.describe Storages::Storages::CreateContract, with_ee: %i[one_drive_sharepoint_file_storage] do
let(:storage) do
build_stubbed(:one_drive_storage,
creator: storage_creator,
@@ -45,5 +45,9 @@ RSpec.describe Storages::Storages::CreateContract do
include_examples 'contract is invalid', creator: :invalid
end
+
+ context 'without ee token', with_ee: false do
+ include_examples 'contract is invalid', base: I18n.t('api_v3.errors.code_500_missing_enterprise_token')
+ end
end
end
diff --git a/modules/storages/spec/features/admin_storages_spec.rb b/modules/storages/spec/features/admin_storages_spec.rb
index c470062d557..32e5c1f40f7 100644
--- a/modules/storages/spec/features/admin_storages_spec.rb
+++ b/modules/storages/spec/features/admin_storages_spec.rb
@@ -259,7 +259,22 @@ RSpec.describe 'Admin storages',
end
end
- context 'with OneDrive Storage' do
+ context 'with OneDrive Storage and enterprise token missing', with_ee: false do
+ it 'renders enterprise icon and redirects to upsale', :webmock do
+ visit admin_settings_storages_path
+ within('.PageHeader') { click_button("Storage") }
+
+ within_test_selector('storages-select-provider-action-menu') do
+ expect(page).to have_css('.octicon-op-enterprise-addons')
+ click_link('OneDrive/SharePoint')
+ end
+
+ expect(page).to have_current_path(upsale_admin_settings_storages_path)
+ wait_for(page).to have_text("OneDrive/SharePoint integration")
+ end
+ end
+
+ context 'with OneDrive Storage', with_ee: %i[one_drive_sharepoint_file_storage] do
it 'renders a One Drive specific multi-step form', :webmock do
visit admin_settings_storages_path
diff --git a/modules/storages/spec/requests/api/v3/storages/storage_files_api_spec.rb b/modules/storages/spec/requests/api/v3/storages/storage_files_api_spec.rb
index 3da514a1568..1c1ff026f95 100644
--- a/modules/storages/spec/requests/api/v3/storages/storage_files_api_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storage_files_api_spec.rb
@@ -46,16 +46,11 @@ RSpec.describe 'API v3 storage files', :webmock, content_type: :json do
let(:storage) { create(:nextcloud_storage, creator: current_user, oauth_application:) }
let(:project_storage) { create(:project_storage, project:, storage:) }
- 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_messages(get_authorization_uri: authorize_url, authorization_state: :connected)
- allow(OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
project_storage
login_as current_user
end
@@ -314,5 +309,17 @@ RSpec.describe 'API v3 storage files', :webmock, content_type: :json do
end
end
end
+
+ context 'with invalid request body' do
+ let(:body) { { fileNam_: "ape.png", parent: "/Pictures", projectId: project.id }.to_json }
+
+ it { expect(last_response.status).to be(400) }
+ end
+
+ context 'without ee token', with_ee: false do
+ let(:storage) { create(:one_drive_storage, creator: current_user) }
+
+ it { expect(last_response.status).to be(500) }
+ end
end
end