BigW Consortium Gitlab

access_requests.rb 2.73 KB
Newer Older
1 2
module API
  class AccessRequests < Grape::API
3 4
    include PaginationParams

5 6 7 8 9
    before { authenticate! }

    helpers ::API::Helpers::MembersHelpers

    %w[group project].each do |source_type|
10 11 12
      params do
        requires :id, type: String, desc: "The #{source_type} ID"
      end
13
      resource source_type.pluralize do
14 15 16 17
        desc "Gets a list of access requests for a #{source_type}." do
          detail 'This feature was introduced in GitLab 8.11.'
          success Entities::AccessRequester
        end
18 19 20
        params do
          use :pagination
        end
21 22 23
        get ":id/access_requests" do
          source = find_source(source_type, params[:id])

24 25
          access_requesters = AccessRequestsFinder.new(source).execute!(current_user)
          access_requesters = paginate(access_requesters.includes(:user))
26

27
          present access_requesters.map(&:user), with: Entities::AccessRequester, source: source
28 29
        end

30 31 32 33
        desc "Requests access for the authenticated user to a #{source_type}." do
          detail 'This feature was introduced in GitLab 8.11.'
          success Entities::AccessRequester
        end
34 35 36 37 38 39 40 41 42 43 44
        post ":id/access_requests" do
          source = find_source(source_type, params[:id])
          access_requester = source.request_access(current_user)

          if access_requester.persisted?
            present access_requester.user, with: Entities::AccessRequester, access_requester: access_requester
          else
            render_validation_error!(access_requester)
          end
        end

45 46 47 48 49 50 51 52
        desc 'Approves an access request for the given user.' do
          detail 'This feature was introduced in GitLab 8.11.'
          success Entities::Member
        end
        params do
          requires :user_id, type: Integer, desc: 'The user ID of the access requester'
          optional :access_level, type: Integer, desc: 'A valid access level (defaults: `30`, developer access level)'
        end
53 54 55
        put ':id/access_requests/:user_id/approve' do
          source = find_source(source_type, params[:id])

56
          member = ::Members::ApproveAccessRequestService.new(source, current_user, declared_params).execute
57 58 59 60 61

          status :created
          present member.user, with: Entities::Member, member: member
        end

62 63 64 65 66 67
        desc 'Denies an access request for the given user.' do
          detail 'This feature was introduced in GitLab 8.11.'
        end
        params do
          requires :user_id, type: Integer, desc: 'The user ID of the access requester'
        end
68 69 70
        delete ":id/access_requests/:user_id" do
          source = find_source(source_type, params[:id])

71 72
          ::Members::DestroyService.new(source, current_user, params).
            execute(:requesters)
73 74 75 76 77
        end
      end
    end
  end
end