forked from sharetribe/sharetribe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_context_store.rb
80 lines (67 loc) · 1.73 KB
/
session_context_store.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
module SessionContextStore
Context = EntityUtils.define_builder(
[:marketplace_id, :fixnum],
[:marketplace_uuid, :uuid],
[:user_id, :string],
[:user_uuid, :uuid],
[:user_role, one_of: [nil, :user, :admin]])
module_function
def set(ctx)
RequestStore[:__session_context] = Context.call(ctx)
end
def get
RequestStore[:__session_context] ||= Context.call({})
end
# Resets the store
#
# Note: Calling `reset!` is needed only in exceptional cases!
# Normally, the underlying RequestStore is reseted after
# each request or delayed job
#
def reset!
RequestStore[:__session_context] = nil
end
def set_from_model(community: nil, person: nil)
role =
if person.nil?
nil
elsif person.has_admin_rights?(community)
:admin
else
:user
end
session_context = {
marketplace_id: community&.id,
marketplace_uuid: community&.uuid_object,
user_id: person&.id,
user_uuid: person&.uuid_object,
user_role: role
}
set(session_context)
end
def set_from_transaction(actor:, tx:)
marketplace_session_ctx = {
marketplace_id: tx[:community_id],
marketplace_uuid: tx[:community_uuid]
}
user_session_ctx =
case actor
when :starter
{
user_id: tx[:starter_id],
user_uuid: tx[:starter_uuid]
}
when :author
{
user_id: tx[:listing_author_id],
user_uuid: tx[:listing_author_uuid]
}
when :unknown
# Unknown user
{}
else
raise ArgumentError.new("Unknown transition actor: #{actor}")
end
SessionContextStore.set(marketplace_session_ctx.merge(user_session_ctx))
end
end