diff --git a/app/controllers/robots_controller.rb b/app/controllers/robots_controller.rb new file mode 100644 index 000000000..791986559 --- /dev/null +++ b/app/controllers/robots_controller.rb @@ -0,0 +1,43 @@ +class RobotsController < ApplicationController + before_action :authenticate_user!, except: :show + before_action :find_robots_txt + before_action :throw_breadcrumbs, except: :show + layout 'hyrax/dashboard' + + def show + render body: @robots_txt.value + end + + def edit + authorize! :edit, @robots_txt + end + + def update + authorize! :update, @robots_txt + respond_to do |format| + if @robots_txt.update(permitted_params) + format.html { redirect_to edit_robots_path, notice: 'robots.txt updated.' } + else + flash.now[:alert] = "robots.txt could not be updated. #{@robots_txt.errors.full_messages}" + format.html { render :edit } + end + end + end + + private + + def find_robots_txt + @robots_txt = ContentBlock.find_or_create_by(name: 'robots_txt') + end + + def throw_breadcrumbs + add_breadcrumb t(:'hyrax.controls.home'), root_path + add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path + add_breadcrumb t(:'hyrax.admin.sidebar.configuration'), '#' + add_breadcrumb 'robots.txt', edit_robots_path + end + + def permitted_params + params.require(:content_block).permit(:value) + end +end \ No newline at end of file diff --git a/app/views/hyrax/dashboard/sidebar/_configuration.html.erb b/app/views/hyrax/dashboard/sidebar/_configuration.html.erb new file mode 100644 index 000000000..b1cf1e458 --- /dev/null +++ b/app/views/hyrax/dashboard/sidebar/_configuration.html.erb @@ -0,0 +1,39 @@ + <% if menu.show_configuration? %> +
  • <%= t('hyrax.admin.sidebar.configuration') %>
  • +
  • + <%= menu.collapsable_section t('hyrax.admin.sidebar.settings'), + icon_class: "fa fa-cog", + id: 'collapseSettings', + open: menu.settings_section? do %> + <% if can?(:update, :appearance) %> + <%= menu.nav_link(hyrax.admin_appearance_path) do %> + <%= t('hyrax.admin.sidebar.appearance') %> + <% end %> + <% end %> + <% if can?(:manage, :collection_types) %> + <%= menu.nav_link(hyrax.admin_collection_types_path) do %> + <%= t('hyrax.admin.sidebar.collection_types') %> + <% end %> + <% end %> + <% if can?(:manage, Hyrax::Feature) %> + <%= menu.nav_link(hyrax.edit_pages_path) do %> + <%= t('hyrax.admin.sidebar.pages') %> + <% end %> + <%= menu.nav_link(hyrax.edit_content_blocks_path) do %> + <%= t('hyrax.admin.sidebar.content_blocks') %> + <% end %> + <%= menu.nav_link(hyrax.admin_features_path) do %> + <%= t('hyrax.admin.sidebar.technical') %> + <% end %> + <%= menu.nav_link(main_app.edit_robots_path) do %> + robots.txt + <% end %> + <% end %> + <% end %> +
  • + <% if can?(:manage, Sipity::WorkflowResponsibility) %> + <%= menu.nav_link(hyrax.admin_workflow_roles_path) do %> + <%= t('hyrax.admin.sidebar.workflow_roles') %> + <% end %> + <% end # end of configuration block %> + <% end %> diff --git a/app/views/robots/_form.html.erb b/app/views/robots/_form.html.erb new file mode 100644 index 000000000..c973f8c2a --- /dev/null +++ b/app/views/robots/_form.html.erb @@ -0,0 +1,20 @@ +
    +
    +
    +
    + <%= simple_form_for @robots_txt, url: robots_path do |f| %> +
    +
    + <%= f.label :value, 'robots.txt' %>
    + <%= f.text_area :value, value: f.object.value, class: 'form-control', rows: 20, cols: 120 %> +
    +
    + + <% end %> +
    +
    +
    +
    diff --git a/app/views/robots/edit.html.erb b/app/views/robots/edit.html.erb new file mode 100644 index 000000000..70eb57d0c --- /dev/null +++ b/app/views/robots/edit.html.erb @@ -0,0 +1,9 @@ +<% content_for :page_header do %> +

    Editing robots.txt

    +<% end %> + +
    +
    + <%= render 'form' %> +
    +
    diff --git a/config/routes.rb b/config/routes.rb index 1bfbc479e..916aefe74 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -101,6 +101,9 @@ get '/purl/formats/:id', to: 'purl#formats', as: 'formats_purl' get '/purl/*id', to: 'purl#default', as: 'default_purl' + # robots.txt + resource :robots, only: [:show, :edit, :update] + # Send ActionController::RoutingError to 404 page # Must be the last route defined match '*unmatched', to: 'application#rescue_404', via: :all diff --git a/public/robots.txt b/public/robots.txt deleted file mode 100644 index 37b576a4a..000000000 --- a/public/robots.txt +++ /dev/null @@ -1 +0,0 @@ -# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file diff --git a/spec/controllers/robots_controller_spec.rb b/spec/controllers/robots_controller_spec.rb new file mode 100644 index 000000000..3ac26c358 --- /dev/null +++ b/spec/controllers/robots_controller_spec.rb @@ -0,0 +1,79 @@ +require 'rails_helper' + +describe RobotsController do + let(:user) { create(:user) } + let(:admin) { create(:admin) } + let(:robots_txt) { ContentBlock.create(name: 'robots_txt', value: content) } + let(:content) { "User-Agent: *\nDisallow: /concern" } + + after do + ContentBlock.delete('robots_txt') + end + + describe '#show' do + it 'is blank by default' do + get :show + expect(response).to be_successful + expect(response.body).to eq '' + end + + it 'renders the value' do + robots_txt + get :show + expect(response).to be_successful + expect(response.body).to eq content + end + + it 'is route for /robots.txt', type: :routing do + expect(get: '/robots.txt').to route_to(controller: 'robots', action: 'show', format: 'txt') + end + end + + describe '#edit' do + it 'is unavailable to the public' do + get :edit + expect(response).to redirect_to(new_user_session_path(locale: nil)) + end + + it 'is unavailable to regular users' do + sign_in user + get :edit + expect(response).to be_unauthorized + end + + context 'with rendering' do + render_views + it 'is rendered for admins' do + robots_txt + sign_in admin + get :edit + expect(response).to be_successful + expect(response.body).to include(content) + end + end + end + + describe '#update' do + let(:new_content) { 'Disallow: *' } + + it 'is unavailable to the public' do + patch :update, params: { content_block: { value: new_content } } + expect(response).to redirect_to(new_user_session_path(locale: nil)) + end + + it 'is unavailable to regular users' do + sign_in user + patch :update, params: { content_block: { value: new_content } } + expect(response).to be_unauthorized + end + + it 'is updated for admins' do + robots_txt + sign_in admin + patch :update, params: { content_block: { value: new_content } } + expect(response).to redirect_to(edit_robots_path) + get :show + expect(response.body).to eq new_content + end + end +end \ No newline at end of file