summaryrefslogtreecommitdiff
path: root/app/services/members/approve_access_request_service.rb
blob: 416aee2ab51d94e5a3575ff6b5934cb8ed8e5f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Members
  class ApproveAccessRequestService < BaseService
    include MembersHelper

    attr_accessor :source

    def initialize(source, current_user, params = {})
      @source = source
      @current_user = current_user
      @params = params
    end

    def execute
      condition = params[:user_id] ? { user_id: params[:user_id] } : { id: params[:id] }
      access_requester = source.requesters.find_by!(condition)

      raise Gitlab::Access::AccessDeniedError unless can_update_access_requester?(access_requester)

      access_requester.access_level = params[:access_level] if params[:access_level]
      access_requester.accept_request

      access_requester
    end

    private

    def can_update_access_requester?(access_requester)
      access_requester && can?(current_user, action_member_permission(:update, access_requester), access_requester)
    end
  end
end