debian-mirror-gitlab/app/models/concerns/bulk_member_access_load.rb

62 lines
1.7 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
# 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.
2021-12-07 22:27:20 +05:30
def max_member_access_for_resource_ids(resource_klass, resource_ids, &block)
2018-03-17 18:26:18 +05:30
raise 'Block is mandatory' unless block_given?
2021-12-07 22:27:20 +05:30
memoization_index = self.id
memoization_class = self.class
2018-03-17 18:26:18 +05:30
resource_ids = resource_ids.uniq
2021-12-07 22:27:20 +05:30
memo_id = "#{memoization_class}:#{memoization_index}"
access = load_access_hash(resource_klass, memo_id)
2018-03-17 18:26:18 +05:30
# Look up only the IDs we need
2020-11-24 15:15:51 +05:30
resource_ids -= access.keys
2018-03-17 18:26:18 +05:30
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-12-07 22:27:20 +05:30
def merge_value_to_request_store(resource_klass, resource_id, value)
max_member_access_for_resource_ids(resource_klass, [resource_id]) do
2021-04-29 21:17:54 +05:30
{ resource_id => value }
end
end
2018-03-17 18:26:18 +05:30
private
def max_member_access_for_resource_key(klass, memoization_index)
"max_member_access_for_#{klass.name.underscore.pluralize}:#{memoization_index}"
end
2021-04-29 21:17:54 +05:30
2021-12-07 22:27:20 +05:30
def load_access_hash(resource_klass, memo_id)
return {} unless Gitlab::SafeRequestStore.active?
2021-04-29 21:17:54 +05:30
2021-12-07 22:27:20 +05:30
key = max_member_access_for_resource_key(resource_klass, memo_id)
Gitlab::SafeRequestStore[key] ||= {}
2021-04-29 21:17:54 +05:30
2021-12-07 22:27:20 +05:30
Gitlab::SafeRequestStore[key]
2021-04-29 21:17:54 +05:30
end
2018-03-17 18:26:18 +05:30
end
end