module ForestLiana class PermissionsChecker @@permissions = nil @@last_retrieve = nil @@expiration_in_seconds = (ENV['FOREST_PERMISSIONS_EXPIRATION_IN_SECONDS'] || 3600).to_i def initialize(resource, permission_name) @collection_name = ForestLiana.name_for(resource) @permission_name = permission_name end def is_authorized? (is_permission_expired? || !is_allowed?) ? retrieve_permissions_and_check_allowed : true end private def is_allowed? if @@permissions && @@permissions[@collection_name] && @@permissions[@collection_name]['collection'] @@permissions[@collection_name]['collection'][@permission_name] else false end end def retrieve_permissions @@permissions = PermissionsGetter.new.perform @@last_retrieve = Time.now end def date_difference_in_seconds(date1, date2) (date1 - date2).to_i end def is_permission_expired? return true if @@last_retrieve.nil? elapsed_seconds = date_difference_in_seconds(Time.now, @@last_retrieve) elapsed_seconds >= @@expiration_in_seconds end def retrieve_permissions_and_check_allowed retrieve_permissions is_allowed? end end end