2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2015-08-17 14:12:40 +00:00
|
|
|
|
|
|
|
class UserDevicesController < ApplicationController
|
2020-03-19 09:39:51 +00:00
|
|
|
prepend_before_action { authentication_check && authorize! }
|
2015-08-17 14:12:40 +00:00
|
|
|
|
|
|
|
def index
|
2019-04-07 15:23:03 +00:00
|
|
|
devices = UserDevice.where(user_id: current_user.id).order(updated_at: :desc, name: :asc)
|
2015-08-17 14:12:40 +00:00
|
|
|
devices_full = []
|
2017-10-01 12:25:52 +00:00
|
|
|
devices.each do |device|
|
2015-08-17 14:12:40 +00:00
|
|
|
attributes = device.attributes
|
2017-11-23 08:09:44 +00:00
|
|
|
if device.location_details['city_name'].present?
|
2015-08-18 22:36:58 +00:00
|
|
|
attributes['location'] += ", #{device.location_details['city_name']}"
|
2015-08-17 14:12:40 +00:00
|
|
|
end
|
|
|
|
attributes.delete('created_at')
|
|
|
|
attributes.delete('device_details')
|
|
|
|
attributes.delete('location_details')
|
2016-08-18 05:26:07 +00:00
|
|
|
attributes.delete('fingerprint')
|
2015-08-17 16:14:44 +00:00
|
|
|
|
2016-08-18 07:45:26 +00:00
|
|
|
# mark current device to prevent killing own session via user preferences device management
|
2022-01-03 09:47:32 +00:00
|
|
|
if session[:user_device_fingerprint] == device.fingerprint && device.updated_at > 30.minutes.ago
|
2015-08-17 16:14:44 +00:00
|
|
|
attributes['current'] = true
|
|
|
|
end
|
2015-08-17 14:12:40 +00:00
|
|
|
devices_full.push attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-17 14:12:40 +00:00
|
|
|
model_index_render_result(devices_full)
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2015-08-18 22:36:58 +00:00
|
|
|
|
2015-08-17 16:14:44 +00:00
|
|
|
# find device
|
|
|
|
user_device = UserDevice.find_by(user_id: current_user.id, id: params[:id])
|
|
|
|
|
|
|
|
# delete device and session's
|
|
|
|
if user_device
|
2017-10-01 12:25:52 +00:00
|
|
|
SessionHelper.list.each do |session|
|
2015-08-17 16:14:44 +00:00
|
|
|
next if !session.data['user_id']
|
2015-08-18 22:36:58 +00:00
|
|
|
next if !session.data['user_device_id']
|
|
|
|
next if session.data['user_device_id'] != user_device.id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-07-16 13:38:01 +00:00
|
|
|
SessionHelper.destroy(session.id)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-17 16:14:44 +00:00
|
|
|
user_device.destroy
|
|
|
|
end
|
2015-08-17 14:12:40 +00:00
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|