Class: Masks::Credentials::Session

Inherits:
Masks::Credential show all
Defined in:
app/models/masks/credentials/session.rb

Overview

Checks for past :actor(s) on a session.

This can be used in lieu of supplying identifying details like an email/nickname (usually provided they were supplied in the past).

Instance Method Summary collapse

Methods inherited from Masks::Credential

#backup!, #check, checks, #cleanup!, #mask!, #name, #patch_params, #slug

Instance Method Details

#backupObject



50
51
52
53
54
55
56
# File 'app/models/masks/credentials/session.rb', line 50

def backup
  return unless actor && passed?

  session.data[:actors] ||= {}
  session.data[:actors][actor.actor_id] = actor.session_key
  session.data[:actor] = actor.actor_id
end

#cleanupObject



58
59
60
61
62
63
64
# File 'app/models/masks/credentials/session.rb', line 58

def cleanup
  actor_id = actor&.actor_id || session.data[:actor]

  session.data[:actor] = nil
  session.data[:actors] ||= {}
  session.data[:actors].delete(actor_id)
end

#lookupObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/masks/credentials/session.rb', line 12

def lookup
  return if session_params[:actor_id]

  actor_ids = session.data[:actors]&.keys || []
  actor_id = session.data[:actor]
  actors = (actor_ids.any? ? config.find_actors(session, actor_ids) : [])

  # only lookup and return the current actor if
  # it's not provided via a param (e.g. someone
  # is trying to login)
  actor =
    if actor_id
      actors.find do |a|
        a.actor_id == actor_id &&
          a.session_key == session.data[:actors][a.actor_id]
      end
    end

  actor = Actors::Anonymous.new(session:) if optional? && !actors.present?

  actor
end

#maskupObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/masks/credentials/session.rb', line 35

def maskup
  return approve! if optional? && actor&.anonymous?

  actor_id = actor&.actor_id

  return unless actor_id && session.data[:actors]&.fetch(actor_id, nil)
  return unless session.data[:actor] == actor_id

  if session.data.dig(:actors, actor_id) == actor.session_key
    approve!
  else
    cleanup
  end
end