lfs_helper.rb 2.0 KB
Newer Older
J
Jacob Vosmaer 已提交
1
module LfsHelper
J
Jacob Vosmaer 已提交
2
  def require_lfs_enabled!
J
Jacob Vosmaer 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    return if Gitlab.config.lfs.enabled

    render(
      json: {
        message: 'Git LFS is not enabled on this GitLab server, contact your admin.',
        documentation_url: "#{Gitlab.config.gitlab.url}/help",
      },
      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?
26 27
    return false unless project.lfs_enabled?

28 29 30 31
    project.public? || ci? || privileged_user_can_download_code? || restricted_user_can_download_code?
  end

  def privileged_user_can_download_code?
32
    has_capability?(:download_code) && user && user.can?(:download_code, project)
33 34 35
  end

  def restricted_user_can_download_code?
36
    has_capability?(:restricted_download_code) && user && user.can?(:restricted_download_code, project)
J
Jacob Vosmaer 已提交
37 38 39
  end

  def lfs_upload_access?
40 41
    return false unless project.lfs_enabled?

42 43 44 45
    privileged_user_can_push_code?
  end

  def privileged_user_can_push_code?
46
    has_capability?(:push_code) && user && user.can?(:push_code, project)
J
Jacob Vosmaer 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  end

  def render_lfs_forbidden
    render(
      json: {
        message: 'Access forbidden. Check your access level.',
        documentation_url: "#{Gitlab.config.gitlab.url}/help",
      },
      content_type: "application/vnd.git-lfs+json",
      status: 403
    )
  end

  def render_lfs_not_found
    render(
      json: {
        message: 'Not found.',
        documentation_url: "#{Gitlab.config.gitlab.url}/help",
      },
      content_type: "application/vnd.git-lfs+json",
      status: 404
    )
  end

  def storage_project
    @storage_project ||= begin
      result = project

J
Rubocop  
Jacob Vosmaer 已提交
75 76
      loop do
        break unless result.forked?
J
Jacob Vosmaer 已提交
77 78 79 80 81 82 83
        result = result.forked_from_project
      end

      result
    end
  end
end