Browse Source

Added extra notification for new country signin.

Martin Edenhofer 9 years ago
parent
commit
fd58ac0a29

+ 1 - 1
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,

+ 25 - 9
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

+ 2 - 2
app/views/mailer/user_device/de.html.erb → app/views/mailer/user_device_new/de.html.erb

@@ -2,12 +2,12 @@
 
 <p>Hallo <%= d 'user.firstname' %>,</p>
 <br>
-<p>es sieht aus, als ob Du Dich bei <%= c 'product_name' %> mit einem neuen Gerät um "<%= d 'user_device.created_at' %>" angemeldet hast:</p>
+<p>es sieht aus, als ob Du Dich mit <b>einem neuen Gerät</b> um "<%= d 'user_device.created_at' %>" angemeldet hast:</p>
 <br>
 <p>
 Dein Gerät: <%= d 'user_device.name' %><br>
 Deine Lokation (relativ): <%= d 'user_device.location' %><br>
-Deine IP: <%= d 'user_device.ip' %><br
+Deine IP: <%= d 'user_device.ip' %><br>
 </p>
 <br>
 <p>Das Gerät wurde in die Liste der bekannten Geräte hinzugefügt, diese Liste kannst Du hier einsehen:</p>

+ 2 - 2
app/views/mailer/user_device/en.html.erb → app/views/mailer/user_device_new/en.html.erb

@@ -2,12 +2,12 @@
 
 <p>Hi <%= d 'user.firstname' %>,</p>
 <br>
-<p>It looks like you signed into your <%= c 'product_name' %> account using a new device on "<%= d 'user_device.created_at' %>":</p>
+<p>It looks like you signed into your account <b>using a new device</b> on "<%= d 'user_device.created_at' %>":</p>
 <br>
 <p>
 Your device: <%= d 'user_device.name' %><br>
 Your location (relative): <%= d 'user_device.location' %><br>
-Your IP: <%= d 'user_device.ip' %><br
+Your IP: <%= d 'user_device.ip' %><br>
 </p>
 <br>
 <p>Your device has been added to your list of known devices, which you can view here:</p>

+ 19 - 0
app/views/mailer/user_device_new_location/de.html.erb

@@ -0,0 +1,19 @@
+<%= c 'product_name' %>-Anmeldung von einem anderen Land erfasst
+
+<p>Hallo <%= d 'user.firstname' %>,</p>
+<br>
+<p>es sieht aus, als ob Du Dich um "<%= d 'user_device.created_at' %>" von einem <b>bekannten Gerät aus einem anderen Land angemeldet hast</b>:</p>
+<br>
+<p>
+Dein Gerät: <%= d 'user_device.name' %><br>
+Deine Lokation (relativ): <%= d 'user_device.location' %><br>
+Deine IP: <%= d 'user_device.ip' %><br
+</p>
+<br>
+<p>Das neue Land wurde in die Liste der bekannten Geräte hinzugefügt, diese Liste kannst Du hier einsehen:</p>
+<br>
+<p><%= c 'http_type' %>://<%= c 'fqdn' %>/#profile/devices</p>
+<br>
+<p>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.</p>
+<br>
+<p>Dein <%= c 'product_name' %> Team</p>

+ 19 - 0
app/views/mailer/user_device_new_location/en.html.erb

@@ -0,0 +1,19 @@
+<%= c 'product_name' %> signin detected from a new country
+
+<p>Hi <%= d 'user.firstname' %>,</p>
+<br>
+<p>It looks like you used your account with an <b>knwon device but from a new country</b> on "<%= d 'user_device.created_at' %>":</p>
+<br>
+<p>
+Your device: <%= d 'user_device.name' %><br>
+Your location (relative): <%= d 'user_device.location' %><br>
+Your IP: <%= d 'user_device.ip' %><br
+</p>
+<br>
+<p>The country has been added to your list of known devices, which you can view here:</p>
+<br>
+<p><%= c 'http_type' %>://<%= c 'fqdn' %>/#profile/devices</p>
+<br>
+<p>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.</p>
+<br>
+<p>Your <%= c 'product_name' %> Team</p>