user_devices_controller.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class UserDevicesController < ApplicationController
  3. prepend_before_action { authentication_check && authorize! }
  4. def index
  5. devices = UserDevice.where(user_id: current_user.id).order(updated_at: :desc, name: :asc)
  6. devices_full = []
  7. devices.each do |device|
  8. attributes = device.attributes
  9. if device.location_details['city_name'].present?
  10. attributes['location'] += ", #{device.location_details['city_name']}"
  11. end
  12. attributes.delete('created_at')
  13. attributes.delete('device_details')
  14. attributes.delete('location_details')
  15. attributes.delete('fingerprint')
  16. # mark current device to prevent killing own session via user preferences device management
  17. if session[:user_device_fingerprint] == device.fingerprint && device.updated_at > 30.minutes.ago
  18. attributes['current'] = true
  19. end
  20. devices_full.push attributes
  21. end
  22. model_index_render_result(devices_full)
  23. end
  24. def destroy
  25. # find device
  26. user_device = UserDevice.find_by(user_id: current_user.id, id: params[:id])
  27. # delete device and session's
  28. if user_device
  29. SessionHelper.list.each do |session|
  30. next if !session.data['user_id']
  31. next if !session.data['user_device_id']
  32. next if session.data['user_device_id'] != user_device.id
  33. SessionHelper.destroy(session.id)
  34. end
  35. user_device.destroy
  36. end
  37. render json: {}, status: :ok
  38. end
  39. end