-
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 #2558 from alphagov/update-permission-editing-for-…
…other-users Make editing the permissions of other users the same as editing your own permissions
- Loading branch information
Showing
28 changed files
with
1,570 additions
and
503 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,20 @@ | ||
class Users::ApplicationsController < ApplicationController | ||
layout "admin_layout" | ||
|
||
before_action :authenticate_user! | ||
|
||
def show | ||
user = User.find(params[:user_id]) | ||
authorize user, :edit? | ||
|
||
redirect_to user_applications_path(user) | ||
end | ||
|
||
def index | ||
@user = User.find(params[:user_id]) | ||
authorize @user, :edit? | ||
|
||
@applications_with_signin = Doorkeeper::Application.not_api_only.can_signin(@user) | ||
@applications_without_signin = Doorkeeper::Application.not_api_only.without_signin_permission_for(@user) | ||
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,46 @@ | ||
class Users::PermissionsController < ApplicationController | ||
layout "admin_layout" | ||
|
||
before_action :authenticate_user! | ||
before_action :set_user | ||
before_action :set_application | ||
|
||
def show | ||
authorize @user, :edit? | ||
|
||
@permissions = @application | ||
.sorted_supported_permissions_grantable_from_ui | ||
.sort_by { |permission| @user.has_permission?(permission) ? 0 : 1 } | ||
end | ||
|
||
def edit | ||
authorize UserApplicationPermission.for(@user, @application) | ||
|
||
@permissions = @application.sorted_supported_permissions_grantable_from_ui(include_signin: false) | ||
end | ||
|
||
def update | ||
authorize UserApplicationPermission.for(@user, @application) | ||
|
||
permission_ids_for_other_applications = @user.supported_permissions.excluding_application(@application).pluck(:id) | ||
user_update_params = { supported_permission_ids: permission_ids_for_other_applications + update_params[:supported_permission_ids].map(&:to_i) } | ||
UserUpdate.new(@user, user_update_params, current_user, user_ip_address).call | ||
|
||
flash[:application_id] = @application.id | ||
redirect_to user_applications_path(@user) | ||
end | ||
|
||
private | ||
|
||
def update_params | ||
params.require(:application).permit(supported_permission_ids: []) | ||
end | ||
|
||
def set_user | ||
@user = User.find(params[:user_id]) | ||
end | ||
|
||
def set_application | ||
@application = Doorkeeper::Application.with_signin_permission_for(@user).not_api_only.find(params[:application_id]) | ||
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,40 @@ | ||
class Users::SigninPermissionsController < ApplicationController | ||
layout "admin_layout" | ||
|
||
before_action :authenticate_user! | ||
before_action :set_user | ||
before_action :set_application, except: [:create] | ||
|
||
def create | ||
application = Doorkeeper::Application.not_api_only.find(params[:application_id]) | ||
authorize UserApplicationPermission.for(@user, application) | ||
|
||
params = { supported_permission_ids: @user.supported_permissions.map(&:id) + [application.signin_permission.id] } | ||
UserUpdate.new(@user, params, current_user, user_ip_address).call | ||
|
||
redirect_to user_applications_path(@user) | ||
end | ||
|
||
def delete | ||
authorize UserApplicationPermission.for(@user, @application) | ||
end | ||
|
||
def destroy | ||
authorize UserApplicationPermission.for(@user, @application) | ||
|
||
params = { supported_permission_ids: @user.supported_permissions.map(&:id) - [@application.signin_permission.id] } | ||
UserUpdate.new(@user, params, current_user, user_ip_address).call | ||
|
||
redirect_to user_applications_path(@user) | ||
end | ||
|
||
private | ||
|
||
def set_user | ||
@user = User.find(params[:user_id]) | ||
end | ||
|
||
def set_application | ||
@application = Doorkeeper::Application.with_signin_permission_for(@user).not_api_only.find(params[:application_id]) | ||
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 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,25 @@ | ||
module ApplicationPermissionsHelper | ||
def message_for_success(application_id, user = current_user) | ||
application = Doorkeeper::Application.find_by(id: application_id) | ||
return nil unless application | ||
|
||
additional_permissions = user.permissions_for(application).reject { |permission| permission == SupportedPermission::SIGNIN_NAME } | ||
|
||
if additional_permissions.any? | ||
prefix = user == current_user ? "You now have" : "#{user.name} now has" | ||
paragraph = tag.p("#{prefix} the following permissions for #{application.name}:", class: "govuk-body") | ||
list = tag.ul(class: "govuk-list govuk-list--bullet") | ||
additional_permissions.map { |permission| list << tag.li(permission) } | ||
else | ||
string = if user == current_user | ||
"You can access #{application.name} but you do not have any additional permissions." | ||
else | ||
"#{user.name} can access #{application.name} but does not have any additional permissions." | ||
end | ||
paragraph = tag.p(string, class: "govuk-body") | ||
list = nil | ||
end | ||
|
||
paragraph + list | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class UserApplicationPermissionPolicy < BasePolicy | ||
def create? | ||
return false unless Pundit.policy(current_user, user).edit? | ||
|
||
return true if current_user.govuk_admin? | ||
|
||
current_user.publishing_manager? && current_user.has_access_to?(application) && application.signin_permission.delegatable? | ||
end | ||
alias_method :destroy?, :create? | ||
alias_method :delete?, :create? | ||
alias_method :update?, :create? | ||
alias_method :edit?, :create? | ||
|
||
delegate :user, :application, to: :record | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<% content_for :title_caption, "Manage other users" %> | ||
<% content_for :title, "#{@user.name}'s applications" %> | ||
|
||
<% content_for :breadcrumbs, | ||
render("govuk_publishing_components/components/breadcrumbs", { | ||
collapse_on_mobile: true, | ||
breadcrumbs: [ | ||
{ | ||
title: "Dashboard", | ||
url: root_path, | ||
}, | ||
{ | ||
title: "Users", | ||
url: users_path, | ||
}, | ||
{ | ||
title: @user.name, | ||
url: edit_user_path(@user), | ||
}, | ||
{ | ||
title: "#{@user.name}'s applications", | ||
} | ||
] | ||
}) | ||
%> | ||
|
||
<% if flash[:application_id] %> | ||
<%= render "govuk_publishing_components/components/success_alert", { | ||
message: "Permissions updated", | ||
description: message_for_success(flash[:application_id], @user), | ||
} %> | ||
<% end %> | ||
|
||
<table class="govuk-table"> | ||
<caption class="govuk-table__caption govuk-table__caption--m">Apps <%= @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> | ||
<th scope="col" class="govuk-table__header"><span class="govuk-visually-hidden">Remove access</span></th> | ||
</tr> | ||
</thead> | ||
<tbody class="govuk-table__body"> | ||
<% @applications_with_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 govuk-!-text-align-right"> | ||
<% if policy(UserApplicationPermission.for(@user, application)).edit? %> | ||
<% unless application.sorted_supported_permissions_grantable_from_ui(include_signin: false).empty? %> | ||
<%= link_to edit_user_application_permissions_path(@user, application), class: "govuk-link" do %> | ||
Update permissions<span class="govuk-visually-hidden"> for <%= application.name %></span> | ||
<% end %> | ||
<% end %> | ||
<% else %> | ||
<%= link_to user_application_permissions_path(@user, application), class: "govuk-link" do %> | ||
View permissions<span class="govuk-visually-hidden"> for <%= application.name %></span> | ||
<% end %> | ||
<% end %> | ||
</td> | ||
<td class="govuk-table__cell govuk-!-text-align-right"> | ||
<% if policy(UserApplicationPermission.for(@user, application)).delete? %> | ||
<%= link_to delete_user_application_signin_permission_path(@user, application), | ||
class: "govuk-button govuk-button--warning govuk-!-margin-0", | ||
data: { module: "govuk-button" } do %> | ||
Remove access<span class="govuk-visually-hidden"> to <%= application.name %></span> | ||
<% end %> | ||
<% end %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<h2 class="govuk-heading-m" id="other-apps-table-heading">Apps <%= @user.name %> does not have access to</h2> | ||
|
||
<table class="govuk-table" aria-labelledby="other-apps-table-heading"> | ||
<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">Grant access</span></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 govuk-!-text-align-right"> | ||
<% if policy(UserApplicationPermission.for(@user, application)).create? %> | ||
<%= button_to user_application_signin_permission_path(@user, application), | ||
class: "govuk-button govuk-!-margin-0", | ||
data: { module: "govuk-button" } do %> | ||
Grant access<span class="govuk-visually-hidden"> to <%= 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
Oops, something went wrong.