Sha256: ce0e1b5d5cd1fa3b1283cea6a88808799eaab8532ea7197cb5ba230c52b0879f

Contents?: true

Size: 1.5 KB

Versions: 18

Compression:

Stored size: 1.5 KB

Contents

module Scrivito

class WebserviceController < ActionController::Base
  rescue_from ClientError do |exception|
    @exception = exception
    render 'scrivito/webservice/error', formats: :json, status: exception.http_code
  end

  before_filter :merge_correctly_parsed_json_params
  before_filter :authorize

  private

  def authorize
    render_forbidden unless allow_access?
  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

  # 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
  rescue JSON::ParserError => e
    # Rails TestRequest mixes up arguments, therefore ignore elements here
    raise e unless Rails.env.test?
  end

  def can_user_access_workspace?(verb, workspace)
    scrivito_user.can?(verb, workspace)
  end

  def authorize_workspace_access(verb, workspace)
    can_user_access_workspace?(verb, workspace) ? yield : render_forbidden
  end

  def render_forbidden
    render text: 'Forbidden', status: 403
  end

  def render_empty_json
    render 'scrivito/webservice/empty', formats: :json
  end
end

end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
scrivito_sdk-0.50.1 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.50.0 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.50.0.rc2 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.50.0.rc1 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.42.1 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.42.1.rc3 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.42.1.rc2 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.42.1.rc1 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.42.0 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.41.1 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.41.0 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.41.0.rc2 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.41.0.rc1 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.40.0 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.40.0.rc2 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.40.0.rc1 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.30.0 app/controllers/scrivito/webservice_controller.rb
scrivito_sdk-0.30.0.rc1 app/controllers/scrivito/webservice_controller.rb