require 'scrivito/blob' module Scrivito # # Configures the Scrivito SDK. # The configuration keys +tenant+ and +api_key+ _must_ be provided. # # @example # Scrivito.configure do |config| # config.tenant = 'my-tenant-name' # config.api_key = 'secret' # end # # @api public # def self.configure yield ::Scrivito::Configuration end # # @api public # class Configuration # # Default path of a CA certification file in PEM format. # # @api public # DEFAULT_CA_FILE = File.expand_path('../../../config/ca-bundle.crt', __FILE__) class << self # Determine if current visitor is permitted to edit content. attr_accessor :editing_auth_callback # Configure a callback to be invoked when the Scrivito SDK determines, # if current visitor is permitted to edit content. # Default is false. # # Example Usage: # Scrivito::Configuation.editing_auth do |env| # request = Rack::Request.new(env) # # return truey if current visitor is permitted to edit content, falsy otherwise # end # @api public def editing_auth(&block) if block.respond_to?(:arity) && block.arity == 1 self.editing_auth_callback = block else raise ArgumentError, 'editing_auth should have only one attribute!' end end # TODO: Legacy compatiblity. Remove when DisplayHelper gets removed. def editor_interface_enabled? false end # The +Scrivito+ makes heavy use of filesystem caching. # Use this method to configure the directory that should be used to store cached data. # By default, +RAILS_ROOT/tmp/scrivito_cache+ will be used. # @param path [String] Path to directory that should be used to store cached data. # @example Configure +Scrivito+ to store its cache under +/tmp/my_cache+. # Scrivito::Configuration.cache_path = '/tmp/my_cache' # @api public def cache_path=(path) CmsCacheStorage.backend_cache = ActiveSupport::Cache::FileStore.new(path) end # # Sets the tenant name. # This configuration key _must_ be provided. # # @api public # def tenant=(tenant_name) @endpoint_uri = nil @tenant = tenant_name end # # Sets the API key. # This configuration key _must_ be provided. # # @api public # attr_writer :api_key # # Sets the API endpoint URL. # This configuration key is optional. # Default is +'api.scrivito.com'+. # If no schema is provided HTTPS is assumed. # # @api public # attr_writer :endpoint # # Gets the path of a CA certification file in PEM format. # # @return [String] # @api public # attr_reader :ca_file # Sets path of a CA certification file in PEM format. # The file can contain several CA certificates. # Certifications will be used for endpoint peer verification of various scrivito services # e.g. Content Read Service. # @api public def ca_file=(path) File.read(path) if path # Try to read the given file and fail if it doesn't exist or is not readable. @ca_file = path end def to_prepare unless Rails.configuration.cache_classes NamedLink.reset_cache BasicObj.reset_type_computer! BasicWidget.reset_type_computer! ::ApplicationController.__send__(:helper, :cms) end end def configure_cms_database end def tenant assert_key_present(:tenant) @tenant end def api_key assert_key_present(:api_key) @api_key end def endpoint assert_key_present(:endpoint) @endpoint end def endpoint_uri @endpoint_uri ||= calculate_endpoint_uri end def set_defaults! self.ca_file = DEFAULT_CA_FILE self.editing_auth { |env| false } self.endpoint = 'api.scrivito.com' end attr_accessor :choose_homepage_callback # Configure a callback to be invoked when the Scrivito SDK delivers the homepage. # The given callback will receive the rack env # and must return an {BasicObj Obj} to be used as the homepage. # If no callback is configured, {BasicObj.homepage Obj.homepage} will be used as the default. # @api public def choose_homepage(&block) self.choose_homepage_callback = block end def cms_routes(*args) raise <<-EOS.gsub(/\s+/, ' ') Calling Scrivito::Configuration.cms_routes is not needed anymore. Please remove it from config/routes.rb EOS end def use_x_sendfile=(value) raise 'Configuration.use_x_sendfile is now available as Rails configuration:'\ ' config.action_dispatch.x_sendfile_header = "X-Sendfile"' end def obj_formats @obj_formats ||= { '_changes_list' => proc do |obj| { id: obj.id, obj_class_name: obj.obj_class_name, description_for_editor: obj.description_for_editor, modification: obj.modification, has_conflict: obj.has_conflict?, last_changed: obj.last_changed.utc.iso8601, is_binary: obj.binary?, } end } end def register_obj_format(name, &block) if name.start_with? '_' raise InvalidFormatNameError.new('Format names starting with underscore are not allowed.') else obj_formats[name] = block end end private def assert_key_present(key) if instance_variable_get("@#{key}").nil? raise "Missing required configuration key '#{key}'." end end def calculate_endpoint_uri url = endpoint url = "https://#{url}" unless url.match(/^http/) url = "#{url}/tenants/#{tenant}" URI.parse(url) end end self.set_defaults! end class InvalidFormatNameError < StandardError end end