internal.rb 1.8 KB
Newer Older
1
module API
2
  # Internal access API
D
Dmitriy Zaporozhets 已提交
3
  class Internal < Grape::API
V
Valery Sizov 已提交
4 5 6 7
    before {
      authenticate_by_gitlab_shell_token!
    }

8
    namespace 'internal' do
9
      # Check if git command is allowed to project
10
      #
D
Dmitriy Zaporozhets 已提交
11
      # Params:
12 13
      #   key_id - ssh key id for Git over SSH
      #   user_id - user id for Git over HTTP
D
Dmitriy Zaporozhets 已提交
14 15 16
      #   project - project path with namespace
      #   action - git action (git-upload-pack or git-receive-pack)
      #   ref - branch name
17
      #   forced_push - forced_push
D
Dmitriy Zaporozhets 已提交
18
      #
D
Dmitriy Zaporozhets 已提交
19
      post "/allowed" do
20
        status 200
21
        project_path = params[:project]
22

23 24 25 26
        # Check for *.wiki repositories.
        # Strip out the .wiki from the pathname before finding the
        # project. This applies the correct project permissions to
        # the wiki repository as well.
27 28
        access =
          if project_path =~ /\.wiki\Z/
29
            project_path.sub!(/\.wiki\Z/, '')
30 31 32 33 34
            Gitlab::GitAccessWiki.new
          else
            Gitlab::GitAccess.new
          end

35
        project = Project.find_with_namespace(project_path)
36
        return false unless project
37

38 39 40 41 42 43 44 45
        actor = if params[:key_id]
                  Key.find(params[:key_id])
                elsif params[:user_id]
                  User.find(params[:user_id])
                end

        return false unless actor

46
        access.check(
47 48 49
          actor,
          params[:action],
          project,
50
          params[:changes]
51
        )
52 53 54 55 56 57 58
      end

      #
      # Discover user by ssh key
      #
      get "/discover" do
        key = Key.find(params[:key_id])
59
        present key.user, with: Entities::UserSafe
60
      end
D
Dmitriy Zaporozhets 已提交
61 62 63

      get "/check" do
        {
64
          api_version: API.version,
65 66
          gitlab_version: Gitlab::VERSION,
          gitlab_rev: Gitlab::REVISION,
D
Dmitriy Zaporozhets 已提交
67 68
        }
      end
D
Dmitriy Zaporozhets 已提交
69 70 71
    end
  end
end