2019-10-30 11:14:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Groups
|
|
|
|
module GroupLinks
|
2021-04-12 14:12:15 -04:00
|
|
|
class CreateService < Groups::BaseService
|
2021-06-09 05:10:18 -04:00
|
|
|
def initialize(shared_group, shared_with_group, user, params)
|
|
|
|
@shared_group = shared_group
|
|
|
|
super(shared_with_group, user, params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
unless shared_with_group && shared_group &&
|
2020-06-09 14:08:28 -04:00
|
|
|
can?(current_user, :admin_group_member, shared_group) &&
|
2021-06-09 05:10:18 -04:00
|
|
|
can?(current_user, :read_group, shared_with_group) &&
|
|
|
|
sharing_allowed?
|
2019-10-30 11:14:17 -04:00
|
|
|
return error('Not Found', 404)
|
|
|
|
end
|
|
|
|
|
|
|
|
link = GroupGroupLink.new(
|
|
|
|
shared_group: shared_group,
|
2021-06-09 05:10:18 -04:00
|
|
|
shared_with_group: shared_with_group,
|
2019-10-30 11:14:17 -04:00
|
|
|
group_access: params[:shared_group_access],
|
|
|
|
expires_at: params[:expires_at]
|
|
|
|
)
|
|
|
|
|
|
|
|
if link.save
|
2021-07-19 14:08:23 -04:00
|
|
|
shared_with_group.refresh_members_authorized_projects(blocking: false, direct_members_only: true)
|
2019-10-30 11:14:17 -04:00
|
|
|
success(link: link)
|
|
|
|
else
|
|
|
|
error(link.errors.full_messages.to_sentence, 409)
|
|
|
|
end
|
|
|
|
end
|
2021-06-09 05:10:18 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :shared_group
|
|
|
|
|
|
|
|
alias_method :shared_with_group, :group
|
|
|
|
|
|
|
|
def sharing_allowed?
|
|
|
|
sharing_outside_hierarchy_allowed? || within_hierarchy?
|
|
|
|
end
|
|
|
|
|
|
|
|
def sharing_outside_hierarchy_allowed?
|
|
|
|
!shared_group.root_ancestor.namespace_settings.prevent_sharing_groups_outside_hierarchy
|
|
|
|
end
|
|
|
|
|
|
|
|
def within_hierarchy?
|
|
|
|
shared_group.root_ancestor.self_and_descendants_ids.include?(shared_with_group.id)
|
|
|
|
end
|
2019-10-30 11:14:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|