git_http_controller.rb 1.8 KB
Newer Older
J
Jacob Vosmaer 已提交
1
class Projects::GitHttpController < Projects::GitHttpClientController
2
  include WorkhorseRequest
3

4 5 6 7 8
  before_action :access_check

  rescue_from Gitlab::GitAccess::UnauthorizedError, with: :render_403
  rescue_from Gitlab::GitAccess::NotFoundError, with: :render_404

9 10 11
  # GET /foo/bar.git/info/refs?service=git-upload-pack (git pull)
  # GET /foo/bar.git/info/refs?service=git-receive-pack (git push)
  def info_refs
12
    log_user_activity if upload_pack?
13

14
    render_ok
J
Jacob Vosmaer 已提交
15
  end
J
Jacob Vosmaer 已提交
16

17 18
  # POST /foo/bar.git/git-upload-pack (git pull)
  def git_upload_pack
19
    render_ok
20 21 22 23
  end

  # POST /foo/bar.git/git-receive-pack" (git push)
  def git_receive_pack
24
    render_ok
J
Jacob Vosmaer 已提交
25 26 27 28
  end

  private

J
Jacob Vosmaer 已提交
29 30
  def download_request?
    upload_pack?
J
Jacob Vosmaer 已提交
31 32 33
  end

  def upload_pack?
34
    git_command == 'git-upload-pack'
J
Jacob Vosmaer 已提交
35 36
  end

37
  def git_command
J
Jacob Vosmaer 已提交
38
    if action_name == 'info_refs'
J
Jacob Vosmaer 已提交
39
      params[:service]
J
Jacob Vosmaer 已提交
40
    else
41
      action_name.dasherize
J
Jacob Vosmaer 已提交
42 43
    end
  end
J
Jacob Vosmaer 已提交
44

J
Jacob Vosmaer 已提交
45
  def render_ok
46
    set_workhorse_internal_api_content_type
47
    render json: Gitlab::Workhorse.git_http_ok(repository, wiki?, user, action_name)
J
Jacob Vosmaer 已提交
48
  end
J
Jacob Vosmaer 已提交
49

50 51
  def render_403(exception)
    render plain: exception.message, status: :forbidden
52 53
  end

54 55
  def render_404(exception)
    render plain: exception.message, status: :not_found
J
Jacob Vosmaer 已提交
56
  end
J
Jacob Vosmaer 已提交
57

58
  def access
59 60 61 62 63 64
    @access ||= access_klass.new(access_actor, project, 'http', authentication_abilities: authentication_abilities)
  end

  def access_actor
    return user if user
    return :ci if ci?
65 66
  end

J
Jacob Vosmaer 已提交
67 68 69
  def access_check
    # Use the magic string '_any' to indicate we do not know what the
    # changes are. This is also what gitlab-shell does.
70
    access.check(git_command, '_any')
J
Jacob Vosmaer 已提交
71
  end
72 73 74 75

  def access_klass
    @access_klass ||= wiki? ? Gitlab::GitAccessWiki : Gitlab::GitAccess
  end
76 77 78 79

  def log_user_activity
    Users::ActivityService.new(user, 'pull').execute
  end
J
Jacob Vosmaer 已提交
80
end