discourse/app/serializers/concerns/user_auth_tokens_mixin.rb
Bianca Nenciu 1d26a473e7 FEATURE: Show "Recently used devices" in user preferences (#6335)
* FEATURE: Added MaxMindDb to resolve IP information.

* FEATURE: Added browser detection based on user agent.

* FEATURE: Added recently used devices in user preferences.

* DEV: Added acceptance test for recently used devices.

* UX: Do not show 'Show more' button if there aren't more tokens.

* DEV: Fix unit tests.

* DEV: Make changes after code review.

* Add more detailed unit tests.

* Improve logging messages.

* Minor coding style fixes.

* DEV: Use DropdownSelectBoxComponent and run Prettier.

* DEV: Fix unit tests.
2018-10-09 22:21:41 +08:00

61 lines
1.2 KiB
Ruby

require_dependency 'browser_detection'
require_dependency 'discourse_ip_info'
module UserAuthTokensMixin
extend ActiveSupport::Concern
included do
attributes :id,
:client_ip,
:location,
:browser,
:device,
:os,
:icon,
:created_at
end
def client_ip
object.client_ip.to_s
end
def location
ipinfo = DiscourseIpInfo.get(client_ip)
location = [ipinfo[:city], ipinfo[:region], ipinfo[:country]].reject { |x| x.blank? }.join(", ")
return I18n.t('staff_action_logs.unknown') if location.blank?
location
end
def browser
val = BrowserDetection.browser(object.user_agent)
I18n.t("user_auth_tokens.browser.#{val}")
end
def device
val = BrowserDetection.device(object.user_agent)
I18n.t("user_auth_tokens.device.#{val}")
end
def os
val = BrowserDetection.os(object.user_agent)
I18n.t("user_auth_tokens.os.#{val}")
end
def icon
case BrowserDetection.os(object.user_agent)
when :android
'android'
when :macos, :ios
'apple'
when :linux
'linux'
when :windows
'windows'
else
'question'
end
end
end