forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_helper.rb
93 lines (75 loc) · 2.47 KB
/
session_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
module SessionHelper
def self.json_hash(user)
collections, assets = default_collections(user)
{
session: user.filter_unauthorized_attributes(user.filter_attributes(user.attributes)),
models: models(user),
collections: collections,
assets: assets,
}
end
def self.json_hash_error(error)
{
error: error.message,
models: models,
collections: {
Locale.to_app_model => Locale.where(active: true),
PublicLink.to_app_model => PublicLink.all,
}
}
end
def self.default_collections(user)
# auto population collections, store all here
default_collection = {}
assets = user.assets({})
# load collections to deliver from external files
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}
class_name = $1.camelize
next if !Object.const_defined?(class_name) && Rails.env.production?
(default_collection, assets) = class_name.constantize.session(default_collection, assets, user)
end
[default_collection, assets]
end
def self.models(user = nil)
return models_public if user.blank?
ObjectManager.list_objects.each_with_object({}) do |object, models|
attributes = ObjectManager::Object.new(object).attributes(user)
models[object] = attributes
end
end
def self.models_public
allowed_user_attributes = %w[firstname lastname email password]
user_attributes = ObjectManager::Object
.new('User')
.attributes(nil, skip_permission: true)
.select { |attribute| allowed_user_attributes.include?(attribute[:name]) }
{
'User' => user_attributes,
}
end
def self.cleanup_expired
# delete temp. sessions
ActiveRecord::SessionStore::Session
.where(persistent: nil, updated_at: ...2.hours.ago)
.delete_all
# web sessions not updated the last x days
ActiveRecord::SessionStore::Session
.where(updated_at: ...60.days.ago)
.delete_all
end
def self.get(id)
ActiveRecord::SessionStore::Session.find_by(id: id)
end
def self.list(limit = 10_000)
ActiveRecord::SessionStore::Session.reorder(updated_at: :desc).limit(limit)
end
def self.destroy(id)
ActiveRecord::SessionStore::Session
.find_by(id: id)
&.destroy
end
end