Skip to content

Commit

Permalink
[#57707] rework group users query
Browse files Browse the repository at this point in the history
- https://community.openproject.org/work_packages/57707
- use authentication strategy pattern
  • Loading branch information
Kharonus committed Sep 30, 2024
1 parent fd14389 commit 28ae99e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,51 @@ module StorageInteraction
module Nextcloud
class GroupUsersQuery
include TaggedLogging
using ServiceResultRefinements

def self.call(storage:, group: storage.group)
new(storage).call(group:)
def self.call(storage:, auth_strategy:, group:)
new(storage).call(auth_strategy:, group:)
end

def initialize(storage)
@storage = storage
@username = storage.username
@password = storage.password
end

# rubocop:disable Metrics/AbcSize
def call(group:)
def call(auth_strategy:, group:)
with_tagged_logger do
url = UrlBuilder.url(@storage.uri, "ocs/v1.php/cloud/groups", CGI.escapeURIComponent(group))
Authentication[auth_strategy].call(storage: @storage, http_options:) do |http|
url = UrlBuilder.url(@storage.uri, "ocs/v1.php/cloud/groups", group)
info "Requesting user list for group #{group} via url #{url} "

info "Requesting user list for group #{group} via url #{url} "
response = OpenProject.httpx
.basic_auth(@username, @password)
.with(headers: { "OCS-APIRequest" => "true" })
.get(url)
handle_response(http.get(url))
end
end
end

error_data = StorageErrorData.new(source: self.class, payload: response)
private

case response
in { status: 200..299 }
group_users = Nokogiri::XML(response.body.to_s).xpath("/ocs/data/users/element").map(&:text)
info "#{group_users.size} users found"
ServiceResult.success(result: group_users)
in { status: 405 }
Util.error(:not_allowed, "Outbound request method not allowed", error_data)
in { status: 401 }
Util.error(:unauthorized, "Outbound request not authorized", error_data)
in { status: 404 }
Util.error(:not_found, "Outbound request destination not found", error_data)
in { status: 409 }
Util.error(:conflict, error_text_from_response(response), error_data)
else
Util.error(:error, "Outbound request failed", error_data)
end
def http_options
Util.ocs_api_request
end

# rubocop:disable Metrics/AbcSize
def handle_response(response)
error_data = StorageErrorData.new(source: self.class, payload: response)

case response
in { status: 200..299 }
group_users = Nokogiri::XML(response.body.to_s).xpath("/ocs/data/users/element").map(&:text)
info "#{group_users.size} users found"
ServiceResult.success(result: group_users)
in { status: 405 }
Util.error(:not_allowed, "Outbound request method not allowed", error_data)
in { status: 401 }
Util.error(:unauthorized, "Outbound request not authorized", error_data)
in { status: 404 }
Util.error(:not_found, "Outbound request destination not found", error_data)
in { status: 409 }
Util.error(:conflict, error_text_from_response(response), error_data)
else
Util.error(:error, "Outbound request failed", error_data)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,6 @@ def error(code, log_message = nil, data = nil)
)
end

def token(user:, configuration:, &)
connection_manager = OAuthClients::ConnectionManager.new(user:, configuration:)
connection_manager.get_access_token.match(
on_success: lambda do |token|
connection_manager.request_with_token_refresh(token) { yield token }
end,
on_failure: lambda do |_|
error(:unauthorized,
"Query could not be created! No access token found!",
StorageErrorData.new(source: connection_manager))
end
)
end

def error_text_from_response(response)
response.xml.xpath("//s:message").text
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def build_set_permissions_input_data(file_id, user_permissions)

def remote_group_users
info "Retrieving users that a part of the #{@storage.group} group"
group_users.call(storage: @storage, group: @storage.group)
group_users.call(storage: @storage, auth_strategy:, group: @storage.group)
end

### Model Scopes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ module Storages
end

# No AuthStrategy on GroupUsers
allow(group_users).to receive(:call).with(storage:, group: storage.group).and_return(group_users_result)
allow(group_users).to receive(:call).with(storage:,
auth_strategy:,
group: storage.group)
.and_return(group_users_result)
# Updating the group users
allow(add_user).to receive(:call).with(storage:,
auth_strategy:,
Expand Down

0 comments on commit 28ae99e

Please sign in to comment.