trabajo-afectivo/lib/session_helper.rb

66 lines
1.8 KiB
Ruby
Raw Permalink Normal View History

# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
module SessionHelper
2019-09-05 14:02:31 +00:00
def self.json_hash(user)
collections, assets = default_collections(user)
{
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)
# auto population collections, store all here
default_collection = {}
2019-09-05 14:02:31 +00:00
assets = user.assets({})
# load collections to deliver from external files
2018-04-12 14:57:37 +00:00
dir = File.expand_path('..', __dir__)
files = Dir.glob("#{dir}/lib/session_helper/collection_*.rb")
files.each do |file|
file =~ %r{/(session_helper/collection_.*)\.rb\z}
(default_collection, assets) = $1.camelize.constantize.session(default_collection, assets, user)
end
[default_collection, assets]
end
2013-12-08 16:01:54 +00:00
2014-09-09 23:42:20 +00:00
def self.models(user = nil)
models = {}
objects = ObjectManager.list_objects
objects.each do |object|
attributes = ObjectManager::Object.new(object).attributes(user)
2014-09-09 23:42:20 +00:00
models[object] = attributes
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
2016-05-12 09:26:11 +00:00
ActiveRecord::SessionStore::Session.where('persistent IS NULL AND updated_at < ?', Time.zone.now - 2.hours).delete_all
2015-06-23 12:17:04 +00:00
# web sessions not updated the last x days
2016-05-12 09:26:11 +00:00
ActiveRecord::SessionStore::Session.where('updated_at < ?', Time.zone.now - 60.days).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
def self.list(limit = 10_000)
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
2013-12-08 16:01:54 +00:00
session.destroy
end
end