Add welcome mail

This commit is contained in:
Patrick Gansterer 2020-03-06 11:46:39 +01:00
parent 75129b52c6
commit 057f3ec8ea
9 changed files with 92 additions and 1 deletions

View file

@ -41,6 +41,17 @@ class Mailer < ActionMailer::Base
subject: I18n.t('mailer.upcoming_tasks.subject')
end
# Sends a welcome email with instructions on how to reset the password.
# Assumes user.setResetPasswordToken has been successfully called already.
def welcome(user)
@user = user
@additional_text = additonal_welcome_text(user)
@link = new_password_url(id: @user.id, token: @user.reset_password_token)
mail to: user,
subject: I18n.t('mailer.welcome.subject')
end
# Sends order result for specific Ordergroup
def order_result(user, group_order)
@order = group_order.order
@ -145,6 +156,10 @@ class Mailer < ActionMailer::Base
attachments['order.csv'] = OrderCsv.new(order, options).to_csv
end
# separate method to allow plugins to mess with the text
def additonal_welcome_text(user)
end
private
def format_address(email, name)

View file

@ -20,7 +20,7 @@ class User < ApplicationRecord
has_many :created_orders, :class_name => 'Order', :foreign_key => 'created_by_user_id', :dependent => :nullify
has_many :mail_delivery_status, :class_name => 'MailDeliveryStatus', :foreign_key => 'email', :primary_key => 'email'
attr_accessor :create_ordergroup, :password, :settings_attributes
attr_accessor :create_ordergroup, :password, :send_welcome_mail, :settings_attributes
scope :deleted, -> { where.not(deleted_at: nil) }
scope :undeleted, -> { where(deleted_at: nil) }
@ -52,6 +52,13 @@ class User < ApplicationRecord
settings.defaults['notify'] = { 'upcoming_tasks' => true } unless settings.notify
end
before_save do
if send_welcome_mail?
self.reset_password_token = new_random_password(16)
self.reset_password_expires = Time.now.advance(days: 2)
end
end
after_save do
settings_attributes.each do |key, value|
value.each do |k, v|
@ -70,6 +77,12 @@ class User < ApplicationRecord
og.memberships.build({user: self})
og.save!
end
Mailer.welcome(self).deliver_now if send_welcome_mail?
end
def send_welcome_mail?
ActiveRecord::Type::Boolean.new.type_cast_from_user(send_welcome_mail)
end
# sorted by display name

View file

@ -1,5 +1,6 @@
= simple_form_for([:admin, @user]) do |f|
= render 'shared/user_form_fields', f: f, password_autocomplete: false
= f.input :send_welcome_mail, as: :boolean, label: false, inline_label: t('.send_welcome_mail')
- unless @user.ordergroup
= f.input :create_ordergroup, as: :boolean, label: false, inline_label: t('.create_ordergroup')
.form-actions

View file

@ -0,0 +1,6 @@
= raw t '.text0', user: show_user(@user)
- if @additional_text
\
= raw @additional_text
\
= raw t '.text1', link: @link, expires: I18n.l(@user.reset_password_expires), foodcoop: FoodsoftConfig[:name]

View file

@ -341,6 +341,7 @@ de:
title: Benutzer/in bearbeiten
form:
create_ordergroup: Bestellgruppe mit dem selben Namen erstellen und Benutzer_in hinzufügen.
send_welcome_mail: Eine Willkommen-Nachricht an Benutzer_in senden.
index:
first_paragraph: Hier kannst du Benutzer/innen %{url}, bearbeiten und natürlich auch löschen.
new_user: Neue/n Benutzer/in anlegen
@ -1264,6 +1265,19 @@ de:
Deine Aufgaben: %{user_tasks_url}
Viele Grüße von %{foodcoop}
welcome:
subject: Willkommen in der Foodcoop
text0: |
Liebe(r) %{user},
für dich wure ein neuer Foodsoft-Zugang erstellt.
text1: |
Um ein neues Passwort einzugeben, gehe zu: %{link}
Dieser Link kann nur einmal aufgerufen werden und läuft am %{expires} ab.
Du kannst jederzeit "Passwort vergessen?" benutzen, um einen neuen Link zu erhalten.
Viele Grüße von %{foodcoop}
messages_mailer:
foodsoft_message:

View file

@ -357,6 +357,7 @@ en:
title: Edit user
form:
create_ordergroup: Create ordergroup with the same name and add user.
send_welcome_mail: Send a welcome mail to the user.
index:
first_paragraph: Here you can %{url}, edit and delete users.
new_user: Create new user
@ -1303,6 +1304,19 @@ en:
My tasks: %{user_tasks_url}
Kind regards from %{foodcoop}.
welcome:
subject: Welcome to the Foodcoop
text0: |
Dear %{user},
a new Foodsoft account has been created for you.
text1: |
In order to choose a new password follow this link: %{link}
This link works only once and expires on %{expires}.
You can always use "Forgot password?" to get an new link.
Kind regards from %{foodcoop}.
messages_mailer:
foodsoft_message:

View file

@ -36,6 +36,10 @@ class Page < ApplicationRecord
where(permalink: "Public_frontpage").first
end
def self.welcome_mail
where(permalink: "Welcome_mail").first
end
def set_permalink
unless title.blank?
self.permalink = Page.count == 0 ? "Home" : Page.permalink(title)

View file

@ -3,6 +3,7 @@ require 'acts_as_versioned'
require 'diffy'
require 'content_for_in_controllers'
require 'foodsoft_wiki/engine'
require 'foodsoft_wiki/mailer'
require 'foodsoft_wiki/wiki_parser'
module FoodsoftWiki

View file

@ -0,0 +1,23 @@
module FoodsoftWiki
module Mailer
def self.included(base) # :nodoc:
base.class_eval do
# modify user presentation link to writing a message for the user
def additonal_welcome_text(user)
if FoodsoftWiki.enabled? && (page = Page.welcome_mail)
page.body
end
end
end
end
end
end
# modify existing helper
ActiveSupport.on_load(:after_initialize) do
Mailer.send :include, FoodsoftWiki::Mailer
end