generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Functionality to anonymise deactivated users
Here we add functionality to anonymise an already deactivated user. An anonymised user has: - a name which is simply "Deleted User <database id>" - an email address which is "deleted.user.<database id>@<original domain name>" - no telephone number In the frontend, an admin user will see an "Anonymise" button when editing an inactive user; this leads to a confirmation screen which submits a form to UsersController#anonymise_update, which calls the AnonymiseUser service object.
- Loading branch information
1 parent
16c4875
commit f72221f
Showing
13 changed files
with
178 additions
and
2 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
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,22 @@ | ||
class AnonymiseUser | ||
attr_accessor :user | ||
|
||
def initialize(user:) | ||
self.user = user | ||
end | ||
|
||
def call | ||
result = Result.new(true) | ||
|
||
User.transaction do | ||
user.anonymised_at = DateTime.now | ||
new_email = "deleted.user.#{user.id}@#{user.email.split("@").last}" | ||
user.email = new_email | ||
user.name = "Deleted User #{user.id}" | ||
user.mobile_number = "" | ||
result.success = user.save! | ||
end | ||
|
||
result | ||
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,14 @@ | ||
=content_for :page_title_prefix, t("page_title.users.anonymise") | ||
|
||
%main.govuk-main-wrapper#main-content{ role: "main" } | ||
.govuk-grid-row | ||
.govuk-grid-column-two-thirds | ||
%h1.govuk-heading-xl | ||
= t("page_content.users.anonymise.title") | ||
|
||
= t("page_content.users.anonymise.content_html", email: @user.email) | ||
|
||
= form_with model: @user, url: anonymise_update_user_path do |f| | ||
.govuk-button-group | ||
= f.govuk_submit t("page_content.users.button.continue"), class: "govuk-button govuk-button--warning" | ||
= link_to t("page_content.users.button.cancel"), edit_user_path, class: "govuk-button govuk-button--secondary" |
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 |
---|---|---|
|
@@ -14,6 +14,15 @@ | |
expect(user.errors[:email]).to eq([I18n.t("activerecord.errors.models.user.attributes.email.cannot_be_changed")]) | ||
end | ||
|
||
it "should allow an email to be changed if the user is anonymised" do | ||
user = create(:administrator, email: "[email protected]") | ||
|
||
user.anonymised_at = DateTime.now | ||
user.email = "[email protected]" | ||
|
||
expect(user).to be_valid | ||
end | ||
|
||
it "is not case sensitive" do | ||
# When a non-lowercase email address exists (Devise lowercases emails on creation so this is for pre-existing addresses) | ||
user = create(:partner_organisation_user) | ||
|
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,38 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe AnonymiseUser do | ||
let(:user) { create(:administrator) } | ||
|
||
describe "#call" do | ||
it "returns a successful result" do | ||
result = described_class.new(user: user).call | ||
|
||
expect(result.success?).to be(true) | ||
expect(result.failure?).to be(false) | ||
end | ||
|
||
it "anonymises the email address" do | ||
email = user.email | ||
described_class.new(user:).call | ||
expect(user.email).not_to eql(email) | ||
|
||
# Domain element of email address should be unchanged: | ||
expect(user.email.split("@").last).to eql(email.split("@").last) | ||
end | ||
|
||
it "anonymises the name" do | ||
name = user.name | ||
described_class.new(user:).call | ||
expect(user.name).not_to eql(name) | ||
|
||
# Check IDs match; they're in this format: | ||
# "Deleted User <id>", "deleted.user.<id>@<domain>" | ||
expect(user.name.split(" ").last).to eql(user.email.split("@").first.split(".").last) | ||
end | ||
|
||
it "removes the mobile number" do | ||
described_class.new(user:).call | ||
expect(user.mobile_number).to be_blank | ||
end | ||
end | ||
end |