trabajo-afectivo/app/models/user_device.rb

70 lines
1.5 KiB
Ruby
Raw Normal View History

2015-08-17 13:25:41 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class UserDevice < ApplicationModel
store :device_details
store :location_details
validates :name, presence: true
=begin
store device for user
user_device = UserDevice.add(
2015-08-17 13:25:41 +00:00
'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',
'172.0.0.1',
user.id,
)
=end
def self.add(user_agent, ip, user_id)
# get browser details
browser = Browser.new(:ua => user_agent, :accept_language => 'en-us')
browser = {
plattform: browser.platform.to_s.camelize,
name: browser.name,
version: browser.version,
full_version: browser.full_version,
}
# generate device name
name = browser[:plattform] || ''
if browser[:name]
if name
name += ', '
end
name += browser[:name]
end
# get location info
location = Service::GeoIp.location(ip)
country = location['country_name']
# check if exists
exists = self.find_by(
:user_id => user_id,
os: browser[:plattform],
browser: browser[:name],
2015-08-17 15:16:17 +00:00
location: country,
2015-08-17 13:25:41 +00:00
)
if exists
exists.touch
return exists
end
# create new device
self.create(
user_id: user_id,
name: name,
os: browser[:plattform],
browser: browser[:name],
2015-08-17 15:16:17 +00:00
location: country,
2015-08-17 13:25:41 +00:00
device_details: browser,
location_details: location,
)
end
end