Module: Bp3::Core::Cookies

Extended by:
ActiveSupport::Concern
Included in:
Test
Defined in:
lib/bp3/core/cookies.rb

Constant Summary collapse

'_bp3_visitor'
DO_NOT_TRACK_VALUE =
'do_not_track'

Instance Method Summary collapse

Instance Method Details

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bp3/core/cookies.rb', line 91

def check_new_visitor_cookie
  cookie_value = cookies.signed[new_visitor_cookie_name]
  return if cookie_value.blank? && do_not_track

  identification = cookie_value
  sites_site_id = grsc.current_site_id
  tenant_id = grsc.current_tenant_id
  visitor = Users::Visitor.find_by(sites_site_id:, tenant_id:, identification:)
  if visitor.nil?
    visitor = create_visitor
    message = "check_visitor_cookie: create_visitor #{visitor.id} and create #{new_visitor_cookie_name} cookie"
    Rails.logger.debug message
    cookies.signed[new_visitor_cookie_name] = {
      value: visitor.identification,
      expires: 365.days.from_now
    }
  end
  @current_visitor = grsc.current_visitor = visitor
  Rails.logger.debug do
    "check_visitor_cookie: cookie[#{new_visitor_cookie_name}]=#{cookies.signed[new_visitor_cookie_name]}"
  end
end

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



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
# File 'lib/bp3/core/cookies.rb', line 62

def check_old_visitor_cookie
  cookie_value = cookies.signed[visitor_cookie_name]
  return if cookie_value.blank? && do_not_track

  sites_site_id, tenant_id, identification = cookie_value&.split('/')
  if sites_site_id && sites_site_id != grsc.current_site.id
    Rails.logger.warn { "check_visitor_cookie: site mismatch! (#{sites_site_id} and #{grsc.current_site.id}" }
  end
  if tenant_id && tenant_id != grsc.current_tenant.id
    Rails.logger.warn { "check_visitor_cookie: tenant mismatch! (#{tenant_id} and #{grsc.current_tenant.id}" }
  end
  visitor = Users::Visitor.find_by(sites_site_id:, tenant_id:, identification:)
  if visitor.nil?
    visitor = create_visitor
    message = "check_visitor_cookie: create_visitor #{visitor.id} and create #{visitor_cookie_name} cookie"
    Rails.logger.debug message
    cookies.signed[visitor_cookie_name] = {
      value: visitor.scoped_identification,
      expires: 365.days.from_now
    }
  end
  @current_visitor = grsc.current_visitor = visitor
  Rails.logger.debug do
    "check_visitor_cookie: cookie[#{visitor_cookie_name}]=#{cookies.signed[visitor_cookie_name]}"
  end
end


41
42
43
44
# File 'lib/bp3/core/cookies.rb', line 41

def check_visitor_cookie
  switch_old_to_new
  check_new_visitor_cookie
end


130
131
132
# File 'lib/bp3/core/cookies.rb', line 130

def cookie_site_id
  grsc.current_site.id[0..7]
end


134
135
136
# File 'lib/bp3/core/cookies.rb', line 134

def cookie_tenant_id
  grsc.current_tenant.id[0..7]
end

#create_visitorObject (private)



123
124
125
126
127
128
# File 'lib/bp3/core/cookies.rb', line 123

def create_visitor
  Users::Visitor.create!(sites_site: grsc.current_site,
                         tenant: grsc.current_tenant,
                         workspaces_workspace: grsc.current_workspace,
                         identification: SecureRandom.uuid)
end

#do_not_trackObject (private)



19
20
21
22
23
24
25
26
# File 'lib/bp3/core/cookies.rb', line 19

def do_not_track
  return @do_not_track unless @do_not_track.nil? # could be set to true or false already

  cookie_value = cookies[new_visitor_cookie_name]
  @do_not_track = cookie_value == DO_NOT_TRACK_VALUE
  reset_session if @do_not_track
  @do_not_track
end


119
120
121
# File 'lib/bp3/core/cookies.rb', line 119

def new_visitor_cookie_name
  VISITOR_COOKIE_NAME_PREFIX
end

#set_do_not_trackObject (private)



28
29
30
31
# File 'lib/bp3/core/cookies.rb', line 28

def set_do_not_track
  # this replaces the signed temporary cookie with an unsigned, permanent cookie
  cookies.permanent[new_visitor_cookie_name] = DO_NOT_TRACK_VALUE
end

#start_trackingObject (private)



33
34
35
36
37
38
39
# File 'lib/bp3/core/cookies.rb', line 33

def start_tracking
  return unless do_not_track

  cookies.delete(visitor_cookie_name)
  @do_not_track = false
  check_visitor_cookie
end

#switch_old_to_newObject (private)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bp3/core/cookies.rb', line 46

def switch_old_to_new
  cookie_value = cookies.signed[visitor_cookie_name]
  return if cookie_value.blank?

  _sites_site_id, _tenant_id, identification = cookie_value&.split('/')

  message = "check_visitor_cookie: switching to new visitor_cookie for #{identification}"
  Rails.logger.debug message
  cookies.delete(visitor_cookie_name)
  cookies.signed[new_visitor_cookie_name] = {
    value: identification,
    expires: 365.days.from_now
  }
end

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



115
116
117
# File 'lib/bp3/core/cookies.rb', line 115

def visitor_cookie_name
  @visitor_cookie_name ||= "#{VISITOR_COOKIE_NAME_PREFIX}_#{cookie_site_id}_#{cookie_tenant_id}"
end