2018-10-09 22:21:41 +08:00
|
|
|
require_dependency 'browser_detection'
|
|
|
|
require_dependency 'discourse_ip_info'
|
|
|
|
|
2018-09-03 10:44:22 +08:00
|
|
|
module UserAuthTokensMixin
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
attributes :id,
|
|
|
|
:client_ip,
|
2018-10-09 22:21:41 +08:00
|
|
|
:location,
|
|
|
|
:browser,
|
|
|
|
:device,
|
2018-09-03 10:44:22 +08:00
|
|
|
:os,
|
|
|
|
:icon,
|
|
|
|
:created_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_ip
|
|
|
|
object.client_ip.to_s
|
|
|
|
end
|
|
|
|
|
2018-10-09 22:21:41 +08:00
|
|
|
def location
|
2018-10-31 09:38:57 +08:00
|
|
|
ipinfo = DiscourseIpInfo.get(client_ip, locale: I18n.locale)
|
2018-12-20 17:23:05 +08:00
|
|
|
ipinfo[:location].presence || I18n.t('staff_action_logs.unknown')
|
2018-09-03 10:44:22 +08:00
|
|
|
end
|
|
|
|
|
2018-10-09 22:21:41 +08:00
|
|
|
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}")
|
2018-09-03 10:44:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def icon
|
2018-10-09 22:21:41 +08:00
|
|
|
case BrowserDetection.os(object.user_agent)
|
|
|
|
when :android
|
2018-09-03 10:44:22 +08:00
|
|
|
'android'
|
2018-10-09 22:21:41 +08:00
|
|
|
when :macos, :ios
|
2018-09-03 10:44:22 +08:00
|
|
|
'apple'
|
2018-10-09 22:21:41 +08:00
|
|
|
when :linux
|
2018-09-03 10:44:22 +08:00
|
|
|
'linux'
|
2018-10-09 22:21:41 +08:00
|
|
|
when :windows
|
2018-09-03 10:44:22 +08:00
|
|
|
'windows'
|
|
|
|
else
|
|
|
|
'question'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|