-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2599 from alphagov/manage-permissions-for-api-users
Move API user permission management to design system
- Loading branch information
Showing
19 changed files
with
709 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class ApiUsers::ApplicationsController < ApplicationController | ||
layout "admin_layout" | ||
|
||
before_action :authenticate_user! | ||
|
||
def index | ||
@api_user = ApiUser.find(params[:api_user_id]) | ||
|
||
authorize @api_user | ||
|
||
@applications = @api_user.authorised_applications.merge(Doorkeeper::AccessToken.not_revoked) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class ApiUsers::PermissionsController < ApplicationController | ||
layout "admin_layout" | ||
|
||
before_action :authenticate_user! | ||
before_action :set_user | ||
before_action :set_application | ||
before_action :set_permissions | ||
|
||
def edit | ||
authorize @api_user | ||
end | ||
|
||
def update | ||
authorize @api_user | ||
|
||
UserUpdate.new(@api_user, build_user_update_params, current_user, user_ip_address).call | ||
|
||
flash[:application_id] = @application.id | ||
redirect_to api_user_applications_path(@api_user) | ||
end | ||
|
||
private | ||
|
||
def update_params | ||
params.require(:application).permit(supported_permission_ids: []) | ||
end | ||
|
||
def set_user | ||
@api_user = ApiUser.find(params[:api_user_id]) | ||
end | ||
|
||
def set_application | ||
@application = @api_user.authorised_applications.merge(Doorkeeper::AccessToken.not_revoked).find(params[:application_id]) | ||
end | ||
|
||
def set_permissions | ||
@permissions = @application.sorted_supported_permissions_grantable_from_ui(include_signin: false) | ||
end | ||
|
||
def build_user_update_params | ||
permissions_user_has = @api_user.supported_permissions.pluck(:id) | ||
updatable_permissions_for_this_app = @permissions.pluck(:id) | ||
selected_permissions = update_params[:supported_permission_ids].map(&:to_i) | ||
permissions_to_add = updatable_permissions_for_this_app.intersection(selected_permissions) | ||
permissions_to_remove = updatable_permissions_for_this_app.difference(selected_permissions) | ||
|
||
{ supported_permission_ids: (permissions_user_has + permissions_to_add - permissions_to_remove).sort } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<% content_for :title_caption, "Manage API users" %> | ||
<% content_for :title, "#{@api_user.name}'s applications" %> | ||
|
||
<% content_for :breadcrumbs, | ||
render("govuk_publishing_components/components/breadcrumbs", { | ||
collapse_on_mobile: true, | ||
breadcrumbs: [ | ||
{ | ||
title: "Dashboard", | ||
url: root_path, | ||
}, | ||
{ | ||
title: "API users", | ||
url: api_users_path, | ||
}, | ||
{ | ||
title: @api_user.name, | ||
url: edit_api_user_path(@api_user), | ||
}, | ||
{ | ||
title: "#{@api_user.name}'s applications", | ||
} | ||
] | ||
}) | ||
%> | ||
|
||
<% if flash[:application_id] %> | ||
<% content_for(:custom_alerts) do %> | ||
<%= render "govuk_publishing_components/components/success_alert", { | ||
message: "Permissions updated", | ||
description: message_for_success(flash[:application_id], @api_user), | ||
} %> | ||
<% end %> | ||
<% end %> | ||
|
||
<table class="govuk-table"> | ||
<caption class="govuk-table__caption govuk-table__caption--m">Apps <%= @api_user.name %> has access to</caption> | ||
<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-one-third">Description</th> | ||
<th scope="col" class="govuk-table__header"><span class="govuk-visually-hidden">Permissions</span></th> | ||
</tr> | ||
</thead> | ||
<tbody class="govuk-table__body"> | ||
<% @applications.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 govuk-!-text-align-right"> | ||
<% unless application.sorted_supported_permissions_grantable_from_ui(include_signin: false).empty? %> | ||
<%= link_to edit_api_user_application_permissions_path(@api_user, application), class: "govuk-link" do %> | ||
Update permissions<span class="govuk-visually-hidden"> for <%= application.name %></span> | ||
<% end %> | ||
<% end %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<% content_for :title_caption, "Manage API users" %> | ||
<% content_for :title, "Update #{@api_user.name}'s permissions for #{@application.name}" %> | ||
|
||
<% content_for :breadcrumbs, | ||
render("govuk_publishing_components/components/breadcrumbs", { | ||
collapse_on_mobile: true, | ||
breadcrumbs: [ | ||
{ | ||
title: "Dashboard", | ||
url: root_path, | ||
}, | ||
{ | ||
title: "API users", | ||
url: api_users_path, | ||
}, | ||
{ | ||
title: @api_user.name, | ||
url: edit_api_user_path(@api_user), | ||
}, | ||
{ | ||
title: "#{@api_user.name}'s applications", | ||
url: api_user_applications_path(@api_user), | ||
}, | ||
{ | ||
title: "Update #{@api_user.name}'s permissions for #{@application.name}", | ||
} | ||
] | ||
}) | ||
%> | ||
|
||
<%= form_tag api_user_application_permissions_path(@api_user, @application), method: :patch do |f| %> | ||
<%= render "govuk_publishing_components/components/checkboxes", { | ||
name: "application[supported_permission_ids][]", | ||
heading: "Permissions", | ||
items: @permissions.map { |permission| { label: permission.name, value: permission.id, checked: @api_user.has_permission?(permission) } }, | ||
} %> | ||
|
||
<%= hidden_field_tag "application[supported_permission_ids][]", @application.signin_permission.id, id: "checkboxes-signin" %> | ||
|
||
<div class="govuk-button-group"> | ||
<%= render "govuk_publishing_components/components/button", { | ||
text: "Update permissions" | ||
} %> | ||
|
||
<%= link_to "Cancel", api_user_applications_path(@api_user), class: "govuk-link govuk-link--no-visited-state" %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.