From fd58ac0a29cd5023d76639d404b1678ad4b0d709 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 30 Mar 2016 10:00:21 +0200 Subject: [PATCH] Added extra notification for new country signin. --- app/controllers/application_controller.rb | 2 +- app/models/user_device.rb | 34 ++++++++++++++----- .../de.html.erb | 4 +-- .../en.html.erb | 4 +-- .../user_device_new_location/de.html.erb | 19 +++++++++++ .../user_device_new_location/en.html.erb | 19 +++++++++++ 6 files changed, 68 insertions(+), 14 deletions(-) rename app/views/mailer/{user_device => user_device_new}/de.html.erb (78%) rename app/views/mailer/{user_device => user_device_new}/en.html.erb (78%) create mode 100644 app/views/mailer/user_device_new_location/de.html.erb create mode 100644 app/views/mailer/user_device_new_location/en.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ded2e306b..460211f5c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -142,7 +142,7 @@ class ApplicationController < ActionController::Base return false end - # add defice if needed + # add device if needed user_device = UserDevice.add( request.env['HTTP_USER_AGENT'], request.remote_ip, diff --git a/app/models/user_device.rb b/app/models/user_device.rb index 9363a44eb..23241682c 100644 --- a/app/models/user_device.rb +++ b/app/models/user_device.rb @@ -7,7 +7,7 @@ class UserDevice < ApplicationModel =begin -store device for user +store new device for user if device not already known user_device = UserDevice.add( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', @@ -34,13 +34,17 @@ store device for user end # find device by fingerprint + device_exists_by_fingerprint = false if fingerprint - user_device = UserDevice.find_by( + user_devices = UserDevice.where( user_id: user_id, fingerprint: fingerprint, - location: location, ) - return action(user_device.id, user_agent, ip, user_id) if user_device + user_devices.each {|local_user_device| + device_exists_by_fingerprint = true + next if local_user_device.location != location + return action(local_user_device.id, user_agent, ip, user_id) if local_user_device + } end # for basic_auth|token_auth search for user agent @@ -112,7 +116,13 @@ store device for user # send notification if needed user_devices = UserDevice.where(user_id: user_id).count if user_devices >= 2 - user_device.send_notification + + # notify on now device of if country has changed + if device_exists_by_fingerprint + user_device.notification_send('user_device_new_location') + else + user_device.notification_send('user_device_new') + end end user_device @@ -141,6 +151,11 @@ log user device action user_device.location_details = location_details location = location_details['country_name'] + + # notify if country has changed + if user_device.location != location + user_device.notification_send('user_device_new_location') + end user_device.location = location end @@ -152,19 +167,19 @@ log user device action =begin -send new user device info +send user notification about new device or new location for device user_device = UserDevice.find(id) - user_device.send_notification + user_device.notification_send('user_device_new_location') =end - def send_notification + def notification_send(template) user = User.find(user_id) NotificationFactory.notification( - template: 'user_device', + template: template, user: user, objects: { user_device: self, @@ -172,4 +187,5 @@ send new user device info } ) end + end diff --git a/app/views/mailer/user_device/de.html.erb b/app/views/mailer/user_device_new/de.html.erb similarity index 78% rename from app/views/mailer/user_device/de.html.erb rename to app/views/mailer/user_device_new/de.html.erb index 64c2d014d..f03227830 100644 --- a/app/views/mailer/user_device/de.html.erb +++ b/app/views/mailer/user_device_new/de.html.erb @@ -2,12 +2,12 @@

Hallo <%= d 'user.firstname' %>,


-

es sieht aus, als ob Du Dich bei <%= c 'product_name' %> mit einem neuen Gerät um "<%= d 'user_device.created_at' %>" angemeldet hast:

+

es sieht aus, als ob Du Dich mit einem neuen Gerät um "<%= d 'user_device.created_at' %>" angemeldet hast:


Dein Gerät: <%= d 'user_device.name' %>
Deine Lokation (relativ): <%= d 'user_device.location' %>
-Deine IP: <%= d 'user_device.ip' %>


Das Gerät wurde in die Liste der bekannten Geräte hinzugefügt, diese Liste kannst Du hier einsehen:

diff --git a/app/views/mailer/user_device/en.html.erb b/app/views/mailer/user_device_new/en.html.erb similarity index 78% rename from app/views/mailer/user_device/en.html.erb rename to app/views/mailer/user_device_new/en.html.erb index a44f42fa8..5d887cb28 100644 --- a/app/views/mailer/user_device/en.html.erb +++ b/app/views/mailer/user_device_new/en.html.erb @@ -2,12 +2,12 @@

Hi <%= d 'user.firstname' %>,


-

It looks like you signed into your <%= c 'product_name' %> account using a new device on "<%= d 'user_device.created_at' %>":

+

It looks like you signed into your account using a new device on "<%= d 'user_device.created_at' %>":


Your device: <%= d 'user_device.name' %>
Your location (relative): <%= d 'user_device.location' %>
-Your IP: <%= d 'user_device.ip' %>


Your device has been added to your list of known devices, which you can view here:

diff --git a/app/views/mailer/user_device_new_location/de.html.erb b/app/views/mailer/user_device_new_location/de.html.erb new file mode 100644 index 000000000..ffeba715c --- /dev/null +++ b/app/views/mailer/user_device_new_location/de.html.erb @@ -0,0 +1,19 @@ +<%= c 'product_name' %>-Anmeldung von einem anderen Land erfasst + +

Hallo <%= d 'user.firstname' %>,

+
+

es sieht aus, als ob Du Dich um "<%= d 'user_device.created_at' %>" von einem bekannten Gerät aus einem anderen Land angemeldet hast:

+
+

+Dein Gerät: <%= d 'user_device.name' %>
+Deine Lokation (relativ): <%= d 'user_device.location' %>
+Deine IP: <%= d 'user_device.ip' %>
+
+

Das neue Land wurde in die Liste der bekannten Geräte hinzugefügt, diese Liste kannst Du hier einsehen:

+
+

<%= c 'http_type' %>://<%= c 'fqdn' %>/#profile/devices

+
+

Wenn dies nicht Du warst, entferne die neue Lokation aus der Liste, ändere Dein Account-Passwort und kontaktieren Deinen Administrator. Jemand könnte unberechtigten Zugriff auf Dein Konto bekommen haben.

+
+

Dein <%= c 'product_name' %> Team

diff --git a/app/views/mailer/user_device_new_location/en.html.erb b/app/views/mailer/user_device_new_location/en.html.erb new file mode 100644 index 000000000..af1deac20 --- /dev/null +++ b/app/views/mailer/user_device_new_location/en.html.erb @@ -0,0 +1,19 @@ +<%= c 'product_name' %> signin detected from a new country + +

Hi <%= d 'user.firstname' %>,

+
+

It looks like you used your account with an knwon device but from a new country on "<%= d 'user_device.created_at' %>":

+
+

+Your device: <%= d 'user_device.name' %>
+Your location (relative): <%= d 'user_device.location' %>
+Your IP: <%= d 'user_device.ip' %>
+
+

The country has been added to your list of known devices, which you can view here:

+
+

<%= c 'http_type' %>://<%= c 'fqdn' %>/#profile/devices

+
+

If this wasn't you, remove the device, changing your account password, and contacting your administrator. Somebody might have gained unauthorized access to your account.

+
+

Your <%= c 'product_name' %> Team