Skip to content

Commit

Permalink
Split PasswordHelpers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrmhaig committed Sep 6, 2024
1 parent fcc615d commit 72846b5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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]

Expand Down
28 changes: 1 addition & 27 deletions app/controllers/concerns/password_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
47 changes: 47 additions & 0 deletions app/controllers/concerns/user_admin_helpers.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 72846b5

Please sign in to comment.