-
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.
Introduce Personal details page under /account
This provides the functionality that was lost in the switch from Users#edit to the new Account page but wasn't already covered by one of the existing/repurposed "sub-pages". We've deliberately left out the ability to change your name because we're not convinced that it's ever used. Since this is another mixed bag of a page and another Account sub-page, for consistency I've mostly followed the approaches taken by the Change your email password page.
- Loading branch information
Showing
7 changed files
with
176 additions
and
1 deletion.
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,39 @@ | ||
class Account::PersonalDetailsController < ApplicationController | ||
layout "admin_layout" | ||
|
||
before_action :authenticate_user! | ||
before_action :authorise_user | ||
|
||
def show; end | ||
|
||
def update_organisation | ||
new_organisation_id = params[:user][:organisation_id] | ||
new_organisation = Organisation.find(new_organisation_id) | ||
|
||
if current_user.update(organisation_id: new_organisation_id) | ||
redirect_to account_path, notice: "Your organisation is now #{new_organisation.name}" | ||
else | ||
flash[:alert] = "There was a problem changing your organisation." | ||
render :show | ||
end | ||
end | ||
|
||
def update_role | ||
previous_role = current_user.role | ||
new_role = params[:user][:role] | ||
|
||
if current_user.update(role: new_role) | ||
EventLog.record_role_change(current_user, previous_role, new_role, current_user) | ||
redirect_to account_path, notice: "Your role is now #{new_role.humanize}" | ||
else | ||
flash[:alert] = "There was a problem changing your role." | ||
render :show | ||
end | ||
end | ||
|
||
private | ||
|
||
def authorise_user | ||
authorize %i[account personal_details] | ||
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,7 @@ | ||
class Account::PersonalDetailsPolicy < BasePolicy | ||
def show? | ||
current_user.govuk_admin? | ||
end | ||
alias_method :update_organisation?, :show? | ||
alias_method :update_role?, :show? | ||
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,52 @@ | ||
<% content_for :title, "Personal details" %> | ||
|
||
<% content_for :breadcrumbs, | ||
render("govuk_publishing_components/components/breadcrumbs", { | ||
collapse_on_mobile: true, | ||
breadcrumbs: [ | ||
{ | ||
title: "Dashboard", | ||
url: root_path, | ||
}, | ||
{ | ||
title: "Settings", | ||
url: account_path, | ||
}, | ||
{ | ||
title: "Personal details", | ||
} | ||
] | ||
}) | ||
%> | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<h2 class="govuk-heading-m">Change your role</h2> | ||
|
||
<%= form_for current_user, url: update_role_account_personal_details_path do |f| %> | ||
<%= render "govuk_publishing_components/components/select", { | ||
id: "user_role", | ||
name: "user[role]", | ||
label: "Role", | ||
options: current_user.manageable_roles.map { |role| { text: role.humanize, value: role, selected: current_user.role == role } } | ||
} %> | ||
<%= render "govuk_publishing_components/components/button", { | ||
text: "Change role" | ||
} %> | ||
<% end %> | ||
|
||
<hr class="govuk-section-break govuk-section-break--visible govuk-section-break--xl"> | ||
|
||
<%= form_for current_user, url: update_organisation_account_personal_details_path do |f| %> | ||
<%= render "govuk_publishing_components/components/select", { | ||
id: "user_organisation_id", | ||
name: "user[organisation_id]", | ||
label: "Organisation", | ||
options: policy_scope(Organisation).map { |organisation| { text: organisation.name_with_abbreviation, value: organisation.id, selected: current_user.organisation == organisation } } | ||
} %> | ||
<%= render "govuk_publishing_components/components/button", { | ||
text: "Change organisation" | ||
} %> | ||
<% end %> | ||
</div> | ||
</div> |
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,62 @@ | ||
require "test_helper" | ||
|
||
class AccountPersonalDetailsTest < ActionDispatch::IntegrationTest | ||
context "#show" do | ||
should "only allow GOV.UK Admins access" do | ||
[ | ||
FactoryBot.create(:user), | ||
FactoryBot.create(:organisation_admin_user), | ||
FactoryBot.create(:super_organisation_admin_user), | ||
].each do |non_govuk_admin_user| | ||
visit new_user_session_path | ||
signin_with non_govuk_admin_user | ||
|
||
visit account_personal_details_path | ||
assert page.has_text? "You do not have permission to perform this action." | ||
|
||
signout | ||
end | ||
end | ||
|
||
should "allow user to change their role" do | ||
user = FactoryBot.create(:superadmin_user) | ||
|
||
visit new_user_session_path | ||
signin_with user | ||
|
||
visit account_personal_details_path | ||
|
||
select "Admin", from: "Role" | ||
click_button "Change role" | ||
|
||
assert_current_url account_path | ||
assert page.has_text? "Your role is now Admin" | ||
|
||
visit account_personal_details_path | ||
|
||
assert page.has_select? "Role", selected: "Admin" | ||
end | ||
|
||
should "allow user to change their organisation" do | ||
current_organisation = create(:organisation, name: "Judiciary") | ||
user = FactoryBot.create(:admin_user, organisation: current_organisation) | ||
|
||
create(:organisation, name: "Postage") | ||
|
||
visit new_user_session_path | ||
signin_with user | ||
|
||
visit account_personal_details_path | ||
|
||
select "Postage", from: "Organisation" | ||
click_button "Change organisation" | ||
|
||
assert_current_url account_path | ||
assert page.has_text? "Your organisation is now Postage" | ||
|
||
visit account_personal_details_path | ||
|
||
assert page.has_select? "Organisation", selected: "Postage" | ||
end | ||
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