Add action NotificationMailer#signed_in
This commit is contained in:
parent
dcf2f3b00c
commit
901d8c44da
14 changed files with 129 additions and 6 deletions
12
app/helpers/mail_helper.rb
Normal file
12
app/helpers/mail_helper.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module MailHelper
|
||||
def greeting_text(name)
|
||||
capture do
|
||||
concat translate :hello
|
||||
concat ', '
|
||||
concat name
|
||||
concat '!'
|
||||
end
|
||||
end
|
||||
end
|
|
@ -4,10 +4,30 @@ class LogUserSession
|
|||
include Interactor
|
||||
|
||||
def call
|
||||
Session.create!(
|
||||
create_session
|
||||
send_alerts
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def contacts
|
||||
@contacts ||= context.user.account.contact_list.contacts
|
||||
.where(send_security_notifications: true)
|
||||
end
|
||||
|
||||
def create_session
|
||||
@session = Session.create!(
|
||||
account: context.user.account,
|
||||
logged_at: context.user.current_sign_in_at,
|
||||
ip_address: context.user.current_sign_in_ip,
|
||||
)
|
||||
end
|
||||
|
||||
def send_alerts
|
||||
contacts.each do |contact|
|
||||
NotificationMailer.signed_in(contact.value, @session).deliver_now
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,4 +4,6 @@ class ApplicationMailer < ActionMailer::Base
|
|||
layout 'mailer'
|
||||
|
||||
default from: Rails.application.settings(:identity)[:noreply_email_contact]
|
||||
|
||||
helper :mail
|
||||
end
|
||||
|
|
9
app/mailers/notification_mailer.rb
Normal file
9
app/mailers/notification_mailer.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class NotificationMailer < ApplicationMailer
|
||||
def signed_in(email, session)
|
||||
@session = session
|
||||
|
||||
mail to: email
|
||||
end
|
||||
end
|
|
@ -1,7 +1,8 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<html lang="<%= I18n.locale %>">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||
|
||||
<style>
|
||||
/* Email styles need to be inline */
|
||||
</style>
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
<%= yield %>
|
16
app/views/notification_mailer/signed_in.html.erb
Normal file
16
app/views/notification_mailer/signed_in.html.erb
Normal file
|
@ -0,0 +1,16 @@
|
|||
<p><%= greeting_text @session.account.public_name %></p>
|
||||
|
||||
<p><%= translate '.summary' %>:</p>
|
||||
|
||||
<dl>
|
||||
<dt><strong><%= Session.human_attribute_name :logged_at %>:</strong></dt>
|
||||
<dd><%= localize @session.logged_at, format: :long %></dd>
|
||||
|
||||
<dt><strong><%= Session.human_attribute_name :ip_address %>:</strong></dt>
|
||||
<dd><%= @session.ip_address %></dd>
|
||||
</dl>
|
||||
|
||||
<p>
|
||||
<%= translate '.instructions' %>:
|
||||
<%= link_to edit_user_registration_url, edit_user_registration_url %>
|
||||
</p>
|
|
@ -3,10 +3,19 @@ en:
|
|||
show:
|
||||
join: Join
|
||||
tracking: Your application
|
||||
description: >
|
||||
description: >-
|
||||
We stand for a free market and freedom of government intervention
|
||||
in the economy, for the principle of self-ownership and self-value
|
||||
of the human person.
|
||||
notification_mailer:
|
||||
signed_in:
|
||||
subject: '[LPR Alert]: New sign in to your account'
|
||||
summary: >-
|
||||
This email address was used to access the Libertarian Party of Russia
|
||||
instructions: >-
|
||||
If this was you, you can ignore this alert. If you suspect
|
||||
any suspicious activity on your account, please change your password
|
||||
and enable two-factor authentication
|
||||
staffs:
|
||||
people:
|
||||
show:
|
||||
|
|
|
@ -3,10 +3,20 @@ ru:
|
|||
show:
|
||||
join: Вступить
|
||||
tracking: Ваше заявление
|
||||
description: >
|
||||
description: >-
|
||||
Мы выступаем за свободный рынок и государственное невмешательство
|
||||
в экономику, за принцип самопринадлежности и самоценности человеческой
|
||||
личности.
|
||||
notification_mailer:
|
||||
signed_in:
|
||||
subject: '[LPR Alert]: Произведён вход в ваш аккаунт'
|
||||
summary: >-
|
||||
Этот адрес электронной почты был использован для доступа
|
||||
к Либертарианской Партии России
|
||||
instructions: >-
|
||||
Если это были вы, можете проигнорировать это предупреждение.
|
||||
Если вы заметили подозрительную активность вашего аккаунта, пожалуйста
|
||||
измените пароль и включите двухфакторную аутентификацию
|
||||
settings:
|
||||
people:
|
||||
show:
|
||||
|
|
|
@ -10,3 +10,4 @@ en:
|
|||
create: Create
|
||||
destroy_confirmation: 'Do you really want to destroy %{value}?'
|
||||
save: Save
|
||||
hello: Hello
|
||||
|
|
|
@ -10,3 +10,4 @@ ru:
|
|||
create: Создать
|
||||
destroy_confirmation: 'Вы действительно хотите удалить %{value}?'
|
||||
save: Сохранить
|
||||
hello: Здравствуйте
|
||||
|
|
7
spec/helpers/mail_helper_spec.rb
Normal file
7
spec/helpers/mail_helper_spec.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe MailHelper do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
32
spec/mailers/notification_spec.rb
Normal file
32
spec/mailers/notification_spec.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe NotificationMailer do
|
||||
describe '#signed_in' do
|
||||
let(:mail) { NotificationMailer.signed_in email, session }
|
||||
|
||||
let(:email) { Faker::Internet.email }
|
||||
let(:session) { create :some_session }
|
||||
|
||||
it 'renders the headers' do
|
||||
expect(mail.subject).to eq '[LPR Alert]: Произведён вход в ваш аккаунт'
|
||||
expect(mail.from).to eq ['no-reply@libertarian-party.com']
|
||||
expect(mail.to).to eq [email]
|
||||
end
|
||||
|
||||
it 'includes greeting' do
|
||||
expect(mail.body.encoded).to \
|
||||
match "Здравствуйте, #{session.account.public_name}"
|
||||
end
|
||||
|
||||
it 'includes datetime' do
|
||||
expect(mail.body.encoded).to \
|
||||
match I18n.localize session.logged_at, format: :long
|
||||
end
|
||||
|
||||
it 'includes IP address' do
|
||||
expect(mail.body.encoded).to match session.ip_address
|
||||
end
|
||||
end
|
||||
end
|
4
spec/mailers/previews/notification_preview.rb
Normal file
4
spec/mailers/previews/notification_preview.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class NotificationPreview < ActionMailer::Preview
|
||||
end
|
Reference in a new issue