diff --git a/app/helpers/mail_helper.rb b/app/helpers/mail_helper.rb new file mode 100644 index 0000000..7a4bb51 --- /dev/null +++ b/app/helpers/mail_helper.rb @@ -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 diff --git a/app/interactors/log_user_session.rb b/app/interactors/log_user_session.rb index 428f58c..3dfc39b 100644 --- a/app/interactors/log_user_session.rb +++ b/app/interactors/log_user_session.rb @@ -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 diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 9ea728f..815e3f1 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -4,4 +4,6 @@ class ApplicationMailer < ActionMailer::Base layout 'mailer' default from: Rails.application.settings(:identity)[:noreply_email_contact] + + helper :mail end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb new file mode 100644 index 0000000..33e5b97 --- /dev/null +++ b/app/mailers/notification_mailer.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class NotificationMailer < ApplicationMailer + def signed_in(email, session) + @session = session + + mail to: email + end +end diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb index cbd34d2..f6a71ce 100644 --- a/app/views/layouts/mailer.html.erb +++ b/app/views/layouts/mailer.html.erb @@ -1,7 +1,8 @@ - + - + + diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb deleted file mode 100644 index 37f0bdd..0000000 --- a/app/views/layouts/mailer.text.erb +++ /dev/null @@ -1 +0,0 @@ -<%= yield %> diff --git a/app/views/notification_mailer/signed_in.html.erb b/app/views/notification_mailer/signed_in.html.erb new file mode 100644 index 0000000..7388b32 --- /dev/null +++ b/app/views/notification_mailer/signed_in.html.erb @@ -0,0 +1,16 @@ +

<%= greeting_text @session.account.public_name %>

+ +

<%= translate '.summary' %>:

+ +
+
<%= Session.human_attribute_name :logged_at %>:
+
<%= localize @session.logged_at, format: :long %>
+ +
<%= Session.human_attribute_name :ip_address %>:
+
<%= @session.ip_address %>
+
+ +

+ <%= translate '.instructions' %>: + <%= link_to edit_user_registration_url, edit_user_registration_url %> +

diff --git a/config/locales/lazy/en.yml b/config/locales/lazy/en.yml index 8738ba5..03e56cf 100644 --- a/config/locales/lazy/en.yml +++ b/config/locales/lazy/en.yml @@ -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: diff --git a/config/locales/lazy/ru.yml b/config/locales/lazy/ru.yml index fd1b891..0b44b79 100644 --- a/config/locales/lazy/ru.yml +++ b/config/locales/lazy/ru.yml @@ -3,10 +3,20 @@ ru: show: join: Вступить tracking: Ваше заявление - description: > + description: >- Мы выступаем за свободный рынок и государственное невмешательство в экономику, за принцип самопринадлежности и самоценности человеческой личности. + notification_mailer: + signed_in: + subject: '[LPR Alert]: Произведён вход в ваш аккаунт' + summary: >- + Этот адрес электронной почты был использован для доступа + к Либертарианской Партии России + instructions: >- + Если это были вы, можете проигнорировать это предупреждение. + Если вы заметили подозрительную активность вашего аккаунта, пожалуйста + измените пароль и включите двухфакторную аутентификацию settings: people: show: diff --git a/config/locales/other/en.yml b/config/locales/other/en.yml index dfb8725..fc053bd 100644 --- a/config/locales/other/en.yml +++ b/config/locales/other/en.yml @@ -10,3 +10,4 @@ en: create: Create destroy_confirmation: 'Do you really want to destroy %{value}?' save: Save + hello: Hello diff --git a/config/locales/other/ru.yml b/config/locales/other/ru.yml index 0cb7810..567b320 100644 --- a/config/locales/other/ru.yml +++ b/config/locales/other/ru.yml @@ -10,3 +10,4 @@ ru: create: Создать destroy_confirmation: 'Вы действительно хотите удалить %{value}?' save: Сохранить + hello: Здравствуйте diff --git a/spec/helpers/mail_helper_spec.rb b/spec/helpers/mail_helper_spec.rb new file mode 100644 index 0000000..ef305cf --- /dev/null +++ b/spec/helpers/mail_helper_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe MailHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/mailers/notification_spec.rb b/spec/mailers/notification_spec.rb new file mode 100644 index 0000000..74418ed --- /dev/null +++ b/spec/mailers/notification_spec.rb @@ -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 diff --git a/spec/mailers/previews/notification_preview.rb b/spec/mailers/previews/notification_preview.rb new file mode 100644 index 0000000..009e570 --- /dev/null +++ b/spec/mailers/previews/notification_preview.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class NotificationPreview < ActionMailer::Preview +end