Class: Person

Inherits:
Object
  • Object
show all
Defined in:
lib/terraorg/model/person.rb

Constant Summary collapse

ACTIVE_USER_STATUSES =
['ACTIVE', 'PROVISIONED', 'PASSWORD_EXPIRED'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uid, okta: nil, cached: nil) ⇒ Person

Returns a new instance of Person.



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
# File 'lib/terraorg/model/person.rb', line 22

def initialize(uid, okta: nil, cached: nil)
  @id = uid

  if cached
    @name = cached.fetch('name')
    @okta_id = cached.fetch('okta_id')
    @email = cached.fetch('email')
    @status = cached.fetch('status')

    return
  elsif !okta
    # We could just be running in fmt mode, so lie about everything
    @name = "real name of #{@id}"
    @okta_id = "fake okta id for #{@id}"
    @email = "#{@id}@my.domain"
    @status = 'PROVISIONED'

    return
  end

  # Retrieve from okta
  tries = 1
  total_tries = 5

  begin
    o = okta.get_user(uid)
  rescue Faraday::ConnectionFailed => e
    if tries <= total_tries
      puts "looking up user #{uid}: #{e} (try #{tries}/#{total_tries})"
      tries += 1
      retry
    end
    raise
  end

  if tries > 1
    puts "looking up user #{uid}: success!"
  end

  # NOTE: allows users in states other than ACTIVE
  # if you want to check that, do it outside of here
  obj = o[0].to_hash
  @name = obj.fetch(:profile).fetch(:displayName)
  @okta_id = obj.fetch(:id)
  @email = obj.fetch(:profile).fetch(:email)
  @status = obj.fetch(:status)
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



20
21
22
# File 'lib/terraorg/model/person.rb', line 20

def email
  @email
end

#idObject

Returns the value of attribute id.



20
21
22
# File 'lib/terraorg/model/person.rb', line 20

def id
  @id
end

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/terraorg/model/person.rb', line 20

def name
  @name
end

#okta_idObject

Returns the value of attribute okta_id.



20
21
22
# File 'lib/terraorg/model/person.rb', line 20

def okta_id
  @okta_id
end

#statusObject

Returns the value of attribute status.



20
21
22
# File 'lib/terraorg/model/person.rb', line 20

def status
  @status
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/terraorg/model/person.rb', line 70

def active?
  ACTIVE_USER_STATUSES.member?(@status)
end

#to_json(options = nil) ⇒ Object



74
75
76
# File 'lib/terraorg/model/person.rb', line 74

def to_json(options = nil)
  {'id' => @id, 'name' => @name, 'okta_id' => @okta_id, 'email' => @email, 'status' => @status}.to_json
end