2018-07-16 12:31:01 -04:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2016-03-16 18:44:33 -04:00
|
|
|
|
module Groups
|
|
|
|
|
class CreateService < Groups::BaseService
|
2016-03-17 18:42:46 -04:00
|
|
|
|
def initialize(user, params = {})
|
2021-04-19 17:09:27 -04:00
|
|
|
|
@current_user = user
|
|
|
|
|
@params = params.dup
|
2017-02-20 07:41:50 -05:00
|
|
|
|
@chat_team = @params.delete(:create_chat_team)
|
2021-10-20 05:12:43 -04:00
|
|
|
|
@create_event = @params.delete(:create_event)
|
2016-03-16 18:44:33 -04:00
|
|
|
|
end
|
|
|
|
|
|
2016-03-17 18:42:46 -04:00
|
|
|
|
def execute
|
2019-04-02 11:55:34 -04:00
|
|
|
|
remove_unallowed_params
|
2019-09-26 14:06:29 -04:00
|
|
|
|
set_visibility_level
|
2019-04-02 11:55:34 -04:00
|
|
|
|
|
2021-03-25 17:09:13 -04:00
|
|
|
|
@group = Group.new(params.except(*::NamespaceSetting::NAMESPACE_SETTINGS_PARAMS))
|
|
|
|
|
|
|
|
|
|
@group.build_namespace_settings
|
|
|
|
|
handle_namespace_settings
|
2016-03-18 08:28:16 -04:00
|
|
|
|
|
2019-02-01 10:22:30 -05:00
|
|
|
|
after_build_hook(@group, params)
|
|
|
|
|
|
2020-09-30 11:09:46 -04:00
|
|
|
|
inherit_group_shared_runners_settings
|
|
|
|
|
|
2017-09-07 14:35:45 -04:00
|
|
|
|
unless can_use_visibility_level? && can_create_group?
|
2016-12-29 12:18:05 -05:00
|
|
|
|
return @group
|
2016-12-26 04:56:19 -05:00
|
|
|
|
end
|
|
|
|
|
|
2016-03-21 19:09:20 -04:00
|
|
|
|
@group.name ||= @group.path.dup
|
2017-02-02 09:04:02 -05:00
|
|
|
|
|
2017-02-20 07:41:50 -05:00
|
|
|
|
if create_chat_team?
|
2021-05-31 23:10:06 -04:00
|
|
|
|
response = ::Mattermost::CreateTeamService.new(@group, current_user).execute
|
2017-02-20 07:41:50 -05:00
|
|
|
|
return @group if @group.errors.any?
|
2017-03-06 08:06:33 -05:00
|
|
|
|
|
|
|
|
|
@group.build_chat_team(name: response['name'], team_id: response['id'])
|
2017-02-02 09:04:02 -05:00
|
|
|
|
end
|
|
|
|
|
|
2020-09-28 02:09:56 -04:00
|
|
|
|
Group.transaction do
|
|
|
|
|
if @group.save
|
|
|
|
|
@group.add_owner(current_user)
|
2021-05-12 08:10:24 -04:00
|
|
|
|
Integration.create_from_active_default_integrations(@group, :group_id)
|
2021-01-11 07:10:41 -05:00
|
|
|
|
OnboardingProgress.onboard(@group)
|
2020-09-28 02:09:56 -04:00
|
|
|
|
end
|
2020-07-16 11:09:38 -04:00
|
|
|
|
end
|
|
|
|
|
|
2021-10-20 05:12:43 -04:00
|
|
|
|
after_create_hook
|
|
|
|
|
|
2016-03-17 18:42:46 -04:00
|
|
|
|
@group
|
2016-03-16 18:44:33 -04:00
|
|
|
|
end
|
2017-03-01 14:34:29 -05:00
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2021-10-20 05:12:43 -04:00
|
|
|
|
attr_reader :create_event
|
|
|
|
|
|
2019-02-01 10:22:30 -05:00
|
|
|
|
def after_build_hook(group, params)
|
2019-02-25 05:42:31 -05:00
|
|
|
|
# overridden in EE
|
2019-02-01 10:22:30 -05:00
|
|
|
|
end
|
|
|
|
|
|
2021-10-20 05:12:43 -04:00
|
|
|
|
def after_create_hook
|
|
|
|
|
# overridden in EE
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-24 05:09:44 -04:00
|
|
|
|
def remove_unallowed_params
|
|
|
|
|
params.delete(:default_branch_protection) unless can?(current_user, :create_group_with_default_branch_protection)
|
2020-10-15 17:09:12 -04:00
|
|
|
|
params.delete(:allow_mfa_for_subgroups)
|
2020-04-24 05:09:44 -04:00
|
|
|
|
end
|
|
|
|
|
|
2017-03-01 14:34:29 -05:00
|
|
|
|
def create_chat_team?
|
|
|
|
|
Gitlab.config.mattermost.enabled && @chat_team && group.chat_team.nil?
|
|
|
|
|
end
|
2017-09-07 14:35:45 -04:00
|
|
|
|
|
|
|
|
|
def can_create_group?
|
|
|
|
|
if @group.subgroup?
|
|
|
|
|
unless can?(current_user, :create_subgroup, @group.parent)
|
|
|
|
|
@group.parent = nil
|
2019-04-15 08:25:48 -04:00
|
|
|
|
@group.errors.add(:parent_id, s_('CreateGroup|You don’t have permission to create a subgroup in this group.'))
|
2017-09-07 14:35:45 -04:00
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
unless can?(current_user, :create_group)
|
2019-04-15 08:25:48 -04:00
|
|
|
|
@group.errors.add(:base, s_('CreateGroup|You don’t have permission to create groups.'))
|
2017-09-07 14:35:45 -04:00
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def can_use_visibility_level?
|
2019-04-12 01:00:50 -04:00
|
|
|
|
unless Gitlab::VisibilityLevel.allowed_for?(current_user, visibility_level)
|
2017-09-07 14:35:45 -04:00
|
|
|
|
deny_visibility_level(@group)
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
end
|
2019-09-26 14:06:29 -04:00
|
|
|
|
|
|
|
|
|
def set_visibility_level
|
|
|
|
|
return if visibility_level.present?
|
|
|
|
|
|
|
|
|
|
params[:visibility_level] = Gitlab::CurrentSettings.current_application_settings.default_group_visibility
|
|
|
|
|
end
|
2020-09-30 11:09:46 -04:00
|
|
|
|
|
|
|
|
|
def inherit_group_shared_runners_settings
|
|
|
|
|
return unless @group.parent
|
|
|
|
|
|
|
|
|
|
@group.shared_runners_enabled = @group.parent.shared_runners_enabled
|
|
|
|
|
@group.allow_descendants_override_disabled_shared_runners = @group.parent.allow_descendants_override_disabled_shared_runners
|
|
|
|
|
end
|
2016-03-16 18:44:33 -04:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
|
Groups::CreateService.prepend_mod_with('Groups::CreateService')
|