Skip to content

Commit

Permalink
look up email/login from IAM
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroldwong committed Sep 20, 2024
1 parent 8d7c293 commit c30a391
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def search
require 'dss_dw'

@results = DssDw.search_people(params[:term])&.map do |entry|

# look up email/login in case DW missed teh latest update
if !entry['email']
entry['email'] = UcdIam.get_email_by_iam_id(entry['iamId'])
end

if !entry['userId']
entry['userId'] = UcdIam.get_login_by_iam_id(entry['iamId'])
end

OpenStruct.new(
loginid: entry['userId'],
email: entry['email'],
Expand Down
44 changes: 44 additions & 0 deletions lib/ucd_iam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,50 @@ def self.fetch_bous
return json['responseData']['results'] # rubocop:disable Style/RedundantReturn
end

def self.get_email_by_iam_id(iam_id)
raise UcdIamError, 'IAM_URL and/or IAM_API_KEY environment variable(s) missing' if iam_url.blank? || iam_api_key.blank?

url = "#{iam_url}/api/iam/people/contactinfo/#{iam_id}?key=#{iam_api_key}&v=1.0"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)

return nil if response.code.to_i == 404

begin
json = JSON.parse(response.body)
rescue JSON::ParserError
return nil # not a 404 but JSON response did not make sense
end

return json.dig('responseData', 'results', 0, 'email')
end

def self.get_login_by_iam_id(iam_id)
raise UcdIamError, 'IAM_URL and/or IAM_API_KEY environment variable(s) missing' if iam_url.blank? || iam_api_key.blank?

url = "#{iam_url}/api/iam/people/prikerbacct/#{iam_id}?key=#{iam_api_key}&v=1.0"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)

return nil if response.code.to_i == 404

begin
json = JSON.parse(response.body)
rescue JSON::ParserError
return nil # not a 404 but JSON response did not make sense
end

return json.dig('responseData', 'results', 0, 'userId')
end

def self.iam_url
@@IAM_URL ||= ENV['IAM_URL']
@@IAM_URL ||= Rails.application.secrets[:iam_url]
Expand Down

0 comments on commit c30a391

Please sign in to comment.