debian-mirror-gitlab/app/services/groups/create_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
3 KiB
Ruby
Raw Normal View History

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