module Scrivito class WorkspacesController < WebserviceController around_action only: [:check, :publish_approval] do |_, block| authorize_workspace_access(:publish, current_workspace, &block) end def check assert_workspace_is_not_outdated unless current_workspace.auto_update? @check_result = publish_checker.call(params[:from].to_i) end def publish_approval if valid_publish_request? @approval = current_workspace.api_request(:put, '/publish_approval', if_content_state_id_equals: current_workspace.content_state_id, user_id: scrivito_user.id) else raise_content_state_id_changed end rescue ScrivitoError => e raise ClientError.new(e.message) end private def valid_publish_request? publish_checker.passing_certificates?(certificates_param) end def publish_checker Workspace::PublishChecker.new(current_workspace, scrivito_user) end def current_workspace @current_workspace ||= Workspace.find(params[:id]) end def certificates_param assert_valid_certificates_param params[:certificates] end def assert_workspace_is_not_outdated if current_workspace.outdated? raise ClientError.new( 'Workspace is outdated. Please update your working copy.', backend_code: 'precondition_not_verifiable.workspace.publish.content_state_id', ) end end def assert_valid_certificates_param if params[:certificates].blank? raise ClientError.new("Required parameter 'certificates' is missing.") end unless params[:certificates].is_a?(Array) raise ClientError.new("Parameter 'certificates' is not an array.") end end def raise_content_state_id_changed raise ClientError.new( 'Content state id changed', backend_code: 'precondition_not_met.workspace.publish.content_state_id', ) end end end