module Scrivito class WorkspacesController < WebserviceController def self.restrict_access_to(action_name, require_permission:) around_action only: action_name do |_, block| subject = params.key?(:id) ? current_workspace : :workspace authorize_workspace_access(require_permission, subject, &block) end end restrict_access_to :show, require_permission: :read restrict_access_to :rebase, require_permission: :write restrict_access_to :check, require_permission: :publish restrict_access_to :publish, require_permission: :publish def index @workspaces = Workspace.all.select(&method(:can_user_read_workspace?)) render :workspaces end def show end def rebase @task = task_unaware_request(:put, current_workspace_backend_path('/rebase'), {})['task'] render :task 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 if valid_publish_request? @task = conditional_publish['task'] render :task else raise_content_state_id_changed end rescue ScrivitoError => e raise ClientError.new(e.message) end private delegate :task_unaware_request, to: CmsRestApi def current_workspace_backend_path(suffix) "/workspaces/#{current_workspace.id}#{suffix}" end def conditional_publish task_unaware_request(:put, current_workspace_backend_path('/publish'), if_content_state_id_equals: current_workspace.content_state_id) end 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 helper_method :current_workspace def workspace_params assert_valid_workspace_params params[:workspace] 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 assert_valid_workspace_params raise "Required parameter 'workspace' is missing." unless params[:workspace].present? raise "Parameter 'workspace' is not a hash." unless params[:workspace].is_a?(Hash) 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