diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index d28f363b..fe23c2c3 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -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'], diff --git a/lib/ucd_iam.rb b/lib/ucd_iam.rb index 79596c9c..f3892a50 100644 --- a/lib/ucd_iam.rb +++ b/lib/ucd_iam.rb @@ -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]