diff options
Diffstat (limited to 'lib/bulk_imports/groups/loaders/group_loader.rb')
-rw-r--r-- | lib/bulk_imports/groups/loaders/group_loader.rb | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/lib/bulk_imports/groups/loaders/group_loader.rb b/lib/bulk_imports/groups/loaders/group_loader.rb index a631685c2ad..5f5307123a5 100644 --- a/lib/bulk_imports/groups/loaders/group_loader.rb +++ b/lib/bulk_imports/groups/loaders/group_loader.rb @@ -4,10 +4,21 @@ module BulkImports module Groups module Loaders class GroupLoader + GroupCreationError = Class.new(StandardError) + def load(context, data) - return unless user_can_create_group?(context.current_user, data) + path = data['path'] + current_user = context.current_user + destination_namespace = context.entity.destination_namespace + + raise(GroupCreationError, 'Path is missing') unless path.present? + raise(GroupCreationError, 'Destination is not a group') if user_namespace_destination?(destination_namespace) + raise(GroupCreationError, 'User not allowed to create group') unless user_can_create_group?(current_user, data) + raise(GroupCreationError, 'Group exists') if group_exists?(destination_namespace, path) + + group = ::Groups::CreateService.new(current_user, data).execute - group = ::Groups::CreateService.new(context.current_user, data).execute + raise(GroupCreationError, group.errors.full_messages.to_sentence) if group.errors.any? context.entity.update!(group: group) @@ -25,6 +36,18 @@ module BulkImports Ability.allowed?(current_user, :create_group) end end + + def group_exists?(destination_namespace, path) + full_path = destination_namespace.present? ? File.join(destination_namespace, path) : path + + Group.find_by_full_path(full_path).present? + end + + def user_namespace_destination?(destination_namespace) + return false unless destination_namespace.present? + + Namespace.find_by_full_path(destination_namespace)&.user_namespace? + end end end end |