2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2012-10-23 19:12:00 +00:00
|
|
|
module SessionHelper
|
2019-09-05 14:02:31 +00:00
|
|
|
def self.json_hash(user)
|
|
|
|
collections, assets = default_collections(user)
|
|
|
|
{
|
2021-09-22 14:57:27 +00:00
|
|
|
session: user.filter_unauthorized_attributes(user.filter_attributes(user.attributes)),
|
2019-09-05 14:02:31 +00:00
|
|
|
models: models(user),
|
|
|
|
collections: collections,
|
|
|
|
assets: assets,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.default_collections(user)
|
2012-10-23 19:12:00 +00:00
|
|
|
|
|
|
|
# auto population collections, store all here
|
|
|
|
default_collection = {}
|
2019-09-05 14:02:31 +00:00
|
|
|
assets = user.assets({})
|
2012-10-23 19:12:00 +00:00
|
|
|
|
|
|
|
# load collections to deliver from external files
|
2018-04-12 14:57:37 +00:00
|
|
|
dir = File.expand_path('..', __dir__)
|
2021-07-16 13:38:01 +00:00
|
|
|
files = Dir.glob("#{dir}/lib/session_helper/collection_*.rb")
|
2017-10-01 12:25:52 +00:00
|
|
|
files.each do |file|
|
2021-06-23 11:35:27 +00:00
|
|
|
file =~ %r{/(session_helper/collection_.*)\.rb\z}
|
|
|
|
(default_collection, assets) = $1.camelize.constantize.session(default_collection, assets, user)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2012-10-23 19:12:00 +00:00
|
|
|
|
2015-04-30 17:20:27 +00:00
|
|
|
[default_collection, assets]
|
2012-10-23 19:12:00 +00:00
|
|
|
end
|
2013-12-08 16:01:54 +00:00
|
|
|
|
2014-09-09 23:42:20 +00:00
|
|
|
def self.models(user = nil)
|
|
|
|
models = {}
|
2015-05-05 10:51:19 +00:00
|
|
|
objects = ObjectManager.list_objects
|
2017-10-01 12:25:52 +00:00
|
|
|
objects.each do |object|
|
2020-08-20 07:10:08 +00:00
|
|
|
attributes = ObjectManager::Object.new(object).attributes(user)
|
2014-09-09 23:42:20 +00:00
|
|
|
models[object] = attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-09-09 23:42:20 +00:00
|
|
|
models
|
|
|
|
end
|
|
|
|
|
2013-12-08 16:01:54 +00:00
|
|
|
def self.cleanup_expired
|
|
|
|
|
2015-06-23 12:17:04 +00:00
|
|
|
# delete temp. sessions
|
2022-01-03 09:47:32 +00:00
|
|
|
ActiveRecord::SessionStore::Session.where('persistent IS NULL AND updated_at < ?', 2.hours.ago).delete_all
|
2015-06-23 12:17:04 +00:00
|
|
|
|
2015-09-08 07:55:47 +00:00
|
|
|
# web sessions not updated the last x days
|
2022-01-03 09:47:32 +00:00
|
|
|
ActiveRecord::SessionStore::Session.where('updated_at < ?', 60.days.ago).delete_all
|
2013-12-08 16:01:54 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get(id)
|
2016-05-12 09:26:11 +00:00
|
|
|
ActiveRecord::SessionStore::Session.find_by(id: id)
|
2013-12-08 16:01:54 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 13:43:34 +00:00
|
|
|
def self.list(limit = 10_000)
|
2019-04-07 15:23:03 +00:00
|
|
|
ActiveRecord::SessionStore::Session.order(updated_at: :desc).limit(limit)
|
2013-12-08 16:01:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.destroy(id)
|
2016-05-12 09:26:11 +00:00
|
|
|
session = ActiveRecord::SessionStore::Session.find_by(id: id)
|
2013-12-08 16:01:54 +00:00
|
|
|
return if !session
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2013-12-08 16:01:54 +00:00
|
|
|
session.destroy
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|