Skip to content

Commit

Permalink
switch to IAM for people search/import
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroldwong committed Sep 26, 2024
1 parent c30a391 commit 8c7bc2f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 33 deletions.
14 changes: 1 addition & 13 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,7 @@ def show
def search
authorize Person

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

@results = UcdIam.search_people(params[:term])&.map do |entry|
OpenStruct.new(
loginid: entry['userId'],
email: entry['email'],
Expand Down
2 changes: 1 addition & 1 deletion lib/dss_dw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def self.create_or_update_using_dw(loginid)
return p
end

dw_person = DssDw.fetch_person_by_loginid(loginid)
dw_person = DssDw.fetch_person_by_loginid(loginid) || UcdIam.fetch_person_by_loginid(loginid)

return nil unless dw_person

Expand Down
101 changes: 82 additions & 19 deletions lib/ucd_iam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,95 @@ 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)
def self.search_by(field, query)
endpoints = {
"full" => "iam/people/search?",
"first" => "iam/people/search?dFirstName=",
"last" => "iam/people/search?dFirstName=",
"login" => "iam/people/prikerbacct/search?userId=",
"email" => "iam/people/contactinfo/search?email="
}

if field == "full"
names = query.split(" ")
params = "dFirstName=#{names.first}&dLastName=#{names.last}"
else
params = query
end

return nil if response.code.to_i == 404
fetch(endpoints[field] + params)
end

begin
json = JSON.parse(response.body)
rescue JSON::ParserError
return nil # not a 404 but JSON response did not make sense
# Returns hash of userId, email, dFullName
def self.search_people(query)
results = {}
threads = []

threads << Thread.new { results[:fullName] = search_by("full", query) }
threads << Thread.new { results[:firstName] = search_by("first", query) }
threads << Thread.new { results[:lastName] = search_by("last", query) }
threads << Thread.new { results[:email] = search_by("email", query) }
threads << Thread.new { results[:loginid] = search_by("login", query) }
threads.each(&:join)

iam_ids = results.values.flatten.compact.map { |res| res["iamId"] }.uniq.take(10)

users = iam_ids.map do |iam_id|
user = {}
p = fetch_person_by_iam_id(iam_id)
user["userId"] = p["userId"]
user["email"] = p["campusEmail"]
user["dFullName"] = p["dFullName"]

user
end

users
end

# Returns hash of contactInfo, ppsAssociations, person, prikerbacct, sisAssociations
def self.fetch_person_by_loginid(loginid)
p = search_by('login', loginid)
iam_id = p[0]["iamId"]

results = {}
threads = []

threads << Thread.new { results["contactInfo"] = fetch_contact_by_iam_id(iam_id) }
threads << Thread.new { results["ppsAssociations"] = fetch_pps_associations_by_iam_id(iam_id) }
threads << Thread.new { results["person"] = fetch_person_by_iam_id(iam_id) }
threads << Thread.new { results["prikerbacct"] = fetch_kerb_by_iam_id(iam_id) }
threads << Thread.new { results["sisAssociations"] = fetch_sis_associations_by_iam_id(iam_id) }

threads.each(&:join)

results
end

def self.fetch_contact_by_iam_id(iam_id)
fetch("iam/people/contactinfo/#{iam_id}")[0]
end

def self.fetch_pps_associations_by_iam_id(iam_id)
fetch("iam/associations/pps/#{iam_id}")
end

def self.fetch_person_by_iam_id(iam_id)
fetch("iam/people/#{iam_id}")[0]
end

def self.fetch_kerb_by_iam_id(iam_id)
fetch("iam/people/prikerbacct/#{iam_id}")[0]
end

return json.dig('responseData', 'results', 0, 'email')
def self.fetch_sis_associations_by_iam_id(iam_id)
fetch("iam/associations/sis/#{iam_id}")
end

def self.get_login_by_iam_id(iam_id)
def self.fetch(path)
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"
url = "#{iam_url}/api/#{path}#{path.include?("?") ? "&" : "?"}key=#{iam_api_key}&v=1.0"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
Expand All @@ -91,7 +154,7 @@ def self.get_login_by_iam_id(iam_id)
return nil # not a 404 but JSON response did not make sense
end

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

def self.iam_url
Expand Down

0 comments on commit 8c7bc2f

Please sign in to comment.