From bb8fd896e29f280942050b44c91d20aff70feea2 Mon Sep 17 00:00:00 2001 From: Samuel Couillard <43917914+scouillard@users.noreply.github.com> Date: Tue, 21 Mar 2023 14:47:13 -0400 Subject: [PATCH] Add Reset Password Email Task (#5029) * Initial commit * Fix difference between root and base url * Add batch_size * Improve logs * Add exit if env is not set up * Rubo * Fix smtp configs * Fix logic error - set smtp_ssl_verify default to true * Add smtp configs to production.rb * Move error catcher inside of find_each and add a smtp connection checker * Remove SMTP check --- config/environments/development.rb | 6 +-- config/environments/production.rb | 6 +-- lib/tasks/reset_password_email.rake | 58 +++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 lib/tasks/reset_password_email.rake diff --git a/config/environments/development.rb b/config/environments/development.rb index 0fc3ccce..c4f401ba 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -65,9 +65,9 @@ Rails.application.configure do user_name: ENV.fetch('SMTP_USERNAME', nil), password: ENV.fetch('SMTP_PASSWORD', nil), authentication: ENV.fetch('SMTP_AUTH', nil), - enable_starttls_auto: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS_AUTO', 'true')), - enable_starttls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS', 'false')), - tls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_TLS', 'false')), + enable_starttls_auto: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS_AUTO', nil)), + enable_starttls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS', nil)), + tls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_TLS', nil)), openssl_verify_mode: ENV.fetch('SMTP_SSL_VERIFY', 'true') == 'false' ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER }.compact diff --git a/config/environments/production.rb b/config/environments/production.rb index a4ce4a6e..caa5f454 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -75,9 +75,9 @@ Rails.application.configure do user_name: ENV.fetch('SMTP_USERNAME', nil), password: ENV.fetch('SMTP_PASSWORD', nil), authentication: ENV.fetch('SMTP_AUTH', nil), - enable_starttls_auto: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS_AUTO', 'true')), - enable_starttls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS', 'false')), - tls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_TLS', 'false')), + enable_starttls_auto: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS_AUTO', nil)), + enable_starttls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS', nil)), + tls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_TLS', nil)), openssl_verify_mode: ENV.fetch('SMTP_SSL_VERIFY', 'true') == 'false' ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER }.compact diff --git a/lib/tasks/reset_password_email.rake b/lib/tasks/reset_password_email.rake new file mode 100644 index 00000000..2e1f1602 --- /dev/null +++ b/lib/tasks/reset_password_email.rake @@ -0,0 +1,58 @@ +# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/. +# +# Copyright (c) 2022 BigBlueButton Inc. and by respective authors (see below). +# +# This program is free software; you can redistribute it and/or modify it under the +# terms of the GNU Lesser General Public License as published by the Free Software +# Foundation; either version 3.0 of the License, or (at your option) any later +# version. +# +# Greenlight is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License along +# with Greenlight; if not, see <http://www.gnu.org/licenses/>. + +# frozen_string_literal: true + +require_relative 'task_helpers' + +namespace :migration do + batch_size = 500 + + desc 'Send a reset password email to users' + task :reset_password_email, %i[base_url provider] => :environment do |_task, args| + args.with_defaults(provider: 'greenlight') + + root_url = "#{args[:base_url]}/" + + if ENV['SMTP_SERVER'].blank? + err 'SMTP Server not set. Skipping sending reset password emails.' + exit 0 + end + + info 'Sending reset password emails...' + User.where(external_id: nil, provider: args[:provider]) + .find_each(batch_size:) do |user| + token = user.generate_reset_token! + + UserMailer.with(user:, + reset_url: reset_password_url(root_url, token), + base_url: args[:base_url], + provider: args[:provider]).reset_password_email.deliver_now + + success 'Successfully sent reset password email to:' + info " name: #{user.name}" + info " email: #{user.email}" + rescue StandardError => e + err "Unable to send reset password email to:\n name: #{user.name} \n email: #{user.email} \n error: #{e}" + end + end + + private + + def reset_password_url(root_url, token) + "#{root_url}reset_password/#{token}" + end +end -- GitLab