Class: ForestLiana::ScopeManager
- Inherits:
-
Object
- Object
- ForestLiana::ScopeManager
- Defined in:
- app/services/forest_liana/scope_manager.rb
Constant Summary collapse
- @@scope_cache_expiration_delta =
5 minutes expiration cache
300
Class Method Summary collapse
- .append_scope_for_user(existing_filter, user, collection_name, request_context_variables = nil) ⇒ Object
- .apply_scopes_on_records(records, user, collection_name, timezone) ⇒ Object
- .decode_scope(raw_scopes) ⇒ Object
- .fetch_scopes(rendering_id) ⇒ Object
- .get_scope(collection_name, user, request_context_variables = nil) ⇒ Object
- .inject_context_variables(filter, user, request_context_variables = nil) ⇒ Object
- .invalidate_scope_cache(rendering_id) ⇒ Object
Class Method Details
.append_scope_for_user(existing_filter, user, collection_name, request_context_variables = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/services/forest_liana/scope_manager.rb', line 14 def self.append_scope_for_user(existing_filter, user, collection_name, request_context_variables = nil) existing_filter = inject_context_variables(existing_filter, user, request_context_variables) if existing_filter scope_filter = get_scope(collection_name, user, request_context_variables) filters = [existing_filter, scope_filter].compact case filters.length when 0 nil when 1 filters[0] else { 'aggregator' => 'and', 'conditions' => [existing_filter, scope_filter] } end end |
.apply_scopes_on_records(records, user, collection_name, timezone) ⇒ Object
6 7 8 9 10 11 12 |
# File 'app/services/forest_liana/scope_manager.rb', line 6 def self.apply_scopes_on_records(records, user, collection_name, timezone) scope_filters = get_scope(collection_name, user) return records if scope_filters.blank? FiltersParser.new(scope_filters, records, timezone).apply_filters end |
.decode_scope(raw_scopes) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'app/services/forest_liana/scope_manager.rb', line 69 def self.decode_scope(raw_scopes) scopes = {} raw_scopes.each do |collection_name, value| scopes[collection_name] = value['scope'] unless value['scope'].nil? end scopes end |
.fetch_scopes(rendering_id) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/services/forest_liana/scope_manager.rb', line 51 def self.fetch_scopes(rendering_id) response = ForestLiana::ForestApiRequester.get("/liana/v4/permissions/renderings/#{rendering_id}") if response.is_a?(Net::HTTPOK) Rails.cache.fetch('forest.scopes.' + rendering_id.to_s, expires_in: @@scope_cache_expiration_delta) do data = {} parse_response = JSON.parse(response.body) data['scopes'] = decode_scope(parse_response['collections']) data['team'] = parse_response['team'] data end else raise 'Unable to fetch scopes' end end |
.get_scope(collection_name, user, request_context_variables = nil) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'app/services/forest_liana/scope_manager.rb', line 29 def self.get_scope(collection_name, user, request_context_variables = nil) retrieve = fetch_scopes(user['rendering_id']) scope = retrieve['scopes'][collection_name] return nil if scope.nil? inject_context_variables(scope, user, request_context_variables) end |
.inject_context_variables(filter, user, request_context_variables = nil) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'app/services/forest_liana/scope_manager.rb', line 38 def self.inject_context_variables(filter, user, request_context_variables = nil) filter = JSON.parse(filter) if filter.is_a? String retrieve = fetch_scopes(user['rendering_id']) context_variables = Utils::ContextVariables.new(retrieve['team'], user, request_context_variables) Utils::ContextVariablesInjector.inject_context_in_filter(filter, context_variables) end |
.invalidate_scope_cache(rendering_id) ⇒ Object
47 48 49 |
# File 'app/services/forest_liana/scope_manager.rb', line 47 def self.invalidate_scope_cache(rendering_id) Rails.cache.delete('forest.scopes.' + rendering_id.to_s) end |