Sha256: 6454f20e2c98bf733ffb7cb58572c3bdafd05b1208a08f80ff18e89e00caf8cb

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module Scrivito

class WebserviceController < ActionController::Base
  rescue_from ClientError do |exception|
    render json: {error: exception.message}, status: exception.http_code
  end

  before_filter :merge_correctly_parsed_json_params
  before_filter :authorize

  private

  def authorize
    unless allow_access?
      render text: 'Forbidden', status: 403
    end
  end

  def editing_context
    request.env[EditingContextMiddleware::ENVKEY] || EditingContext.new
  end

  def scrivito_user
    editing_context.editor
  end

  # If +true+, allow access to ObjsController, else deny access.
  # See {Scrivito::Configuration.editing_auth} for details.
  # @return [Bool]
  def allow_access?
    !!scrivito_user
  end

  def able_to?(ability)
    scrivito_user && scrivito_user.able_to?(ability)
  end

  # Workaround for https://github.com/rails/rails/issues/8832
  def merge_correctly_parsed_json_params
    if request.format.json?
      body = request.body.read
      request.body.rewind
      params.merge!(ActiveSupport::JSON.decode(body)) if body.present?
    end
  end
end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scrivito_sdk-0.17.0 app/controllers/scrivito/webservice_controller.rb