Skip to content

Commit

Permalink
Only allow Site managers to create new sites
Browse files Browse the repository at this point in the history
This adds authorization that only people with the `Site manager` role in GOV.UK
Signon can create new sites.

This is so that we can safely test new features before we officially archive
Transition Config.
  • Loading branch information
jkempster34 committed Sep 5, 2023
1 parent 5df662e commit 6f6e253
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
8 changes: 8 additions & 0 deletions app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class SitesController < ApplicationController
before_action :find_site, only: %i[edit update show]
before_action :find_organisation, only: %i[new create]
before_action :check_user_is_gds_editor, only: %i[edit update]
before_action :check_user_is_site_manager, only: %i[new create]

def new
@site_form = SiteForm.new(organisation_slug: @organisation.whitehall_slug)
Expand Down Expand Up @@ -72,4 +73,11 @@ def check_user_is_gds_editor
redirect_to site_path(@site), alert: message
end
end

def check_user_is_site_manager
unless current_user.site_manager?
message = "Only Site Managers can access that."
redirect_to organisation_path(@organisation), alert: message
end
end
end
4 changes: 3 additions & 1 deletion app/views/organisations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<%= render 'in_conjunction_with' %>
</header>

<%= link_to "Add a transition site", new_organisation_site_path(@organisation), class: "btn btn-default" %>
<% if current_user.site_manager? %>
<%= link_to "Add a transition site", new_organisation_site_path(@organisation), class: "btn btn-default" %>
<% end %>
<% unless @sites.empty? %>
<h2>Sites</h2>
Expand Down
2 changes: 1 addition & 1 deletion features/site.feature
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Scenario: Jumping to a non-existent site
Then I should see the header "Unknown site"

Scenario: Creating a site
Given I have logged in as a GDS Editor
Given I have logged in as a Site Manager
And there are these organisations without sites:
| whitehall_slug | title |
| ukti | UK Trade & Industry |
Expand Down
32 changes: 31 additions & 1 deletion spec/controllers/sites_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,47 @@
describe SitesController do
let(:site) { create :site, abbr: "moj" }
let(:gds_bob) { create(:gds_editor, name: "Bob Terwhilliger") }
let(:site_manager) { create(:site_manager) }

describe "#new" do
let(:organisation) { create(:organisation) }

before { login_as gds_bob }
before { login_as site_manager }

it "returns a success response" do
get :new, params: { organisation_id: organisation.whitehall_slug }

expect(response.status).to eql(200)
end

context "when the user does not have permission" do
def make_request
get :new, params: { organisation_id: organisation.whitehall_slug }
end

it_behaves_like "disallows editing by non-Site managers"
end
end

describe "#create" do
let(:organisation) { create(:organisation) }
let(:params) { attributes_for(:site_form) }

before { login_as site_manager }

it "returns a success response" do
post :create, params: { organisation_id: organisation.whitehall_slug, site_form: params }

expect(response.status).to eql(200)
end

context "when the user does not have permission" do
def make_request
post :create, params: { organisation_id: organisation.whitehall_slug, site_form: params }
end

it_behaves_like "disallows editing by non-Site managers"
end
end

describe "#edit" do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/site_creation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

describe "Site creation", type: :request do
let!(:gds_bob) { create(:gds_editor, name: "Bob Terwhilliger") }
let!(:site_manager) { create(:site_manager) }
let(:organisation) { create(:organisation, whitehall_slug: "air-accidents-investigation-branch") }
let(:params) { attributes_for :site_form, :with_optional_fields, :with_aliases, organisation_slug: "air-accidents-investigation-branch" }

Expand Down
15 changes: 15 additions & 0 deletions spec/support/shared_examples/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
end
end

shared_examples "disallows editing by non-Site managers" do
before do
login_as stub_user
make_request
end

it "redirects to the organisation page" do
expect(response).to redirect_to organisation_path(organisation)
end

it "sets a flash message" do
expect(flash[:alert]).to eql("Only Site Managers can access that.")
end
end

shared_examples "disallows editing of a global site" do
before do
make_request
Expand Down

0 comments on commit 6f6e253

Please sign in to comment.