2018-07-16 16:31:01 +00:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2016-03-16 22:44:33 +00:00
|
|
|
|
module Groups
|
|
|
|
|
class CreateService < Groups::BaseService
|
2016-03-17 22:42:46 +00:00
|
|
|
|
def initialize(user, params = {})
|
|
|
|
|
@current_user, @params = user, params.dup
|
2017-02-20 12:41:50 +00:00
|
|
|
|
@chat_team = @params.delete(:create_chat_team)
|
2016-03-16 22:44:33 +00:00
|
|
|
|
end
|
|
|
|
|
|
2016-03-17 22:42:46 +00:00
|
|
|
|
def execute
|
2019-04-02 15:55:34 +00:00
|
|
|
|
remove_unallowed_params
|
|
|
|
|
|
2016-03-18 12:28:16 +00:00
|
|
|
|
@group = Group.new(params)
|
|
|
|
|
|
2019-02-01 15:22:30 +00:00
|
|
|
|
after_build_hook(@group, params)
|
|
|
|
|
|
2017-09-07 18:35:45 +00:00
|
|
|
|
unless can_use_visibility_level? && can_create_group?
|
2016-12-29 17:18:05 +00:00
|
|
|
|
return @group
|
2016-12-26 09:56:19 +00:00
|
|
|
|
end
|
|
|
|
|
|
2016-03-21 23:09:20 +00:00
|
|
|
|
@group.name ||= @group.path.dup
|
2017-02-02 14:04:02 +00:00
|
|
|
|
|
2017-02-20 12:41:50 +00:00
|
|
|
|
if create_chat_team?
|
2017-03-06 13:06:33 +00:00
|
|
|
|
response = Mattermost::CreateTeamService.new(@group, current_user).execute
|
2017-02-20 12:41:50 +00:00
|
|
|
|
return @group if @group.errors.any?
|
2017-03-06 13:06:33 +00:00
|
|
|
|
|
|
|
|
|
@group.build_chat_team(name: response['name'], team_id: response['id'])
|
2017-02-02 14:04:02 +00:00
|
|
|
|
end
|
|
|
|
|
|
2019-06-25 17:54:42 +00:00
|
|
|
|
@group.add_owner(current_user) if @group.save
|
2016-03-17 22:42:46 +00:00
|
|
|
|
@group
|
2016-03-16 22:44:33 +00:00
|
|
|
|
end
|
2017-03-01 19:34:29 +00:00
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2019-02-01 15:22:30 +00:00
|
|
|
|
def after_build_hook(group, params)
|
2019-02-25 10:42:31 +00:00
|
|
|
|
# overridden in EE
|
2019-02-01 15:22:30 +00:00
|
|
|
|
end
|
|
|
|
|
|
2017-03-01 19:34:29 +00:00
|
|
|
|
def create_chat_team?
|
|
|
|
|
Gitlab.config.mattermost.enabled && @chat_team && group.chat_team.nil?
|
|
|
|
|
end
|
2017-09-07 18:35:45 +00:00
|
|
|
|
|
|
|
|
|
def can_create_group?
|
|
|
|
|
if @group.subgroup?
|
|
|
|
|
unless can?(current_user, :create_subgroup, @group.parent)
|
|
|
|
|
@group.parent = nil
|
2019-04-15 12:25:48 +00:00
|
|
|
|
@group.errors.add(:parent_id, s_('CreateGroup|You don’t have permission to create a subgroup in this group.'))
|
2017-09-07 18:35:45 +00:00
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
unless can?(current_user, :create_group)
|
2019-04-15 12:25:48 +00:00
|
|
|
|
@group.errors.add(:base, s_('CreateGroup|You don’t have permission to create groups.'))
|
2017-09-07 18:35:45 +00:00
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def can_use_visibility_level?
|
2019-04-12 05:00:50 +00:00
|
|
|
|
unless Gitlab::VisibilityLevel.allowed_for?(current_user, visibility_level)
|
2017-09-07 18:35:45 +00:00
|
|
|
|
deny_visibility_level(@group)
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
end
|
2016-03-16 22:44:33 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
|
|
Groups::CreateService.prepend_if_ee('EE::Groups::CreateService')
|