gitlab-org--gitlab-foss/app/services/groups/create_service.rb

66 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Groups
class CreateService < Groups::BaseService
2016-03-17 18:42:46 -04:00
def initialize(user, params = {})
@current_user, @params = user, params.dup
@chat_team = @params.delete(:create_chat_team)
end
2016-03-17 18:42:46 -04:00
def execute
2016-03-18 08:28:16 -04:00
@group = Group.new(params)
unless can_use_visibility_level? && can_create_group?
return @group
end
2016-03-21 19:09:20 -04:00
@group.name ||= @group.path.dup
2017-02-02 09:04:02 -05:00
if create_chat_team?
2017-03-06 08:06:33 -05:00
response = Mattermost::CreateTeamService.new(@group, current_user).execute
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
@group.save
@group.add_owner(current_user)
2016-03-17 18:42:46 -04:00
@group
end
2017-03-01 14:34:29 -05:00
private
def create_chat_team?
Gitlab.config.mattermost.enabled && @chat_team && group.chat_team.nil?
end
def can_create_group?
if @group.subgroup?
unless can?(current_user, :create_subgroup, @group.parent)
@group.parent = nil
@group.errors.add(:parent_id, 'You dont have permission to create a subgroup in this group.')
return false
end
else
unless can?(current_user, :create_group)
@group.errors.add(:base, 'You dont have permission to create groups.')
return false
end
end
true
end
def can_use_visibility_level?
unless Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level])
deny_visibility_level(@group)
return false
end
true
end
end
end