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

[#49760] changed latest findings on one drive storages

Andreas Pfohlabbd1ea3cc3d
diff --git a/app/contracts/oauth_clients/create_contract.rb b/app/contracts/oauth_clients/create_contract.rb
index 62d356f4687..72646e3a087 100644
--- a/app/contracts/oauth_clients/create_contract.rb
+++ b/app/contracts/oauth_clients/create_contract.rb
@@ -34,7 +34,7 @@ module OAuthClients
validates :client_id, presence: true, length: { maximum: 255 }
attribute :client_secret, writable: true
- validates :client_secret, length: { in: 1..255 }, allow_nil: true
+ validates :client_secret, presence: true, length: { maximum: 255 }
attribute :integration_type, writable: true
validates :integration_type, presence: true
diff --git a/modules/storages/app/common/storages/errors.rb b/modules/storages/app/common/storages/errors.rb
index f98e05e725e..a9a1ca3c822 100644
--- a/modules/storages/app/common/storages/errors.rb
+++ b/modules/storages/app/common/storages/errors.rb
@@ -31,5 +31,9 @@
module Storages
module Errors
class OperationNotSupported < StandardError; end
+
+ class MissingContract < StandardError; end
+
+ class ResolverStandardError < StandardError; end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/contracts.rb b/modules/storages/app/common/storages/peripherals/contracts.rb
new file mode 100644
index 00000000000..126f26b638d
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/contracts.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal:true
+
+#-- 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 Storages
+ module Peripherals
+ Contracts = Dry::Container::Namespace.new('contracts') do
+ register(:nextcloud, ::Storages::Storages::NextcloudContract)
+ register(:one_drive, ::Storages::Storages::OneDriveContract)
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/registry.rb b/modules/storages/app/common/storages/peripherals/registry.rb
index 32f0713ce1e..20c27cc7f64 100644
--- a/modules/storages/app/common/storages/peripherals/registry.rb
+++ b/modules/storages/app/common/storages/peripherals/registry.rb
@@ -37,8 +37,14 @@ module Storages
def call(container, key)
super
rescue Dry::Container::KeyError
- _, storage, operation = key.split('.')
- raise ::Storages::Errors::OperationNotSupported, "Operation #{operation} not support by provider: #{storage}"
+ case key.split('.')
+ in ['contracts', storage]
+ raise ::Storages::Errors::MissingContract, "No contract defined for provider: #{storage}"
+ in [_, storage, operation]
+ raise ::Storages::Errors::OperationNotSupported, "Operation #{operation} not support by provider: #{storage}"
+ else
+ raise ::Storages::Errors::ResolverStandardError, "Cannot resolve key #{key}."
+ end
end
end
@@ -47,5 +53,7 @@ module Storages
Registry.import StorageInteraction::Nextcloud::Queries
Registry.import StorageInteraction::Nextcloud::Commands
+
+ Registry.import Contracts
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 00948c4932e..9054f72313a 100644
--- a/modules/storages/app/contracts/storages/storages/base_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/base_contract.rb
@@ -37,28 +37,23 @@ require 'uri'
# (normally it's a model).
module Storages::Storages
class BaseContract < ::BaseContract
- MINIMAL_NEXTCLOUD_VERSION = 22
-
include ::Storages::Storages::Concerns::ManageStoragesGuarded
attribute :name
validates :name, presence: true, length: { maximum: 255 }
attribute :provider_type
- validates :provider_type, inclusion: { in: Storages::Storage::PROVIDER_TYPES }
+ validates :provider_type, inclusion: { in: Storages::Storage::PROVIDER_TYPES }, allow_nil: false
attribute :provider_fields
- validate :provider_type_strategy
+ validate :provider_type_strategy, unless: -> { errors.include?(:provider_type) }
private
def provider_type_strategy
- contract = if model.provider_type_nextcloud?
- NextcloudContract.new(model, @user, options: @options)
- else
- OneDriveContract.new(model, @user, options: @options)
- end
+ contract = ::Storages::Peripherals::Registry.resolve("contracts.#{model.short_provider_type}")
+ .new(model, @user, options: @options)
# Append the attributes defined in the internal contract
# to the list of writable attributes.
diff --git a/modules/storages/app/contracts/storages/storages/one_drive_contract.rb b/modules/storages/app/contracts/storages/storages/one_drive_contract.rb
index c730b8ceb49..006158816a4 100644
--- a/modules/storages/app/contracts/storages/storages/one_drive_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/one_drive_contract.rb
@@ -29,7 +29,7 @@
#++
module Storages::Storages
- class OneDriveContract < BaseContract
+ class OneDriveContract < ::ModelContract
attribute :host
validates :host, absence: true
end
diff --git a/modules/storages/app/controllers/storages/admin/storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages_controller.rb
index c5ce2698642..72736807ac9 100644
--- a/modules/storages/app/controllers/storages/admin/storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/storages_controller.rb
@@ -119,7 +119,7 @@ class Storages::Admin::StoragesController < ApplicationController
flash[:notice] = I18n.t(:notice_successful_update)
redirect_to edit_admin_settings_storage_path(@storage)
else
- @storage = service_result.errors
+ @errors = service_result.errors
render :edit
end
end
diff --git a/modules/storages/app/models/storages/common/configuration_checks.rb b/modules/storages/app/models/storages/common/configuration_checks.rb
index 550b8dbea87..d410a5fbd8a 100644
--- a/modules/storages/app/models/storages/common/configuration_checks.rb
+++ b/modules/storages/app/models/storages/common/configuration_checks.rb
@@ -33,7 +33,7 @@ module Storages::Common
included do
scope :configured, -> do
where.associated(:oauth_client, :oauth_application)
- .where("storages.host IS NOT NULL AND storages.name IS NOT NULL")
+ .where("storages.host IS NOT NULL AND storages.name IS NOT NULL")
end
end
@@ -42,9 +42,12 @@ module Storages::Common
end
def configuration_checks
- { host_name_configured: (host.present? && name.present?),
- openproject_oauth_application_configured: oauth_application.present?,
- storage_oauth_client_configured: oauth_client.present? }
+ { storage_oauth_client_configured: oauth_client.present? }.tap do |configuration_checks|
+ if provider_type_nextcloud?
+ configuration_checks.merge!({ openproject_oauth_application_configured: oauth_application.present?,
+ host_name_configured: (host.present? && name.present?) })
+ end
+ end
end
end
end
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index 796e6cfd4d0..e968acc7aab 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -90,7 +90,7 @@ class Storages::Storage < ApplicationRecord
def self.shorten_provider_type(provider_type)
case /Storages::(?'provider_name'.*)Storage/.match(provider_type)
in provider_name:
- provider_name.downcase
+ provider_name.underscore
else
raise ArgumentError,
"Unknown provider_type! Given: #{provider_type}. " \
diff --git a/modules/storages/app/views/storages/admin/storages/_new.html.erb b/modules/storages/app/views/storages/admin/storages/_new.html.erb
index 3efa55fff5d..a65c1f51dc9 100644
--- a/modules/storages/app/views/storages/admin/storages/_new.html.erb
+++ b/modules/storages/app/views/storages/admin/storages/_new.html.erb
@@ -28,7 +28,7 @@ See COPYRIGHT and LICENSE files for more details.
<% content_controller 'storages--storage-form',
dynamic: true,
- 'storages--storage-form-one-drive-placeholder-value': t(:"storages.provider_types.onedrive.name_placeholder"),
+ 'storages--storage-form-one-drive-placeholder-value': t(:"storages.provider_types.one_drive.name_placeholder"),
'storages--storage-form-nextcloud-placeholder-value': t(:"storages.provider_types.nextcloud.name_placeholder")
%>
diff --git a/modules/storages/app/views/storages/admin/storages/edit.html.erb b/modules/storages/app/views/storages/admin/storages/edit.html.erb
index 4b443fc159d..297e90ff914 100644
--- a/modules/storages/app/views/storages/admin/storages/edit.html.erb
+++ b/modules/storages/app/views/storages/admin/storages/edit.html.erb
@@ -26,9 +26,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See COPYRIGHT and LICENSE files for more details.
++#%>
-<% html_title t(:label_administration), t("project_module_storages"), t('label_edit_x', x: @object.name) %>
+<% html_title t(:label_administration), t("project_module_storages"), t('label_edit_x', x: @storage.name) %>
<% local_assigns[:additional_breadcrumb] = @storage.name %>
-<%= toolbar title: t('label_edit_x', x: @object.name) %>
+<%= toolbar title: t('label_edit_x', x: @storage.name) %>
<%= render(::Storages::Admin::ConfigurationChecksComponent.new(storage: @storage)) %>
diff --git a/modules/storages/app/views/storages/admin/storages/one_drive/_oauth_client.html.erb b/modules/storages/app/views/storages/admin/storages/one_drive/_oauth_client.html.erb
index fe606dd8fec..5ab62161277 100644
--- a/modules/storages/app/views/storages/admin/storages/one_drive/_oauth_client.html.erb
+++ b/modules/storages/app/views/storages/admin/storages/one_drive/_oauth_client.html.erb
@@ -46,10 +46,10 @@ See COPYRIGHT and LICENSE files for more details.
</span>
</div>
- <div class="form--field">
+ <div class="form--field -required">
<%= f.text_field :client_secret,
label: Storages::Admin::LABEL_OAUTH_CLIENT_SECRET,
- required: false,
+ required: true,
size: 40,
container_class: '-wide' %>
<span class="form--field-instructions">
diff --git a/modules/storages/app/views/storages/admin/storages/one_drive/_oauth_client_section.html.erb b/modules/storages/app/views/storages/admin/storages/one_drive/_oauth_client_section.html.erb
index 87c1510955b..5fa95a34745 100644
--- a/modules/storages/app/views/storages/admin/storages/one_drive/_oauth_client_section.html.erb
+++ b/modules/storages/app/views/storages/admin/storages/one_drive/_oauth_client_section.html.erb
@@ -30,16 +30,14 @@ See COPYRIGHT and LICENSE files for more details.
<% component.with_header(title: "#{t("storages.provider_types.#{@storage.short_provider_type}.name")} #{t(:'storages.label_oauth_client_details')}") %>
<% if @storage.oauth_client %>
- <% attributes = [{ key: "#{t("storages.provider_types.#{@storage.short_provider_type}.name")} #{Storages::Admin::LABEL_OAUTH_CLIENT_ID}",
- value: @storage.oauth_client.client_id }] %>
- <% if @storage.oauth_client.client_secret.present? %>
- <% attributes.append({ key: "#{t("storages.provider_types.#{@storage.short_provider_type}.name")} #{Storages::Admin::LABEL_OAUTH_CLIENT_SECRET}",
- value: short_secret(@storage.oauth_client.client_secret) }) %>
- <% end %>
- <% attributes.append({ key: "#{t("storages.provider_types.#{@storage.short_provider_type}.name")} #{Storages::Admin::LABEL_OAUTH_REDIRECT_URI}",
- value: oauth_client_redirect_uri(@storage.oauth_client) }) %>
-
- <% component.with_attributes(attributes) %>
+ <% component.with_attributes([
+ { key: "#{t("storages.provider_types.#{@storage.short_provider_type}.name")} #{Storages::Admin::LABEL_OAUTH_CLIENT_ID}",
+ value: @storage.oauth_client.client_id },
+ { key: "#{t("storages.provider_types.#{@storage.short_provider_type}.name")} #{Storages::Admin::LABEL_OAUTH_CLIENT_SECRET}",
+ value: short_secret(@storage.oauth_client.client_secret) },
+ { key: "#{t("storages.provider_types.#{@storage.short_provider_type}.name")} #{Storages::Admin::LABEL_OAUTH_REDIRECT_URI}",
+ value: oauth_client_redirect_uri(@storage.oauth_client) }
+ ]) %>
<%= link_to(t("storages.buttons.replace_provider_type_oauth", provider_type: t("storages.provider_types.#{@storage.short_provider_type}.name")),
new_admin_settings_storage_oauth_client_path(@storage),
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index ec22a66372f..4c8a0368832 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -107,12 +107,13 @@ en:
copy_from: "Copy this value from"
nextcloud:
integration: "Nextcloud Administration / OpenProject"
- onedrive:
+ one_drive:
integration: "OneDrive/Sharepoint"
oauth_client_id: >
- Copy the client id from the Azure portal.
+ Copy the client id from the Azure portal. This is needed to generate the redirect URI.
oauth_client_secret: >
- Adding a secret is optional. If the OAuth2 application is confidential, copy the value from the Azure portal.
+ Copy the client secret from the Azure portal. For a newly created application the secret first needs to be
+ created manually. For authorization of web applications a secret is mandatory.
missing_client_id_for_redirect_uri: "Client ID missing to provide redirect URI."
help_texts:
project_folder: >
@@ -145,7 +146,7 @@ en:
label_oauth_breadcrumb:
openproject: "OpenProject OAuth"
nextcloud: "Nextcloud OAuth"
- onedrive: "OneDrive OAuth"
+ one_drive: "OneDrive OAuth"
label_oauth_application_details: "OAuth application details"
label_oauth_client_details: "OAuth client details"
label_provider_type: "Provider type"
@@ -170,7 +171,7 @@ en:
nextcloud:
name: "Nextcloud"
name_placeholder: "e.g. Nextcloud"
- onedrive:
+ one_drive:
name: "OneDrive/Sharepoint"
name_placeholder: "e.g. OneDrive"
confirm_replace_oauth_application: "Are you sure? All users will have to authorize again against OpenProject."
diff --git a/modules/storages/spec/contracts/storages/storages/create_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/create_contract_spec.rb
index cc081bbef36..72a7c5f5ec2 100644
--- a/modules/storages/spec/contracts/storages/storages/create_contract_spec.rb
+++ b/modules/storages/spec/contracts/storages/storages/create_contract_spec.rb
@@ -37,10 +37,11 @@ RSpec.describe Storages::Storages::CreateContract do
it_behaves_like 'storage contract' do
let(:current_user) { create(:admin) }
let(:storage) do
- Storages::NextcloudStorage.new(name: storage_name,
- provider_type: storage_provider_type,
- host: storage_host,
- creator: storage_creator)
+ build_stubbed(:nextcloud_storage,
+ creator: storage_creator,
+ host: storage_host,
+ name: storage_name,
+ provider_type: storage_provider_type)
end
let(:contract) { described_class.new(storage, current_user) }
diff --git a/modules/storages/spec/features/admin_storages_spec.rb b/modules/storages/spec/features/admin_storages_spec.rb
index 2139a96c127..aa074b39907 100644
--- a/modules/storages/spec/features/admin_storages_spec.rb
+++ b/modules/storages/spec/features/admin_storages_spec.rb
@@ -50,7 +50,7 @@ RSpec.describe 'Admin storages', :storage_server_helpers, js: true do
expect(page).to have_title('New storage')
expect(page.find('.title-container')).to have_text('New storage')
expect(page).to have_select('storages_storage[provider_type]', selected: 'Nextcloud')
- expect(page).to have_field('storages_storage[name]', with: 'My Nextcloud')
+ expect(page).to have_field('storages_storage[name]', with: 'My storage')
# Test the happy path for a valid storage server (host).
# Mock a valid response (=200) for example.com, so the host validation should succeed
@@ -61,7 +61,7 @@ RSpec.describe 'Admin storages', :storage_server_helpers, js: true do
page.find_by_id('storages_storage_name').set("")
page.find_by_id('storages_storage_name').set("NC 1")
page.find_by_id('storages_storage_host').set("https://example.com")
- page.find('button[type=submit]', text: "Save and continue setup").click
+ page.click_button('Save and continue setup')
######### Step 1: End Create a storage #########
######### Step 2: Begin Show OAuth application #########
@@ -81,21 +81,21 @@ RSpec.describe 'Admin storages', :storage_server_helpers, js: true do
# Set the client_id but leave client_secret empty
page.find_by_id('oauth_client_client_id').set("0123456789")
- page.find('button[type=submit]').click
+ page.click_button('Save')
# Check that we're still on the same page
expect(page).to have_title("OAuth client details")
# Set client_id to be empty but set the client_secret
page.find_by_id('oauth_client_client_id').set("")
page.find_by_id('oauth_client_client_secret').set("1234567890")
- page.find('button[type=submit]', text: 'Save').click
+ page.click_button('Save')
# Check that we're still on the same page
expect(page).to have_title("OAuth client details")
# Both client_id and client_secret valid
page.find_by_id('oauth_client_client_id').set("0123456789")
page.find_by_id('oauth_client_client_secret').set("1234567890")
- page.find('button[type=submit]', text: 'Save').click
+ page.click_button('Save')
######### Step 3: End Add OAuthClient #########
######### Step 4: Begin Automatically managed project folders #########
@@ -156,7 +156,7 @@ RSpec.describe 'Admin storages', :storage_server_helpers, js: true do
page.find_by_id('oauth_client_client_id').set("2345678901")
page.find_by_id('oauth_client_client_secret').set("3456789012")
- page.find('button[type=submit]', text: 'Replace').click
+ page.click_button('Replace')
# Check for client_id
expect(page).to have_text("2345678901")
@@ -166,7 +166,7 @@ RSpec.describe 'Admin storages', :storage_server_helpers, js: true do
mock_server_capabilities_response("https://other.example.com", response_code: '400')
page.find_by_id('storages_storage_name').set("Other NC")
page.find_by_id('storages_storage_host').set("https://other.example.com")
- page.find('button[type=submit]', text: "Save").click
+ page.click_button('Save')
expect(page).to have_title("Edit: Other NC")
expect(page.find('.title-container')).to have_text('Edit: Other NC')
@@ -178,7 +178,7 @@ RSpec.describe 'Admin storages', :storage_server_helpers, js: true do
mock_server_capabilities_response("https://old.example.com", response_nextcloud_major_version: 18)
page.find_by_id('storages_storage_name').set("Old NC")
page.find_by_id('storages_storage_host').set("https://old.example.com")
- page.find('button[type=submit]', text: "Save").click
+ page.click_button('Save')
expect(page).to have_title("Edit: Old NC")
expect(page).to have_selector('.op-toast')
@@ -189,7 +189,7 @@ RSpec.describe 'Admin storages', :storage_server_helpers, js: true do
# Restore the mocked working server example.com
page.find_by_id('storages_storage_host').set("https://example.com")
page.find_by_id('storages_storage_name').set("Other NC")
- page.find('button[type=submit]', text: "Save").click
+ page.click_button('Save')
######### Begin Edit Automatically managed project folders #########
#
diff --git a/spec/contracts/oauth_clients/create_contract_spec.rb b/spec/contracts/oauth_clients/create_contract_spec.rb
index 6fde92f3d49..380a7f54ebd 100644
--- a/spec/contracts/oauth_clients/create_contract_spec.rb
+++ b/spec/contracts/oauth_clients/create_contract_spec.rb
@@ -48,45 +48,27 @@ RSpec.describe OAuthClients::CreateContract do
describe 'validations' do
context 'when all attributes are valid' do
include_examples 'contract is valid'
-
- context 'with client secret being nil' do
- let(:client_secret) { nil }
-
- include_examples 'contract is valid'
- end
end
- context 'when client_id is invalid' do
- context 'as it is too long' do
- let(:client_id) { 'X' * 257 }
-
- include_examples 'contract is invalid', client_id: :too_long
- end
-
- context 'as it is empty' do
- let(:client_id) { '' }
+ %i[client_id client_secret].each do |attribute_name|
+ context 'when client_id is invalid' do
+ context 'as it is too long' do
+ let(attribute_name) { 'X' * 257 }
- include_examples 'contract is invalid', client_id: :blank
- end
-
- context 'as it is nil' do
- let(:client_id) { nil }
-
- include_examples 'contract is invalid', client_id: :blank
- end
- end
+ include_examples 'contract is invalid', attribute_name => :too_long
+ end
- context 'when client_secret is invalid' do
- context 'as it is too long' do
- let(:client_secret) { 'X' * 257 }
+ context 'as it is empty' do
+ let(attribute_name) { '' }
- include_examples 'contract is invalid', client_secret: :too_long
- end
+ include_examples 'contract is invalid', attribute_name => :blank
+ end
- context 'as it is empty' do
- let(:client_secret) { '' }
+ context 'as it is nil' do
+ let(attribute_name) { nil }
- include_examples 'contract is invalid', client_secret: :too_short
+ include_examples 'contract is invalid', attribute_name => :blank
+ end
end
end