session_helper.rb 1.7 KB

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