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

6
    namespace 'internal' do
7
      # Check if git command is allowed to project
8
      #
D
Dmitriy Zaporozhets 已提交
9
      # Params:
10 11
      #   key_id - ssh key id for Git over SSH
      #   user_id - user id for Git over HTTP
D
Dmitriy Zaporozhets 已提交
12 13 14
      #   project - project path with namespace
      #   action - git action (git-upload-pack or git-receive-pack)
      #   ref - branch name
15
      #   forced_push - forced_push
D
Dmitriy Zaporozhets 已提交
16
      #
J
James Lopez 已提交
17 18 19 20 21 22 23 24

      helpers do
        def wiki
          @wiki ||= params[:project].end_with?('.wiki') &&
            !Project.find_with_namespace(params[:project])
        end
      end

D
Dmitriy Zaporozhets 已提交
25
      post "/allowed" do
26
        status 200
27

28 29 30 31 32 33
        actor = 
          if params[:key_id]
            Key.find_by(id: params[:key_id])
          elsif params[:user_id]
            User.find_by(id: params[:user_id])
          end
34

35
        project_path = params[:project]
36
        
37 38 39 40
        # 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.
41
        project_path.chomp!('.wiki') if wiki
42

43
        project = Project.find_with_namespace(project_path)
44

D
Douwe Maan 已提交
45 46 47 48 49 50
        access =
          if wiki
            Gitlab::GitAccessWiki.new(actor, project)
          else
            Gitlab::GitAccess.new(actor, project)
          end
51

D
Douwe Maan 已提交
52
        access.check(params[:action], params[:changes])
53 54 55 56 57 58 59
      end

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

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

      get "/broadcast_message" do
        if message = BroadcastMessage.current
          present message, with: Entities::BroadcastMessage
74 75
        else
          {}
D
Douwe Maan 已提交
76 77
        end
      end
D
Dmitriy Zaporozhets 已提交
78 79 80
    end
  end
end