From 72846b5c9908629a3b196d71571bef90b999f246 Mon Sep 17 00:00:00 2001 From: Joe Haig Date: Fri, 6 Sep 2024 15:57:00 +0100 Subject: [PATCH] Split PasswordHelpers The PasswordHelpers mixin is split into to parts: * PasswordHelpers contains helpers for updating user password * UserAdminHelpers, inheriting from PasswordHelpers, also includes helpers for creating new users Rails 7.1 by default raises exceptions when attempting to create callback for actions that do not exist. This caused exceptions with SuperAdminController, which does not have the `create` action for creating new users. --- .../admin/case_workers_controller.rb | 2 +- app/controllers/concerns/password_helpers.rb | 28 +---------- .../concerns/user_admin_helpers.rb | 47 +++++++++++++++++++ .../admin/external_users_controller.rb | 2 +- .../external_users_controller.rb | 2 +- 5 files changed, 51 insertions(+), 30 deletions(-) create mode 100644 app/controllers/concerns/user_admin_helpers.rb diff --git a/app/controllers/case_workers/admin/case_workers_controller.rb b/app/controllers/case_workers/admin/case_workers_controller.rb index 42d739c450..460289aba9 100644 --- a/app/controllers/case_workers/admin/case_workers_controller.rb +++ b/app/controllers/case_workers/admin/case_workers_controller.rb @@ -1,7 +1,7 @@ module CaseWorkers module Admin class CaseWorkersController < CaseWorkers::Admin::ApplicationController - include PasswordHelpers + include UserAdminHelpers before_action :set_case_worker, only: %i[show edit update destroy change_password update_password] diff --git a/app/controllers/concerns/password_helpers.rb b/app/controllers/concerns/password_helpers.rb index 733c81ce14..33f2817b92 100644 --- a/app/controllers/concerns/password_helpers.rb +++ b/app/controllers/concerns/password_helpers.rb @@ -2,8 +2,7 @@ module PasswordHelpers extend ActiveSupport::Concern included do - before_action :set_resource_params, only: %i[create update_password] - before_action :set_temporary_password, only: :create + before_action :set_resource_params, only: :update_password before_action :set_user_params, only: :update_password end @@ -18,29 +17,8 @@ def update_password end end - # devise mail backgrounding achieved via User#send_devise_notification - def deliver_reset_password_instructions(user) - token, enc = Devise.token_generator.generate(user.class, :reset_password_token) - user.reset_password_token = enc - user.reset_password_sent_at = Time.now.utc - user.save(validate: false) - DeviseMailer.reset_password_instructions(user, token, current_user.name).deliver_later - rescue StandardError => e - Rails.logger.error("DEVISE MAILER ERROR: '#{e.message}' while sending reset password mail") - end - private - def user_for_controller_action - instance_variable_get(:"@#{controller_name.singularize}").user - end - - def params_with_temporary_password - @resource_params['user_attributes']['password'] = @temporary_password - @resource_params['user_attributes']['password_confirmation'] = @temporary_password - @resource_params - end - def password_params %i[email first_name last_name].each { |attribute| @user_params[:user_attributes].delete(attribute) } @user_params @@ -54,8 +32,4 @@ def set_resource_params def set_user_params @user_params = @resource_params.slice(:user_attributes) end - - def set_temporary_password - @temporary_password = SecureRandom.uuid - end end diff --git a/app/controllers/concerns/user_admin_helpers.rb b/app/controllers/concerns/user_admin_helpers.rb new file mode 100644 index 0000000000..e1a010336d --- /dev/null +++ b/app/controllers/concerns/user_admin_helpers.rb @@ -0,0 +1,47 @@ +module UserAdminHelpers < PasswordHelpers + included do + before_action :set_resource_params, only: :create + before_action :set_temporary_password, only: :create + end + + # devise mail backgrounding achieved via User#send_devise_notification + def deliver_reset_password_instructions(user) + token, enc = Devise.token_generator.generate(user.class, :reset_password_token) + user.reset_password_token = enc + user.reset_password_sent_at = Time.now.utc + user.save(validate: false) + DeviseMailer.reset_password_instructions(user, token, current_user.name).deliver_later + rescue StandardError => e + Rails.logger.error("DEVISE MAILER ERROR: '#{e.message}' while sending reset password mail") + end + + private + + def user_for_controller_action + instance_variable_get(:"@#{controller_name.singularize}").user + end + + def params_with_temporary_password + @resource_params['user_attributes']['password'] = @temporary_password + @resource_params['user_attributes']['password_confirmation'] = @temporary_password + @resource_params + end + + def password_params + %i[email first_name last_name].each { |attribute| @user_params[:user_attributes].delete(attribute) } + @user_params + end + + def set_resource_params + resource = controller_name.singularize + @resource_params = send((resource + '_params').to_sym) + end + + def set_user_params + @user_params = @resource_params.slice(:user_attributes) + end + + def set_temporary_password + @temporary_password = SecureRandom.uuid + end +end diff --git a/app/controllers/external_users/admin/external_users_controller.rb b/app/controllers/external_users/admin/external_users_controller.rb index f9e5476b99..62f0b42728 100644 --- a/app/controllers/external_users/admin/external_users_controller.rb +++ b/app/controllers/external_users/admin/external_users_controller.rb @@ -1,7 +1,7 @@ module ExternalUsers module Admin class ExternalUsersController < ExternalUsers::Admin::ApplicationController - include PasswordHelpers + include UserAdminHelpers ATTRIBUTES = %i[id email email_confirmation password password_confirmation current_password first_name last_name email_notification_of_message].freeze diff --git a/app/controllers/provider_management/external_users_controller.rb b/app/controllers/provider_management/external_users_controller.rb index d9f60406e0..d4af60a0e6 100644 --- a/app/controllers/provider_management/external_users_controller.rb +++ b/app/controllers/provider_management/external_users_controller.rb @@ -1,6 +1,6 @@ module ProviderManagement class ExternalUsersController < ApplicationController - include PasswordHelpers + include UserAdminHelpers before_action :set_provider, except: %i[find search] before_action :set_external_user, only: %i[show edit update change_password update_password