2016-07-28 13:30:34 -04:00
|
|
|
module Members
|
|
|
|
class ApproveAccessRequestService < BaseService
|
|
|
|
include MembersHelper
|
|
|
|
|
|
|
|
attr_accessor :source
|
|
|
|
|
2016-10-28 05:03:08 -04:00
|
|
|
# source - The source object that respond to `#requesters` (i.g. project or group)
|
|
|
|
# current_user - The user that performs the access request approval
|
|
|
|
# params - A hash of parameters
|
|
|
|
# :user_id - User ID used to retrieve the access requester
|
|
|
|
# :id - Member ID used to retrieve the access requester
|
|
|
|
# :access_level - Optional access level set when the request is accepted
|
2016-07-28 13:30:34 -04:00
|
|
|
def initialize(source, current_user, params = {})
|
|
|
|
@source = source
|
|
|
|
@current_user = current_user
|
2016-10-28 05:03:08 -04:00
|
|
|
@params = params.slice(:user_id, :id, :access_level)
|
2016-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
|
2016-10-28 05:03:08 -04:00
|
|
|
# opts - A hash of options
|
|
|
|
# :force - Bypass permission check: current_user can be nil in that case
|
|
|
|
def execute(opts = {})
|
2016-09-09 12:06:36 -04:00
|
|
|
condition = params[:user_id] ? { user_id: params[:user_id] } : { id: params[:id] }
|
|
|
|
access_requester = source.requesters.find_by!(condition)
|
2016-07-28 13:30:34 -04:00
|
|
|
|
2016-10-28 05:03:08 -04:00
|
|
|
raise Gitlab::Access::AccessDeniedError unless can_update_access_requester?(access_requester, opts)
|
2016-07-28 13:30:34 -04:00
|
|
|
|
|
|
|
access_requester.access_level = params[:access_level] if params[:access_level]
|
|
|
|
access_requester.accept_request
|
|
|
|
|
|
|
|
access_requester
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-28 05:03:08 -04:00
|
|
|
def can_update_access_requester?(access_requester, opts = {})
|
|
|
|
access_requester && (
|
|
|
|
opts[:force] ||
|
|
|
|
can?(current_user, action_member_permission(:update, access_requester), access_requester)
|
|
|
|
)
|
2016-07-28 13:30:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|