Skip to content

Commit

Permalink
Re-use authorisation logic in the controller_methods class
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Avlonitis committed Apr 23, 2024
1 parent da351b0 commit acec73d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 79 deletions.
33 changes: 1 addition & 32 deletions lib/gds-sso/controller_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,7 @@ def authorise_user!(permissions)
# Otherwise current_user might be nil, and we'd error out
authenticate_user!

case permissions
when String
unless current_user.has_permission?(permissions)
raise PermissionDeniedException, "Sorry, you don't seem to have the #{permissions} permission for this app."
end
when Hash
raise ArgumentError, "Must be either `any_of` or `all_of`" unless permissions.keys.size == 1

if permissions[:any_of]
authorise_user_with_at_least_one_of_permissions!(permissions[:any_of])
elsif permissions[:all_of]
authorise_user_with_all_permissions!(permissions[:all_of])
else
raise ArgumentError, "Must be either `any_of` or `all_of`"
end
end
GDS::SSO::AuthoriseUser.call(current_user, permissions)
end

def authenticate_user!
Expand All @@ -73,22 +58,6 @@ def logout
def warden
request.env["warden"]
end

private

def authorise_user_with_at_least_one_of_permissions!(permissions)
if permissions.none? { |permission| current_user.has_permission?(permission) }
raise PermissionDeniedException,
"Sorry, you don't seem to have any of the permissions: #{permissions.to_sentence} for this app."
end
end

def authorise_user_with_all_permissions!(permissions)
unless permissions.all? { |permission| current_user.has_permission?(permission) }
raise PermissionDeniedException,
"Sorry, you don't seem to have all of the permissions: #{permissions.to_sentence} for this app."
end
end
end
end
end
61 changes: 14 additions & 47 deletions spec/controller/controller_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,24 @@
require "spec_helper"

RSpec.describe GDS::SSO::ControllerMethods, "#authorise_user!" do
let(:current_user) { double }
let(:expected_error) { GDS::SSO::ControllerMethods::PermissionDeniedException }
RSpec.describe GDS::SSO::ControllerMethods do
describe "#authorise_user!" do
let(:current_user) { double }
let(:expected_error) { GDS::SSO::PermissionDeniedError }

context "with a single string permission argument" do
it "permits users with the required permission" do
allow(current_user).to receive(:has_permission?).with("good").and_return(true)
context "when the user is authorised" do
it "does not raise an error" do
allow(current_user).to receive(:has_permission?).with("good").and_return(true)

expect { ControllerSpy.new(current_user).authorise_user!("good") }.not_to raise_error
expect { ControllerSpy.new(current_user).authorise_user!("good") }.not_to raise_error
end
end

it "does not permit the users without the required permission" do
allow(current_user).to receive(:has_permission?).with("good").and_return(false)
context "when the user is not authorised" do
it "raises an error" do
allow(current_user).to receive(:has_permission?).with("bad").and_return(false)

expect { ControllerSpy.new(current_user).authorise_user!("good") }.to raise_error(expected_error)
end
end

context "with the `all_of` option" do
it "permits users with all of the required permissions" do
allow(current_user).to receive(:has_permission?).with("good").and_return(true)
allow(current_user).to receive(:has_permission?).with("bad").and_return(true)

expect { ControllerSpy.new(current_user).authorise_user!(all_of: %w[good bad]) }.not_to raise_error
end

it "does not permit users without all of the required permissions" do
allow(current_user).to receive(:has_permission?).with("good").and_return(false)
allow(current_user).to receive(:has_permission?).with("bad").and_return(true)

expect { ControllerSpy.new(current_user).authorise_user!(all_of: %w[good bad]) }.to raise_error(expected_error)
end
end

context "with the `any_of` option" do
it "permits users with any of the required permissions" do
allow(current_user).to receive(:has_permission?).with("good").and_return(true)
allow(current_user).to receive(:has_permission?).with("bad").and_return(false)

expect { ControllerSpy.new(current_user).authorise_user!(any_of: %w[good bad]) }.not_to raise_error
end

it "does not permit users without any of the required permissions" do
allow(current_user).to receive(:has_permission?).and_return(false)

expect { ControllerSpy.new(current_user).authorise_user!(any_of: %w[good bad]) }.to raise_error(expected_error)
end
end

context "with none of `any_of` or `all_of`" do
it "raises an `ArgumentError`" do
expect { ControllerSpy.new(current_user).authorise_user!(whoops: "bad") }.to raise_error(ArgumentError)
expect { ControllerSpy.new(current_user).authorise_user!("bad") }.to raise_error(expected_error)
end
end
end
end

0 comments on commit acec73d

Please sign in to comment.