session_helper.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. module SessionHelper
  3. def self.json_hash(user)
  4. collections, assets = default_collections(user)
  5. {
  6. session: user.filter_unauthorized_attributes(user.filter_attributes(user.attributes)),
  7. models: models(user),
  8. collections: collections,
  9. assets: assets,
  10. }
  11. end
  12. def self.default_collections(user)
  13. # auto population collections, store all here
  14. default_collection = {}
  15. assets = user.assets({})
  16. # load collections to deliver from external files
  17. dir = File.expand_path('..', __dir__)
  18. files = Dir.glob("#{dir}/lib/session_helper/collection_*.rb")
  19. files.each do |file|
  20. file =~ %r{/(session_helper/collection_.*)\.rb\z}
  21. (default_collection, assets) = $1.camelize.constantize.session(default_collection, assets, user)
  22. end
  23. [default_collection, assets]
  24. end
  25. def self.models(user = nil)
  26. models = {}
  27. objects = ObjectManager.list_objects
  28. objects.each do |object|
  29. attributes = ObjectManager::Object.new(object).attributes(user)
  30. models[object] = attributes
  31. end
  32. models
  33. end
  34. def self.cleanup_expired
  35. # delete temp. sessions
  36. ActiveRecord::SessionStore::Session.where('persistent IS NULL AND updated_at < ?', 2.hours.ago).delete_all
  37. # web sessions not updated the last x days
  38. ActiveRecord::SessionStore::Session.where('updated_at < ?', 60.days.ago).delete_all
  39. end
  40. def self.get(id)
  41. ActiveRecord::SessionStore::Session.find_by(id: id)
  42. end
  43. def self.list(limit = 10_000)
  44. ActiveRecord::SessionStore::Session.order(updated_at: :desc).limit(limit)
  45. end
  46. def self.destroy(id)
  47. session = ActiveRecord::SessionStore::Session.find_by(id: id)
  48. return if !session
  49. session.destroy
  50. end
  51. end