Skip to content

Commit

Permalink
Allow GOV.UK Admins to give themselves access to apps
Browse files Browse the repository at this point in the history
This adds a "Grant access" button next to each app in the list of "Apps
you don't have access to" on /account/applications. This page is
currently only available to GOV.UK Admins and this functionality
replicates what they're already able to do on their /users/<id>/edit page.
  • Loading branch information
chrisroos committed Sep 19, 2023
1 parent 0dfda52 commit 29eabf9
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 2 deletions.
11 changes: 11 additions & 0 deletions app/controllers/account/signin_permissions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Account::SigninPermissionsController < ApplicationController
before_action :authenticate_user!

def create
authorize :account_applications, :grant_signin_permission?

application = Doorkeeper::Application.find(params[:application_id])
current_user.grant_application_signin_permission(application)
redirect_to account_applications_path
end
end
2 changes: 2 additions & 0 deletions app/policies/account_applications_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ class AccountApplicationsPolicy < BasePolicy
def index?
current_user.govuk_admin?
end

alias_method :grant_signin_permission?, :index?
end
10 changes: 9 additions & 1 deletion app/views/account/applications/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header govuk-!-width-one-quarter">Name</th>
<th scope="col" class="govuk-table__header govuk-!-width-three-quarters">Description</th>
<th scope="col" class="govuk-table__header govuk-!-width-two-quarters">Description</th>
<th scope="col" class="govuk-table__header govuk-!-width-one-quarter"></th>
</tr>
</thead>
<tbody class="govuk-table__body">
<% @applications_without_signin.each do |application| %>
<tr class="govuk-table__row">
<td class="govuk-table__cell"><%= application.name %></td>
<td class="govuk-table__cell"><%= application.description %></td>
<td class="govuk-table__cell">
<%= button_to account_application_signin_permission_path(application),
class: "govuk-button govuk-!-margin-0",
data: { module: "govuk-button" } do %>
Grant access<span class="govuk-visually-hidden"> to <%= application.name %></span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@

resource :account, only: [:show]
namespace :account do
resources :applications, only: [:index]
resources :applications, only: [:index] do
resource :signin_permission, only: [:create]
end
end

resources :batch_invitations, only: %i[new create show] do
Expand Down
11 changes: 11 additions & 0 deletions test/controllers/account/signin_permissions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "test_helper"

class Account::SigninPermissionsControllerTest < ActionController::TestCase
should "prevent unauthenticated users from accessing create" do
application = create(:application, name: "app-name", description: "app-description")

post :create, params: { application_id: application.id }

assert_redirected_to "/users/sign_in"
end
end
19 changes: 19 additions & 0 deletions test/integration/account_applications_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,23 @@ class AccountApplicationsTest < ActionDispatch::IntegrationTest
assert_not page.has_content?("retired-app-name")
end
end

context "granting access to apps" do
setup do
create(:application, name: "app-name", description: "app-description")
@user = FactoryBot.create(:admin_user)
end

should "allow admins to grant themselves access to apps" do
visit new_user_session_path
signin_with @user

visit account_applications_path

click_on "Grant access to app-name"

table = find("table caption[text()='Apps you have access to']").ancestor("table")
assert table.has_content?("app-name")
end
end
end
26 changes: 26 additions & 0 deletions test/policies/account_applications_policy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,30 @@ class AccountApplicationsPolicyTest < ActiveSupport::TestCase
end
end
end

context "accessing grant_signin_permission?" do
%i[superadmin admin].each do |user_role|
context "for #{user_role} users" do
setup do
@current_user = FactoryBot.build(:"#{user_role}_user")
end

should "be permitted" do
assert permit?(@current_user, nil, :grant_signin_permission)
end
end
end

%i[super_organisation_admin organisation_admin normal].each do |user_role|
context "for #{user_role} users" do
setup do
@current_user = FactoryBot.build(:"#{user_role}_user")
end

should "be forbidden" do
assert forbid?(@current_user, nil, :grant_signin_permission)
end
end
end
end
end

0 comments on commit 29eabf9

Please sign in to comment.