1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-24 15:49:39 +02:00

Allow a self-hosted user to configure their SMTP settings directly from within the UI (#682)

* Add setting fields to model

* Allow to configure SMTP settings

* Normalize locales

* Cleanup locales

* Remove 'coming soon'

* fix test

* Reset credentials

* Reset development config

* Check smtp spelling

* Use post instead of get method

* TLS ENV variable is more descriptive

* Rework application mailer

* Follow rails convention for mailer action params

* Reset schema.rb to main

* Test WIP

* Add test for controller and mailer

* Move tests from controller to model

* Custom error message if settings are not all present

* Comment smtp config in development env

* Add default tls enabled value

* Rubocop

* Fix controller test

* Reset credentials

* Normalize locales

* Test

* fix test

* Fix application mailer test that fails randomly

* Error flash message instead of notice

* Rework application mailer tests
This commit is contained in:
Thibaut Gorioux 2024-04-29 22:44:24 +02:00 committed by GitHub
parent f0480e7ab7
commit 6fdb8e8d69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 200 additions and 15 deletions

View file

@ -17,6 +17,24 @@ class Settings::HostingsController < ApplicationController
end
end
def send_test_email
unless Setting.smtp_settings_populated?
flash[:error] = t(".missing_smtp_setting_error")
render(:show, status: :unprocessable_entity)
return
end
begin
NotificationMailer.with(user: Current.user).test_email.deliver_now
rescue => _e
flash[:error] = t(".error")
render :show, status: :unprocessable_entity
return
end
redirect_to settings_hosting_path, notice: t(".success")
end
private
def all_updates_valid?
@errors = ActiveModel::Errors.new(Setting)
@ -37,12 +55,13 @@ class Settings::HostingsController < ApplicationController
end
def hosting_params
permitted_params = params.require(:setting).permit(:render_deploy_hook, :upgrades_mode)
permitted_params = params.require(:setting).permit(:render_deploy_hook, :upgrades_mode, :email_sender, :app_domain, :smtp_host, :smtp_port, :smtp_username, :smtp_password)
result = {}
result[:upgrades_mode] = permitted_params[:upgrades_mode] == "manual" ? "manual" : "auto" if permitted_params.key?(:upgrades_mode)
result[:render_deploy_hook] = permitted_params[:render_deploy_hook] if permitted_params.key?(:render_deploy_hook)
result[:upgrades_target] = permitted_params[:upgrades_mode] unless permitted_params[:upgrades_mode] == "manual" if permitted_params.key?(:upgrades_mode)
result.merge!(permitted_params.slice(:email_sender, :app_domain, :smtp_host, :smtp_port, :smtp_username, :smtp_password))
result
end

View file

@ -1,4 +1,16 @@
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout "mailer"
after_action :set_self_host_settings, if: -> { ENV["SELF_HOSTING_ENABLED"] == "true" }
private
def set_self_host_settings
mail.from = Setting.email_sender
mail.delivery_method.settings.merge!({ address: Setting.smtp_host,
port: Setting.smtp_port,
user_name: Setting.smtp_username,
password: Setting.smtp_password,
tls: ENV.fetch("SMTP_TLS_ENABLED", "true") == "true" })
end
end

View file

@ -0,0 +1,5 @@
class NotificationMailer < ApplicationMailer
def test_email
mail(to: params[:user].email, subject: t(".test_email_subject"), body: t(".test_email_body"))
end
end

View file

@ -16,4 +16,18 @@ class Setting < RailsSettings::Base
type: :string,
default: ENV.fetch("UPGRADES_TARGET", "release"),
validates: { inclusion: { in: %w[release commit] } }
field :app_domain, type: :string, default: ENV["APP_DOMAIN"]
field :email_sender, type: :string, default: ENV["EMAIL_SENDER"]
scope :smtp_settings do
field :smtp_host, type: :string, read_only: true, default: ENV["SMTP_ADDRESS"]
field :smtp_port, type: :string, read_only: true, default: ENV["SMTP_PORT"]
field :smtp_username, type: :string, read_only: true, default: ENV["SMTP_USERNAME"]
field :smtp_password, type: :string, read_only: true, default: ENV["SMTP_PASSWORD"]
end
def self.smtp_settings_populated?
Setting.defined_fields.select { |f| f.scope == :smtp_settings }.map(&:read).all?(&:present?)
end
end

View file

@ -51,11 +51,12 @@
<p class="text-gray-500 text-sm mb-4"><%= t(".smtp_settings.description") %></p>
<div class="space-y-4">
<div class="space-y-3">
<%= form.text_field :smtp_domain, disabled: true, label: t(".smtp_settings.domain"), placeholder: t(".smtp_settings.domain_placeholder") %>
<%= form.text_field :smtp_host, disabled: true, label: t(".smtp_settings.host"), placeholder: t(".smtp_settings.host_placeholder") %>
<%= form.number_field :smtp_port, disabled: true, label: t(".smtp_settings.port"), placeholder: t(".smtp_settings.port_placeholder") %>
<%= form.text_field :smtp_username, disabled: true, label: t(".smtp_settings.username"), placeholder: t(".smtp_settings.username_placeholder") %>
<%= form.password_field :smtp_password, disabled: true, label: t(".smtp_settings.password"), placeholder: t(".smtp_settings.password_placeholder") %>
<%= form.text_field :email_sender, label: t(".email_sender"), placeholder: t(".email_sender_placeholder"), value: Setting.email_sender, data: { "auto-submit-form-target" => "auto" } %>
<%= form.text_field :app_domain, label: t(".domain"), placeholder: t(".domain_placeholder"), value: Setting.app_domain, data: { "auto-submit-form-target" => "auto" } %>
<%= form.text_field :smtp_host, label: t(".smtp_settings.host"), placeholder: t(".smtp_settings.host_placeholder"), value: Setting.smtp_host, data: { "auto-submit-form-target" => "auto" } %>
<%= form.number_field :smtp_port, label: t(".smtp_settings.port"), placeholder: t(".smtp_settings.port_placeholder"), value: Setting.smtp_port, data: { "auto-submit-form-target" => "auto" } %>
<%= form.text_field :smtp_username, label: t(".smtp_settings.username"), placeholder: t(".smtp_settings.username_placeholder"), value: Setting.smtp_username, data: { "auto-submit-form-target" => "auto" } %>
<%= form.password_field :smtp_password, label: t(".smtp_settings.password"), placeholder: t(".smtp_settings.password_placeholder"), value: Setting.smtp_password, data: { "auto-submit-form-target" => "auto" } %>
</div>
<div class="flex items-center justify-between bg-white border border-alpha-black-100 p-4 rounded-lg">
<div class="flex items-center gap-3">
@ -68,7 +69,7 @@
</div>
</div>
<div>
<button type="button" class="bg-gray-50 text-gray-900 text-sm font-medium rounded-lg px-3 py-2 cursor-not-allowed" disabled><%= t(".smtp_settings.send_test_email_button") %></button>
<%= link_to t(".smtp_settings.send_test_email_button"), send_test_email_settings_hosting_path, data: { turbo_method: :post }, class:"bg-gray-50 text-gray-900 text-sm font-medium rounded-lg px-3 py-2" %>
</div>
</div>
</div>