From 28eabd3144f2aa6dcb6a09a514d0f6d6b8ff8c45 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 31 May 2022 11:26:57 +0100 Subject: [PATCH] reset confirmed_at, password and sign in count when deactivated. Send reactivation email if user has previously logged in --- app/controllers/users_controller.rb | 10 ++++++++-- app/models/user.rb | 5 ++++- spec/features/user_spec.rb | 12 +++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index aead10683..2e8b05e7f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -41,8 +41,14 @@ class UsersController < ApplicationController flash[:notice] = I18n.t("devise.passwords.updated") if user_params.key?("password") redirect_to account_path else - flash[:notice] = I18n.t("devise.activation.deactivated", user_name: @user.name) if user_params[:active] == "false" - flash[:notice] = I18n.t("devise.activation.reactivated", user_name: @user.name) if user_params[:active] == "true" + case user_params[:active] + when "false" + @user.update!(confirmed_at: nil, sign_in_count: 0, encrypted_password: "") + flash[:notice] = I18n.t("devise.activation.deactivated", user_name: @user.name) + when "true" + @user.send_confirmation_instructions + flash[:notice] = I18n.t("devise.activation.reactivated", user_name: @user.name) + end redirect_to user_path(@user) end elsif user_params.key?("password") diff --git a/app/models/user.rb b/app/models/user.rb index 2ff46ea61..48f53c33c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -73,13 +73,16 @@ class User < ApplicationRecord RESET_PASSWORD_TEMPLATE_ID = "2c410c19-80a7-481c-a531-2bcb3264f8e6".freeze CONFIRMABLE_TEMPLATE_ID = "257460a6-6616-4640-a3f9-17c3d73d9e91".freeze BETA_ONBOARDING_TEMPLATE_ID = "b48bc2cd-5887-4611-8296-d0ab3ed0e7fd".freeze + USER_REACTIVATED_TEMPLATE_ID = "ac45a899-490e-4f59-ae8d-1256fc0001f9".freeze def reset_password_notify_template RESET_PASSWORD_TEMPLATE_ID end def confirmable_template - if was_migrated_from_softwire? + if last_sign_in_at.present? + USER_REACTIVATED_TEMPLATE_ID + elsif was_migrated_from_softwire? BETA_ONBOARDING_TEMPLATE_ID else CONFIRMABLE_TEMPLATE_ID diff --git a/spec/features/user_spec.rb b/spec/features/user_spec.rb index cdbfe1348..0cc8b1662 100644 --- a/spec/features/user_spec.rb +++ b/spec/features/user_spec.rb @@ -407,9 +407,18 @@ RSpec.describe "User Features" do context "when reactivating a user" do let!(:user) { FactoryBot.create(:user, :data_coordinator, last_sign_in_at: Time.zone.now) } - let!(:other_user) { FactoryBot.create(:user, name: "Other name", active: false, organisation: user.organisation) } + let!(:other_user) { FactoryBot.create(:user, name: "Other name", active: false, organisation: user.organisation, last_sign_in_at: Time.zone.now) } + let(:personalisation) do + { + name: other_user.name, + email: other_user.email, + organisation: other_user.organisation.name, + link: include("/account/confirmation?confirmation_token=#{other_user.confirmation_token}"), + } + end before do + other_user.update!(confirmation_token: "abc") visit("/logs") fill_in("user[email]", with: user.email) fill_in("user[password]", with: "pAssword1") @@ -426,6 +435,7 @@ RSpec.describe "User Features" do end it "allows to reactivate the user" do + expect(notify_client).to receive(:send_email).with(email_address: other_user.email, template_id: User::USER_REACTIVATED_TEMPLATE_ID, personalisation:).once click_button("I’m sure - reactivate this user") expect(page).to have_current_path("/users/#{other_user.id}") expect(page).to have_no_content("This user has been deactivated.")