internal.rb 1.5 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1
module Gitlab
2
  # Internal access API
D
Dmitriy Zaporozhets 已提交
3
  class Internal < Grape::API
4 5 6 7
    namespace 'internal' do
      #
      # Check if ssh key has access to project code
      #
D
Dmitriy Zaporozhets 已提交
8 9 10 11 12 13
      # Params:
      #   key_id - SSH Key id
      #   project - project path with namespace
      #   action - git action (git-upload-pack or git-receive-pack)
      #   ref - branch name
      #
14 15 16
      get "/allowed" do
        key = Key.find(params[:key_id])
        project = Project.find_with_namespace(params[:project])
17 18 19 20 21 22
        git_cmd = params[:action]

        if key.is_deploy_key
          project == key.project && git_cmd == 'git-upload-pack'
        else
          user = key.user
23 24 25

          return false if user.blocked?

26 27 28 29 30 31 32 33 34 35
          action = case git_cmd
                   when 'git-upload-pack'
                     then :download_code
                   when 'git-receive-pack'
                     then
                     if project.protected_branch?(params[:ref])
                       :push_code_to_protected_branches
                     else
                       :push_code
                     end
36
                   end
D
Dmitriy Zaporozhets 已提交
37

38 39
          user.can?(action, project)
        end
40 41 42 43 44 45 46
      end

      #
      # Discover user by ssh key
      #
      get "/discover" do
        key = Key.find(params[:key_id])
47
        present key.user, with: Entities::UserSafe
48
      end
D
Dmitriy Zaporozhets 已提交
49 50 51

      get "/check" do
        {
52 53 54
          api_version: Gitlab::API.version,
          gitlab_version: Gitlab::VERSION,
          gitlab_rev: Gitlab::REVISION,
D
Dmitriy Zaporozhets 已提交
55 56
        }
      end
D
Dmitriy Zaporozhets 已提交
57 58 59 60
    end
  end
end