gitlab-org--gitlab-foss/lib/api/access_requests.rb

83 lines
2.9 KiB
Ruby
Raw Normal View History

module API
class AccessRequests < Grape::API
2016-12-04 12:11:19 -05:00
include PaginationParams
before { authenticate! }
helpers ::API::Helpers::MembersHelpers
%w[group project].each do |source_type|
params do
requires :id, type: String, desc: "The #{source_type} ID"
end
2017-08-31 07:44:49 -04:00
resource source_type.pluralize, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
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
2016-12-04 12:11:19 -05:00
params do
use :pagination
end
get ":id/access_requests" do
source = find_source(source_type, params[:id])
access_requesters = AccessRequestsFinder.new(source).execute!(current_user)
access_requesters = paginate(access_requesters.includes(:user))
present access_requesters, with: Entities::AccessRequester
end
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
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, with: Entities::AccessRequester
else
render_validation_error!(access_requester)
end
end
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
put ':id/access_requests/:user_id/approve' do
source = find_source(source_type, params[:id])
access_requester = source.requesters.find_by!(user_id: params[:user_id])
member = ::Members::ApproveAccessRequestService
.new(current_user, declared_params)
.execute(access_requester)
status :created
present member, with: Entities::Member
end
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
delete ":id/access_requests/:user_id" do
source = find_source(source_type, params[:id])
2017-03-02 07:14:13 -05:00
member = source.requesters.find_by!(user_id: params[:user_id])
2017-03-02 07:14:13 -05:00
destroy_conditionally!(member) do
::Members::DestroyService.new(current_user).execute(member)
2017-03-02 07:14:13 -05:00
end
end
end
end
end
end