create_service.rb 808 字节
Newer Older
1 2
module Groups
  class CreateService < Groups::BaseService
F
Felipe Artur 已提交
3 4
    def initialize(user, params = {})
      @current_user, @params = user, params.dup
5 6
    end

F
Felipe Artur 已提交
7
    def execute
8 9
      @group = Group.new(params)

D
Douwe Maan 已提交
10 11 12 13
      unless Gitlab::VisibilityLevel.allowed_for?(current_user, params[:visibility_level])
        deny_visibility_level(@group)
        return @group
      end
14

15 16 17 18 19 20 21 22 23 24 25 26 27
      parent_id = params[:parent_id]

      if parent_id
        parent = Group.find(parent_id)

        unless can?(current_user, :admin_group, parent)
          @group.parent_id = nil
          @group.errors.add(:parent_id, 'manage access required to create subgroup')

          return @group
        end
      end

D
Douwe Maan 已提交
28
      @group.name ||= @group.path.dup
F
Felipe Artur 已提交
29
      @group.save
30
      @group.add_owner(current_user)
F
Felipe Artur 已提交
31
      @group
32 33 34
    end
  end
end