Skip to content

Commit

Permalink
Allow users to be deactivated and re-activated
Browse files Browse the repository at this point in the history
This work builds on switching from an `active` boolean to a
`deactivated_at` date and time. Later this switch will allow us to know
how long a user has been deactivated for and 'anonymise' them if they
meet our retention policies.

Here we are enabling users to be deactivated and re-activated in the
same was as they were before, this work is temporary as we plan to
introduce a new UI for managing the state of a user.

We update the service that updates users so it can set or nil the
deactivated_at date and time, we must set it to nil when re-activating.

At this point the application is working and out tests our passing, this
is the smallest work we can do to get out new model into the
application.
  • Loading branch information
mec committed Dec 13, 2024
1 parent 2e15707 commit ce32688
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

[Full changelog][unreleased]

- The date and time a user is deactivated is now stored

## Release 156 - 2024-12-12

[Full changelog][156]
Expand Down
7 changes: 4 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def new
end

def create
@user = User.new(user_params)
@user = User.new(user_params.except(:active))
authorize @user
@service_owner = service_owner
@partner_organisations = partner_organisations
Expand Down Expand Up @@ -60,10 +60,11 @@ def update
@partner_organisations = partner_organisations

reset_mfa = user_params.delete(:reset_mfa)
@user.assign_attributes(user_params.except(:reset_mfa))
active = user_params[:active] === "true"
@user.assign_attributes(user_params.except(:reset_mfa, :active))

if @user.valid?
result = UpdateUser.new(user: @user, organisation: organisation, reset_mfa: reset_mfa).call
result = UpdateUser.new(user: @user, active: active, organisation: organisation, reset_mfa: reset_mfa).call

if result.success?
flash[:notice] = t("action.user.update.success")
Expand Down
5 changes: 4 additions & 1 deletion app/services/update_user.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class UpdateUser
attr_accessor :user, :organisation, :reset_mfa

def initialize(user:, organisation:, reset_mfa: false)
def initialize(user:, organisation:, active: true, reset_mfa: false)
self.user = user
self.organisation = organisation
self.reset_mfa = reset_mfa
@active = active
end

def call
Expand All @@ -18,6 +19,8 @@ def call
user.mobile_number_confirmed_at = nil
end

user.deactivated_at = @active ? nil : DateTime.now

user.save
end

Expand Down

0 comments on commit ce32688

Please sign in to comment.