2018-08-03 13:22:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-12-07 04:11:41 -05:00
|
|
|
# Returns and caches in thread max member access for a resource
|
|
|
|
#
|
|
|
|
module BulkMemberAccessLoad
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
# Determine the maximum access level for a group of resources in bulk.
|
|
|
|
#
|
|
|
|
# Returns a Hash mapping resource ID -> maximum access level.
|
|
|
|
def max_member_access_for_resource_ids(resource_klass, resource_ids, memoization_index = self.id, &block)
|
|
|
|
raise 'Block is mandatory' unless block_given?
|
|
|
|
|
|
|
|
resource_ids = resource_ids.uniq
|
2021-04-13 14:11:28 -04:00
|
|
|
access = load_access_hash(resource_klass, memoization_index)
|
2017-12-07 04:11:41 -05:00
|
|
|
|
|
|
|
# Look up only the IDs we need
|
2020-09-02 08:10:35 -04:00
|
|
|
resource_ids -= access.keys
|
2017-12-07 04:11:41 -05:00
|
|
|
|
|
|
|
return access if resource_ids.empty?
|
|
|
|
|
|
|
|
resource_access = yield(resource_ids)
|
|
|
|
|
|
|
|
access.merge!(resource_access)
|
|
|
|
|
|
|
|
missing_resource_ids = resource_ids - resource_access.keys
|
|
|
|
|
|
|
|
missing_resource_ids.each do |resource_id|
|
|
|
|
access[resource_id] = Gitlab::Access::NO_ACCESS
|
|
|
|
end
|
|
|
|
|
|
|
|
access
|
|
|
|
end
|
|
|
|
|
2021-04-13 14:11:28 -04:00
|
|
|
def merge_value_to_request_store(resource_klass, resource_id, memoization_index, value)
|
|
|
|
max_member_access_for_resource_ids(resource_klass, [resource_id], memoization_index) do
|
|
|
|
{ resource_id => value }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-07 04:11:41 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def max_member_access_for_resource_key(klass, memoization_index)
|
|
|
|
"max_member_access_for_#{klass.name.underscore.pluralize}:#{memoization_index}"
|
|
|
|
end
|
2021-04-13 14:11:28 -04:00
|
|
|
|
|
|
|
def load_access_hash(resource_klass, memoization_index)
|
|
|
|
key = max_member_access_for_resource_key(resource_klass, memoization_index)
|
|
|
|
|
|
|
|
access = {}
|
|
|
|
if Gitlab::SafeRequestStore.active?
|
|
|
|
Gitlab::SafeRequestStore[key] ||= {}
|
|
|
|
access = Gitlab::SafeRequestStore[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
access
|
|
|
|
end
|
2017-12-07 04:11:41 -05:00
|
|
|
end
|
|
|
|
end
|