BigW Consortium Gitlab

lfs_helper.rb 1.8 KB
Newer Older
Jacob Vosmaer committed
1
module LfsHelper
2
  include Gitlab::Routing.url_helpers
3

4
  def require_lfs_enabled!
Jacob Vosmaer committed
5 6 7 8 9
    return if Gitlab.config.lfs.enabled

    render(
      json: {
        message: 'Git LFS is not enabled on this GitLab server, contact your admin.',
10
        documentation_url: help_url,
Jacob Vosmaer committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
      },
      status: 501
    )
  end

  def lfs_check_access!
    return if download_request? && lfs_download_access?
    return if upload_request? && lfs_upload_access?

    if project.public? || (user && user.can?(:read_project, project))
      render_lfs_forbidden
    else
      render_lfs_not_found
    end
  end

  def lfs_download_access?
28 29
    return false unless project.lfs_enabled?

30
    ci? || lfs_deploy_token? || user_can_download_code? || build_can_download_code?
31 32
  end

33
  def user_can_download_code?
34
    has_authentication_ability?(:download_code) && can?(user, :download_code, project)
35 36
  end

37
  def build_can_download_code?
38
    has_authentication_ability?(:build_download_code) && can?(user, :build_download_code, project)
Jacob Vosmaer committed
39 40 41
  end

  def lfs_upload_access?
42 43
    return false unless project.lfs_enabled?

44
    has_authentication_ability?(:push_code) && can?(user, :push_code, project)
Jacob Vosmaer committed
45 46 47 48 49 50
  end

  def render_lfs_forbidden
    render(
      json: {
        message: 'Access forbidden. Check your access level.',
51
        documentation_url: help_url,
Jacob Vosmaer committed
52 53 54 55 56 57 58 59 60 61
      },
      content_type: "application/vnd.git-lfs+json",
      status: 403
    )
  end

  def render_lfs_not_found
    render(
      json: {
        message: 'Not found.',
62
        documentation_url: help_url,
Jacob Vosmaer committed
63 64 65 66 67 68 69 70 71 72
      },
      content_type: "application/vnd.git-lfs+json",
      status: 404
    )
  end

  def storage_project
    @storage_project ||= begin
      result = project

Jacob Vosmaer committed
73 74
      loop do
        break unless result.forked?
Jacob Vosmaer committed
75 76 77 78 79 80 81
        result = result.forked_from_project
      end

      result
    end
  end
end