user_devices_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class UserDevicesController < ApplicationController
  3. prepend_before_action { authentication_check(permission: 'user_preferences.device') }
  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'] && !device.location_details['city_name'].empty?
  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 > Time.zone.now - 30.minutes
  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