Sha256: 392e9e7d718192f839225a06baec89c4e9d888c8c87ab602df6a9c42a6186aa1

Contents?: true

Size: 1.73 KB

Versions: 32

Compression:

Stored size: 1.73 KB

Contents

module Scrivito

class WebserviceController < ActionController::Base
  rescue_from ApplicationError, ClientError, ResourceNotFound do |error|
    @error = error
    @timestamp = Time.zone.now
    render 'scrivito/webservice/error', formats: :json, status: error.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
    EditingContextMiddleware.from_request(request)
  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 [Boolean]
  def allow_access?
    !!scrivito_user
  end

  # Workaround for https://github.com/rails/rails/issues/8832
  # + support for binary uploads
  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? && !request.form_data?
    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

  def can_user_read_workspace?(workspace)
    can_user_access_workspace?(:read, workspace)
  end

  helper_method :can_user_read_workspace?
end

end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
infopark_fiona7-1.6.1.1.5 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.6.1.1.1 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.6.1.1.0 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.2.0.2.3 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.2.0.2.1 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.2.0.2.2 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.5.5.1 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.5.5.0 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.2.0.1.4 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.5.4.1 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.5.4.0 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.2.0.1.3 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.5.3.1 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.4.3.0 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.3.2.0 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.3.2.0.pre.beta1 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.2.1.0.pre.beta1 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.5.2.0.0 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.2.0.1.1 app/controllers/scrivito/webservice_controller.rb
infopark_fiona7-1.2.0.0.1 app/controllers/scrivito/webservice_controller.rb