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

82 lines
2 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 = {})
@current_user, @params = user, params.dup
2017-08-17 22:00:37 +05:30
@chat_team = @params.delete(:create_chat_team)
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
2016-06-02 11:05:42 +05:30
@group = Group.new(params)
2019-03-02 22:35:43 +05:30
after_build_hook(@group, params)
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?
response = Mattermost::CreateTeamService.new(@group, current_user).execute
return @group if @group.errors.any?
@group.build_chat_team(name: response['name'], team_id: response['id'])
end
2019-09-30 21:07:59 +05:30
@group.add_owner(current_user) if @group.save
2016-06-02 11:05:42 +05:30
@group
end
2017-08-17 22:00:37 +05:30
private
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
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
2016-06-02 11:05:42 +05:30
end
end
2019-12-04 20:38:33 +05:30
Groups::CreateService.prepend_if_ee('EE::Groups::CreateService')