2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-23 15:14:31 +00:00
|
|
|
module API
|
2020-04-28 03:09:53 +00:00
|
|
|
class AccessRequests < Grape::API
|
2016-12-04 17:11:19 +00:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-06-23 15:14:31 +00:00
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
helpers ::API::Helpers::MembersHelpers
|
|
|
|
|
|
|
|
%w[group project].each do |source_type|
|
2016-07-29 14:02:35 +00:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: "The #{source_type} ID"
|
|
|
|
end
|
2018-11-08 12:18:17 +00:00
|
|
|
resource source_type.pluralize, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-07-29 14:02:35 +00:00
|
|
|
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 17:11:19 +00:00
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-23 15:14:31 +00:00
|
|
|
get ":id/access_requests" do
|
|
|
|
source = find_source(source_type, params[:id])
|
|
|
|
|
2016-07-28 17:38:32 +00:00
|
|
|
access_requesters = AccessRequestsFinder.new(source).execute!(current_user)
|
|
|
|
access_requesters = paginate(access_requesters.includes(:user))
|
2016-06-23 15:14:31 +00:00
|
|
|
|
2018-01-27 06:16:56 +00:00
|
|
|
present access_requesters, with: Entities::AccessRequester
|
2016-06-23 15:14:31 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-23 15:14:31 +00:00
|
|
|
|
2016-07-29 14:02:35 +00:00
|
|
|
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
|
2016-06-23 15:14:31 +00:00
|
|
|
post ":id/access_requests" do
|
|
|
|
source = find_source(source_type, params[:id])
|
|
|
|
access_requester = source.request_access(current_user)
|
|
|
|
|
|
|
|
if access_requester.persisted?
|
2018-01-27 06:16:56 +00:00
|
|
|
present access_requester, with: Entities::AccessRequester
|
2016-06-23 15:14:31 +00:00
|
|
|
else
|
|
|
|
render_validation_error!(access_requester)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-29 14:02:35 +00:00
|
|
|
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
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-23 15:14:31 +00:00
|
|
|
put ':id/access_requests/:user_id/approve' do
|
|
|
|
source = find_source(source_type, params[:id])
|
|
|
|
|
2017-10-11 14:47:08 +00:00
|
|
|
access_requester = source.requesters.find_by!(user_id: params[:user_id])
|
|
|
|
member = ::Members::ApproveAccessRequestService
|
2018-02-16 14:10:22 +00:00
|
|
|
.new(current_user, declared_params)
|
2017-10-11 14:47:08 +00:00
|
|
|
.execute(access_requester)
|
2016-06-23 15:14:31 +00:00
|
|
|
|
|
|
|
status :created
|
2018-01-27 06:16:56 +00:00
|
|
|
present member, with: Entities::Member
|
2016-06-23 15:14:31 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-23 15:14:31 +00:00
|
|
|
|
2016-07-29 14:02:35 +00:00
|
|
|
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
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-23 15:14:31 +00:00
|
|
|
delete ":id/access_requests/:user_id" do
|
|
|
|
source = find_source(source_type, params[:id])
|
2017-03-02 12:14:13 +00:00
|
|
|
member = source.requesters.find_by!(user_id: params[:user_id])
|
2017-03-01 13:35:48 +00:00
|
|
|
|
2017-03-02 12:14:13 +00:00
|
|
|
destroy_conditionally!(member) do
|
2018-02-16 14:10:22 +00:00
|
|
|
::Members::DestroyService.new(current_user).execute(member)
|
2017-03-02 12:14:13 +00:00
|
|
|
end
|
2016-06-23 15:14:31 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-23 15:14:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|