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

Initial mess

Marcello Rochaf4947a995a9a
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index 0e9d877b5fa..62c4837038e 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -39,9 +39,10 @@ module OAuthClients
attr_reader :user, :oauth_client
- def initialize(user:, oauth_client:)
+ def initialize(user:, oauth_client: nil, configuration: nil)
@user = user
- @oauth_client = oauth_client
+ @oauth_client = configuration ? configuration.oauth_client : oauth_client
+ @config = configuration
end
# Main method to initiate the OAuth2 flow called by a "client" component
@@ -57,7 +58,12 @@ module OAuthClients
# Return the Nextcloud OAuth authorization URI that a user needs to open to grant access and eventually obtain
# a token.
- @redirect_url = get_authorization_uri(scope:, state:)
+ full_scope = if @config
+ @config.compute_scopes(scope)
+ else
+ scope
+ end
+ @redirect_url = get_authorization_uri(scope: full_scope, state:)
ServiceResult.failure(result: @redirect_url)
end
@@ -91,7 +97,8 @@ module OAuthClients
# @param scope (OAuth2 RFC) specifies the resources to access. Nextcloud has only one global scope.
def get_authorization_uri(scope: [], state: nil)
client = rack_oauth_client # Configure and start the rack-oauth2 client
- client.authorization_uri(scope:, state:)
+ joined_scopes = @config ? @config.compute_scopes(scope) : scope
+ client.authorization_uri(scope: joined_scopes, state:)
end
# Called by callback_page with a cryptographic "code" that indicates
@@ -144,17 +151,20 @@ module OAuthClients
# curl -H "Authorization: Bearer MY_TOKEN" -X GET 'https://my.nextcloud.org/ocs/v1.php/cloud/user' \
# -H "OCS-APIRequest: true" -H "Accept: application/json"
util = Storages::Peripherals::StorageInteraction::Nextcloud::Util
- uri = URI(oauth_client.integration.host).normalize
- response = util
- .http(uri)
- .get(
- util.join_uri_path(uri, AUTHORIZATION_CHECK_PATH),
- {
- 'Authorization' => "Bearer #{oauth_client_token.access_token}",
- 'OCS-APIRequest' => 'true',
- 'Accept' => 'application/json'
- }
- )
+ response = if @config
+ @config.authorization_state_check(oauth_client_token.access_token)
+ else
+ uri = URI(oauth_client.integration.host).normalize
+ util
+ .http(uri)
+ .get(util.join_uri_path(uri, AUTHORIZATION_CHECK_PATH),
+ {
+ 'Authorization' => "Bearer #{oauth_client_token.access_token}",
+ 'OCS-APIRequest' => 'true',
+ 'Accept' => 'application/json'
+ })
+ end
+
case response
when Net::HTTPSuccess
:connected
@@ -244,7 +254,7 @@ module OAuthClients
# Return a fully configured RackOAuth2Client.
# This client does all the heavy lifting with the OAuth2 protocol.
def rack_oauth_client(options = {})
- rack_oauth_client = build_basic_rack_oauth_client
+ rack_oauth_client = @config ? @config.basic_rack_oauth_client : build_basic_rack_oauth_client
# Write options, for example authorization_code and refresh_token
rack_oauth_client.refresh_token = options[:refresh_token] if options[:refresh_token]
diff --git a/modules/storages/app/common/storages/errors.rb b/modules/storages/app/common/storages/errors.rb
index a9a1ca3c822..7043618b2e8 100644
--- a/modules/storages/app/common/storages/errors.rb
+++ b/modules/storages/app/common/storages/errors.rb
@@ -30,10 +30,10 @@
module Storages
module Errors
- class OperationNotSupported < StandardError; end
-
- class MissingContract < StandardError; end
-
- class ResolverStandardError < StandardError; end
+ class BaseError < StandardError; end
+ class MissingContract < BaseError; end
+ class OperationNotSupported < BaseError; end
+ class ResolverStandardError < BaseError; end
+ class SubclassResponsibility < BaseError; end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
new file mode 100644
index 00000000000..64b3d4b1c78
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
@@ -0,0 +1,68 @@
+# frozen_string_literal: true
+
+#-- copyright
+#++
+
+module Storages
+ module Peripherals
+ module OAuthConfigurations
+ class NextcloudConfiguration
+ attr_reader :oauth_client
+
+ def initialize(storage)
+ @uri = storage.uri
+ @oauth_client = storage.oauth_client.freeze
+ end
+
+ def authorization_state_check(token)
+ util = StorageInteraction::Nextcloud::Util
+ # response =
+ Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
+ http.get(
+ util.join_uri_path(@uri, '/ocs/v1.php/cloud/user'),
+ {
+ 'Authorization' => "Bearer #{token}",
+ 'OCS-APIRequest' => 'true',
+ 'Accept' => 'application/json'
+ }
+ )
+ end
+ # case response
+ # when Net::HTTPSuccess
+ # :connected
+ # when Net::HTTPForbidden, Net::HTTPUnauthorized
+ # service_result = refresh_token # `refresh_token` already has exception handling
+ # if service_result.success?
+ # :connected
+ # elsif service_result.result == 'invalid_request'
+ # # This can happen if the Authorization Server invalidated all tokens.
+ # # Then the user would ideally be asked to reauthorize.
+ # :failed_authorization
+ # else
+ # # It could also be that some other error happened, i.e. firewall badly configured.
+ # # Then the user needs to know that something is technically off. The user could try
+ # # to reload the page or contact an admin.
+ # :error
+ # end
+ # end
+ end
+
+ def compute_scopes(scopes)
+ scopes
+ end
+
+ def basic_rack_oauth_client
+ Rack::OAuth2::Client.new(
+ identifier: @oauth_client.client_id,
+ secret: @oauth_client.client_secret,
+ scheme: @uri.scheme,
+ host: @uri.host,
+ port: @uri.port,
+ authorization_endpoint: File.join(@uri.path, "/index.php/apps/oauth2/authorize"),
+ token_endpoint: File.join(@uri.path, "/index.php/apps/oauth2/api/v1/token")
+ )
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb
new file mode 100644
index 00000000000..3f21031ba8f
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb
@@ -0,0 +1,62 @@
+#-- copyright
+#++
+
+module Storages
+ module Peripherals
+ module OAuthConfigurations
+ class OneDriveConfiguration
+ DEFAULT_SCOPES = %w[offline_access files.readwrite.all user.read sites.readwrite.all].freeze
+
+ attr_reader :oauth_client
+
+ def initialize(storage)
+ @storage = storage
+ @uri = storage.uri
+ @oauth_client = storage.oauth_client
+ @oauth_uri = URI('https://login.microsoftonline.com/').normalize
+ end
+
+ def authorization_state_check(access_token)
+ # response =
+ Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
+ http.get('/v1.0/me', { 'Authorization' => "Bearer #{access_token}" })
+ end
+ #
+ # case response
+ # when Net::HTTPSuccess
+ # :connected
+ # when Net::HTTPForbidden, Net::HTTPUnauthorized
+ # service_result = refresh_token
+ # if service_result.success?
+ # :connected
+ # elsif service_result.result == 'invalid_request'
+ # :failed_authorization
+ # else
+ # :error
+ # end
+ # else
+ # raise StandardError, 'not sure what to do'
+ # end
+ # rescue StandardError
+ # :error
+ end
+
+ def compute_scopes(scopes)
+ Array(scopes) | DEFAULT_SCOPES
+ end
+
+ def basic_rack_oauth_client
+ Rack::OAuth2::Client.new(
+ identifier: @oauth_client.client_id,
+ secret: @oauth_client.client_secret,
+ scheme: @oauth_uri.scheme,
+ host: @oauth_uri.host,
+ port: @oauth_uri.port,
+ authorization_endpoint: "/#{@storage.tenant_id}/oauth2/v2.0/authorize",
+ token_endpoint: "/#{@storage.tenant_id}/oauth2/v2.0/token"
+ )
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb
index 67ecdd2bb2d..9deed6e918a 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/add_user_to_group_command.rb
@@ -29,7 +29,7 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class AddUserToGroupCommand
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@username = storage.username
@password = storage.password
@group = storage.group
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
index ddda8a14381..7b353f151dd 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
@@ -31,7 +31,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
using Storages::Peripherals::ServiceResultRefinements
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@username = storage.username
@password = storage.password
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
index a309f9c31f4..81b07c2255f 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
@@ -31,7 +31,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
using Storages::Peripherals::ServiceResultRefinements
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@username = storage.username
@password = storage.password
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
index 8388b1f79bf..4140c96129c 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
@@ -31,7 +31,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
using Storages::Peripherals::ServiceResultRefinements
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@oauth_client = storage.oauth_client
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
index 67b785faf0d..b6787ab161e 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
@@ -33,7 +33,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
FILES_INFO_PATH = 'ocs/v1.php/apps/integration_openproject/filesinfo'.freeze
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@oauth_client = storage.oauth_client
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
index f437012197d..61531659d17 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
@@ -29,8 +29,8 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class FilesQuery
def initialize(storage)
- @uri = URI(storage.host).normalize
- @oauth_client = storage.oauth_client
+ @uri = storage.uri
+ @configuration = storage.oauth_configuration
end
def self.call(storage:, user:, folder:)
@@ -39,7 +39,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:disable Metrics/AbcSize
def call(user:, folder:)
- result = Util.token(user:, oauth_client: @oauth_client) do |token|
+ result = Util.token(user:, configuration: @configuration) do |token|
base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files")
@location_prefix = Util.join_uri_path(base_path, token.origin_user_id.gsub(' ', '%20'))
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb
index dc79744f591..b6ebf4b870b 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/group_users_query.rb
@@ -31,7 +31,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
using Storages::Peripherals::ServiceResultRefinements
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@username = storage.username
@password = storage.password
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
index 08013648406..dc22281ccf9 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
@@ -31,7 +31,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Internal
UTIL = ::Storages::Peripherals::StorageInteraction::Nextcloud::Util
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@base_path = UTIL.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(storage.username))
@username = storage.username
@password = storage.password
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
index 1bead7ef4bf..a2104e3084a 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/propfind_query.rb
@@ -63,7 +63,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Internal
# ].freeze
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@username = storage.username
@password = storage.password
@group = storage.group
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb
index 7bc4a2b3d83..c97eb4e3e4d 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/remove_user_from_group_command.rb
@@ -29,7 +29,7 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class RemoveUserFromGroupCommand
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@username = storage.username
@password = storage.password
@group = storage.group
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb
index 2626a9dcea5..59b1aeaddda 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command.rb
@@ -31,7 +31,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
using Storages::Peripherals::ServiceResultRefinements
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files", CGI.escapeURIComponent(storage.username))
@username = storage.username
@password = storage.password
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
index 643204125af..2660bc0511f 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
@@ -31,7 +31,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
using Storages::Peripherals::ServiceResultRefinements
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@username = storage.username
@password = storage.password
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
index d6a48616fab..1bb6b63d9ac 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
@@ -34,7 +34,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
URI_UPLOAD_BASE_PATH = 'index.php/apps/integration_openproject/direct-upload'.freeze
def initialize(storage)
- @uri = URI(storage.host).normalize
+ @uri = storage.uri
@oauth_client = storage.oauth_client
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
index 7f451e39544..ab2381b099d 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
@@ -55,8 +55,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Util
File.join(uri.to_s, *)
end
- def token(user:, oauth_client:, &block)
- connection_manager = ::OAuthClients::ConnectionManager.new(user:, oauth_client:)
+ def token(user:, oauth_client: nil, configuration: nil, &block)
+ connection_manager = ::OAuthClients::ConnectionManager.new(user:, oauth_client:, configuration:)
connection_manager.get_access_token.match(
on_success: ->(token) do
connection_manager.request_with_token_refresh(token) { block.call(token) }
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb
index cf847189ecf..7e7e37fca23 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb
@@ -32,8 +32,6 @@ module Storages
module Peripherals
module StorageInteraction
module OneDrive
- GRAPH_API_URI = URI('https://graph.microsoft.com').freeze
-
Queries = Dry::Container::Namespace.new('queries') do
namespace('one_drive') do
register(:files, FilesQuery)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
index 6111876a6a6..c92def88938 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
@@ -42,12 +42,13 @@ module Storages
def initialize(storage)
@storage = storage
+ @uri = storage.uri
end
def call(user:, folder: nil)
result = using_user_token(user) do |token|
# Make the Get Request to the necessary endpoints
- response = Net::HTTP.start(GRAPH_API_URI.host, GRAPH_API_URI.port, use_ssl: true) do |http|
+ response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
http.get(uri_path_for(folder) + FIELDS, { 'Authorization' => "Bearer #{token.access_token}" })
end
diff --git a/modules/storages/app/models/storages/nextcloud_storage.rb b/modules/storages/app/models/storages/nextcloud_storage.rb
index 558ceaa87da..9b403f4b9cd 100644
--- a/modules/storages/app/models/storages/nextcloud_storage.rb
+++ b/modules/storages/app/models/storages/nextcloud_storage.rb
@@ -28,52 +28,58 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Storages::NextcloudStorage < Storages::Storage
- PROVIDER_FIELDS_DEFAULTS = {
- automatically_managed: true,
- username: 'OpenProject'
- }.freeze
+module Storages
+ class NextcloudStorage < Storage
+ PROVIDER_FIELDS_DEFAULTS = {
+ automatically_managed: true,
+ username: 'OpenProject'
+ }.freeze
- store_attribute :provider_fields, :automatically_managed, :boolean
- store_attribute :provider_fields, :username, :string
- store_attribute :provider_fields, :password, :string
- store_attribute :provider_fields, :group, :string
- store_attribute :provider_fields, :group_folder, :string
+ store_attribute :provider_fields, :automatically_managed, :boolean
+ store_attribute :provider_fields, :username, :string
+ store_attribute :provider_fields, :password, :string
+ store_attribute :provider_fields, :group, :string
+ store_attribute :provider_fields, :group_folder, :string
- def self.sync_all_group_folders
- # Returns false if lock cannot be acquired, block is not executed then.
- OpenProject::Mutex.with_advisory_lock(self,
- 'sync_all_group_folders',
- timeout_seconds: 0,
- transaction: false) do
- where("provider_fields->>'automatically_managed' = 'true'")
- .includes(:oauth_client)
- .each do |storage|
- Storages::GroupFolderPropertiesSyncService.new(storage).call
+ def self.sync_all_group_folders
+ # Returns false if lock cannot be acquired, block is not executed then.
+ OpenProject::Mutex.with_advisory_lock(self,
+ 'sync_all_group_folders',
+ timeout_seconds: 0,
+ transaction: false) do
+ where("provider_fields->>'automatically_managed' = 'true'")
+ .includes(:oauth_client)
+ .each do |storage|
+ Storages::GroupFolderPropertiesSyncService.new(storage).call
+ end
+ true
end
- true
end
- end
- def configuration_checks
- {
- storage_oauth_client_configured: oauth_client.present?,
- openproject_oauth_application_configured: oauth_application.present?,
- host_name_configured: host.present? && name.present?
- }
- end
+ def oauth_configuration
+ Peripherals::OAuthConfigurations::NextcloudConfiguration.new(self)
+ end
- def automatic_management_unspecified?
- automatically_managed.nil?
- end
+ def automatic_management_unspecified?
+ automatically_managed.nil?
+ end
- %i[username group group_folder].each do |attribute_method|
- define_method(attribute_method) do
- super().presence || PROVIDER_FIELDS_DEFAULTS[:username]
+ def configuration_checks
+ {
+ storage_oauth_client_configured: oauth_client.present?,
+ openproject_oauth_application_configured: oauth_application.present?,
+ host_name_configured: host.present? && name.present?
+ }
end
- end
- def provider_fields_defaults
- PROVIDER_FIELDS_DEFAULTS
+ %i[username group group_folder].each do |attribute_method|
+ define_method(attribute_method) do
+ super().presence || PROVIDER_FIELDS_DEFAULTS[:username]
+ end
+ end
+
+ def provider_fields_defaults
+ PROVIDER_FIELDS_DEFAULTS
+ end
end
end
diff --git a/modules/storages/app/models/storages/one_drive_storage.rb b/modules/storages/app/models/storages/one_drive_storage.rb
index 31121a696e9..ae57f9b1f3c 100644
--- a/modules/storages/app/models/storages/one_drive_storage.rb
+++ b/modules/storages/app/models/storages/one_drive_storage.rb
@@ -36,5 +36,13 @@ module Storages
def configuration_checks
{ storage_oauth_client_configured: oauth_client.present? }
end
+
+ def oauth_configuration
+ Peripherals::OAuthConfigurations::OneDriveConfiguration.new(self)
+ end
+
+ def uri
+ URI('https://graph.microsoft.com').normalize
+ end
end
end
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index fee251ce4e4..92f491533da 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
@@ -59,8 +61,8 @@ class Storages::Storage < ApplicationRecord
has_one :oauth_application, class_name: '::Doorkeeper::Application', as: :integration, dependent: :destroy
PROVIDER_TYPES = [
- PROVIDER_TYPE_NEXTCLOUD = 'Storages::NextcloudStorage'.freeze,
- PROVIDER_TYPE_ONE_DRIVE = 'Storages::OneDriveStorage'.freeze
+ PROVIDER_TYPE_NEXTCLOUD = 'Storages::NextcloudStorage',
+ PROVIDER_TYPE_ONE_DRIVE = 'Storages::OneDriveStorage'
].freeze
validates_uniqueness_of :host, allow_nil: true
@@ -100,7 +102,15 @@ class Storages::Storage < ApplicationRecord
end
def configuration_checks
- raise NotImplementedError
+ raise Errors::SubclassResponsibility
+ end
+
+ def uri
+ URI(host).normalize
+ end
+
+ def oauth_configuration
+ raise Errors::SubclassResponsibility
end
def short_provider_type
diff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rb
index b91b0e4a6aa..883468d5888 100644
--- a/modules/storages/spec/factories/storage_factory.rb
+++ b/modules/storages/spec/factories/storage_factory.rb
@@ -29,16 +29,22 @@
#++
FactoryBot.define do
- factory :storage, class: '::Storages::Storage' do
+ factory :storage, class: 'Storages::Storage' do
sequence(:name) { |n| "Storage #{n}" }
creator factory: :user
+ # rubocop:disable FactoryBot/FactoryAssociationWithStrategy
+ # For some reason the order of saving breaks STI
trait :with_oauth_client do
- oauth_client
+ oauth_client { build(:oauth_client) }
end
+ # rubocop:enable FactoryBot/FactoryAssociationWithStrategy
- factory :nextcloud_storage, class: '::Storages::NextcloudStorage' do
- provider_type { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }
+ factory :one_drive_storage, class: "Storages::OneDriveStorage" do
+ host { nil }
+ end
+
+ factory :nextcloud_storage, class: 'Storages::NextcloudStorage' do
sequence(:host) { |n| "https://host#{n}.example.com" }
trait :as_automatically_managed do
@@ -51,9 +57,5 @@ FactoryBot.define do
automatically_managed { false }
end
end
-
- factory :one_drive_storage, class: '::Storages::OneDriveStorage' do
- provider_type { Storages::Storage::PROVIDER_TYPE_ONE_DRIVE }
- end
end
end
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index 68f5395173f..52998321274 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -29,22 +29,18 @@
#++
require 'spec_helper'
-require 'webmock/rspec'
-RSpec.describe OAuthClients::ConnectionManager, type: :model do
+RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
let(:user) { create(:user) }
+
let(:host) { "https://example.org" }
- let(:provider_type) { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }
- let(:storage) { create(:nextcloud_storage, provider_type:, host: "#{host}/") }
+ let(:storage) { create(:nextcloud_storage, :with_oauth_client, host: "#{host}/") }
+ let(:oauth_client) { storage.oauth_client }
+
let(:scope) { [:all] } # OAuth2 resources to access, specific to provider
- let(:oauth_client) do
- create(:oauth_client,
- client_id: "nwz34rWsolvJvchfQ1bVHXfMb1ETK89lCBgzrLhWx3ACW5nKfmdcyf5ftlCyKGbk",
- client_secret: "A08n6CRBOOr41iqkWRynnP6BbmEnau7LeP9t9xrIbiYX46iXgmIZgqhJoDFjUMEq",
- integration: storage)
- end
let(:oauth_client_token) { create(:oauth_client_token, oauth_client:, user:) }
- let(:instance) { described_class.new(user:, oauth_client:) }
+
+ let(:instance) { described_class.new(user:, oauth_client:, configuration: storage.oauth_configuration) }
# The get_authorization_uri method returns the OAuth2 authorization URI as a string. That URI is the starting point for
# a user to grant OpenProject access to Nextcloud.
diff --git a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
index 6a63700302a..df4c612dd99 100644
--- a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
+++ b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
@@ -30,13 +30,13 @@
require 'spec_helper'
-RSpec.describe OAuthClients::OneDriveConnectionManager, webmock: true do
+RSpec.describe OAuthClients::OneDriveConnectionManager, type: :model, webmock: true do
let(:user) { create(:user) }
let(:storage) { create(:one_drive_storage, :with_oauth_client, tenant_id: 'consumers') }
let(:token) { create(:oauth_client_token, oauth_client: storage.oauth_client, user:) }
subject(:connection_manager) do
- described_class.new(user:, oauth_client: storage.oauth_client, tenant_id: storage.tenant_id)
+ OAuthClients::ConnectionManager.new(user:, oauth_client: nil, configuration: storage.oauth_configuration)
end
describe '#get_authorization_uri' do