Permalink
Browse files

Update to LDAP code

  • Loading branch information...
1 parent 5bd8026 commit da853c278f4109a8ab1435b84f320be91ac17544 @cthielen cthielen committed Feb 28, 2014
Showing with 112 additions and 79 deletions.
  1. +1 −1 config/initializers/last_updated.rb
  2. +110 −77 lib/ldap_person_helper.rb
  3. +1 −1 lib/tasks/ldap_sync.rake
@@ -1 +1 @@
-LAST_UPDATED = 'Thu Feb 27 10:21:32 PST 2014'
+LAST_UPDATED = 'Fri Feb 28 11:33:20 PST 2014'
View
@@ -162,107 +162,102 @@ def LdapPersonHelper.determine_ou_memberships(p, entry, log = nil)
# Prefer UcdLookups for OU, company, and manager information if available
ucdAppointmentDepartmentCode = entry.get_values('ucdAppointmentDepartmentCode').to_s[2..-3]
- if ucdAppointmentDepartmentCode
- if UcdLookups::DEPT_CODES.keys().include? ucdAppointmentDepartmentCode
- ou_name = UcdLookups::DEPT_CODES[ucdAppointmentDepartmentCode]['name']
- ou_manager_name = UcdLookups::DEPT_CODES[ucdAppointmentDepartmentCode]['manager']
- company_code = UcdLookups::DEPT_CODES[ucdAppointmentDepartmentCode]['company']
- company_name = UcdLookups::DEPT_CODES[company_code]['name']
- company_manager_name = UcdLookups::DEPT_CODES[company_code]['manager']
- elsif ucdStudentMajor
- if UcdLookups::MAJORS.keys().include? ucdStudentMajor
- majorDept = UcdLookups::MAJORS[ucdStudentMajor]
- ou_name = UcdLookups::DEPT_CODES[majorDept]['name']
- ou_manager_name = UcdLookups::DEPT_CODES[majorDept]['manager']
- company_code = UcdLookups::DEPT_CODES[majorDept]['company']
- company_name = UcdLookups::DEPT_CODES[company_code]['name']
- company_manager_name = UcdLookups::DEPT_CODES[company_code]['manager']
- end
- end
- elsif ucdStudentMajor
- if UcdLookups::MAJORS.keys().include? ucdStudentMajor
- majorDept = UcdLookups::MAJORS[ucdStudentMajor]
- ou_name = UcdLookups::DEPT_CODES[majorDept]['name']
- ou_manager_name = UcdLookups::DEPT_CODES[majorDept]['manager']
- company_code = UcdLookups::DEPT_CODES[majorDept]['company']
- company_name = UcdLookups::DEPT_CODES[company_code]['name']
- company_manager_name = UcdLookups::DEPT_CODES[company_code]['manager']
- end
+
+ majorDept, ou_name, ou_manager_name, company_code, company_name, company_manager_name = resolve_ou_relationship(ucdAppointmentDepartmentCode, ucdStudentMajor)
+
+ # Log if this individual has neither piece of needed information to assign them to an Organization
+ if (ucdAppointmentDepartmentCode == nil) && (ucdStudentMajor == nil)
+ log.warn "Individual (#{p.loginid}) has neither a ucdAppointmentDepartmentCode nor a ucdStudentMajor. FIXME" unless log.nil?
+ return
end
# DEPT_TRANSLATIONS helps similar names (alternate spellings, etc.) into a consistent name
- if UcdLookups::DEPT_TRANSLATIONS.keys().include? ou_name
- ou_name = UcdLookups::DEPT_TRANSLATIONS[ou_name]
- end
-
+ # if UcdLookups::DEPT_TRANSLATIONS.keys().include? ou_name
+ # ou_name = UcdLookups::DEPT_TRANSLATIONS[ou_name]
+ # end
+
# OU treatment varies for graduate students vs everybody else
if p.affiliations.collect{ |x| x.name }.include?("student:graduate") and ucdStudentMajor
# Graduate student
- ou = Group.find(:first, :conditions => [ "lower(name) = ?", ucdStudentMajor.downcase ]) || Group.create(:name => ucdStudentMajor)
+ # make sure they're in the ucdAppointmentDepartmentCode, ucdStudentMajor
+ #ou = Group.find(:first, :conditions => [ "lower(name) = ?", ucdStudentMajor.downcase ]) || Group.create(:name => ucdStudentMajor)
+ ou = Organization.find(:first, :conditions => [ "lower(name) = ?", ucdStudentMajor.downcase ]) #|| Group.create(:name => ucdStudentMajor)
# The dept code & manager won't be set here but should get updated once a faculty/staff comes along for that dept
- ou.save!
+ #ou.save!
elsif p.affiliations.collect{ |x| x.name }.include?("student:undergraduate") and ucdStudentMajor and not ucdAppointmentDepartmentCode
# Undergraduate with no employment
- ou = Group.find(:first, :conditions => [ "lower(name) = ?", ucdStudentMajor.downcase ]) || Group.create(:name => ucdStudentMajor)
+ #ou = Group.find(:first, :conditions => [ "lower(name) = ?", ucdStudentMajor.downcase ]) || Group.create(:name => ucdStudentMajor)
+ ou = Organization.find(:first, :conditions => [ "lower(name) = ?", ucdStudentMajor.downcase ]) #|| Group.create(:name => ucdStudentMajor)
# The dept code & manager won't be set here but should get updated once a faculty/staff comes along for that dept
- ou.save!
+ #ou.save!
else
- # Not a graduate student
+ # Not an undergraduate nor a graduate student
# Ensure 'ou' exists, update if necessary
if ou_name
- ou = Group.find(:first, :conditions => [ "lower(name) = ?", ou_name.downcase ]) || Group.create(:name => ou_name)
- ou.code = ucdAppointmentDepartmentCode if ou.code.nil?
- ou.save!
+ #ou = Group.find(:first, :conditions => [ "lower(name) = ?", ou_name.downcase ]) || Group.create(:name => ou_name)
+ ou = Organization.find(:first, :conditions => [ "lower(name) = ?", ou_name.downcase ]) #|| Group.create(:name => ou_name)
+ #ou.code = ucdAppointmentDepartmentCode if ou.code.nil?
+ #ou.save!
- # Set OU manager to be an owner of their OU. Ensure this person record is in manager's favorites
- ou_manager = Person.find_or_create_by_loginid(ou_manager_name)
+ if ou
+ # Set OU manager to be an owner of their OU
+ ou_manager = Person.find_or_create_by_loginid(ou_manager_name)
- unless ou.owners.include? ou_manager
- ou.owners << ou_manager
- end
+ # Ensure this manager is recorded for the Organization
+ unless ou.managers.include? ou_manager
+ ou.managers << ou_manager
+ end
- # Ensure they have this person as their favorite
- unless (ou_manager.id == p.id) or (ou_manager.favorites.include? p)
- # Ensure a manager has all their employees automatically set as favorites
- ou_manager.favorites << p
+ # Ensure they have this person as their favorite
+ unless (ou_manager.id == p.id) or (ou_manager.favorites.include? p)
+ # Ensure a manager has all their employees automatically set as favorites
+ # keep adding them as favorites
+ ou_manager.favorites << p
+ end
+ else
+ log.warn "Found a department not in the Organization tree. It has ucdAppointmentDepartmentCode #{ucdAppointmentDepartmentCode} (for person #{p.loginid}). FIXME" unless log.nil?
end
elsif ucdAppointmentDepartmentCode
- # Department with no translation. Assume LDAP data is trustworthy.
- ou = Group.find_or_initialize_by_code(ucdAppointmentDepartmentCode)
- ou_name = entry.get_values('ou')[0]
-
- if UcdLookups::DEPT_TRANSLATIONS.keys().include? ou_name
- ou_name = UcdLookups::DEPT_TRANSLATIONS[ou_name]
- end
-
- ou.name = ou_name
+ log.warn "Found a department not in the Organization tree. It has ucdAppointmentDepartmentCode #{ucdAppointmentDepartmentCode} (for person #{p.loginid}). FIXME" unless log.nil?
- ou.save!
+ # # Department with no translation. Assume LDAP data is trustworthy.
+ # ou = Group.find_or_initialize_by_code(ucdAppointmentDepartmentCode)
+ # ou_name = entry.get_values('ou')[0]
+ #
+ # if UcdLookups::DEPT_TRANSLATIONS.keys().include? ou_name
+ # ou_name = UcdLookups::DEPT_TRANSLATIONS[ou_name]
+ # end
+ #
+ # ou.name = ou_name
+ #
+ # ou.save!
end
# Ensure 'company' exists, update if necessary
- if company_name
- company = Group.find_or_initialize_by_code(company_code)
- company.name = company_name if company.name.blank?
- company.save!
-
- # Set company manager to be an owner of their company.
- company_manager = Person.find_or_create_by_loginid(company_manager_name)
-
- unless company.owners.include? company_manager
- company.owners << company_manager
- end
- end
+ # stop doing this company stuff entirely
+ # when the org tree is fixed, it'll just be the immediate, single parent
+ # if company_name
+ # company = Group.find_or_initialize_by_code(company_code)
+ # company.name = company_name if company.name.blank?
+ # company.save!
+ #
+ # # Set company manager to be an owner of their company.
+ # company_manager = Person.find_or_create_by_loginid(company_manager_name)
+ #
+ # unless company.owners.include? company_manager
+ # company.owners << company_manager
+ # end
+ # end
end
- unless p.groups.include? ou or ou.nil?
- p.groups << ou
+ unless p.organizations.include?(ou) or ou.nil?
+ p.organizations << ou
end
- unless p.groups.include? company or company.nil?
- p.groups << company
- end
+ # unless p.groups.include? company or company.nil?
+ # p.groups << company
+ # end
# Remove this person from any OUs not mentioned in LDAP
# in case they have since left that department/company.
@@ -271,13 +266,51 @@ def LdapPersonHelper.determine_ou_memberships(p, entry, log = nil)
# the proper one above in order to maintain clean
# logs (deleting an OU association may trigger a
# log message).
- p.groups.ous.each do |o|
- if (o != ou) and (o != company)
- p.groups.destroy(o)
+ p.organizations.each do |o|
+ if (o != ou) #and (o != company)
+ p.organizations.destroy(o)
log.debug "Removing ou #{o.name} from person #{p.loginid}" unless log.nil?
end
end
return p
end
+
+ # Simple function to determine _how_ a person is related to UCD (staff, faculty, student) and return
+ # the relevant OU details. This is separate from determine_affiliation_details.
+ def LdapPersonHelper.resolve_ou_relationship(ucdAppointmentDepartmentCode, ucdStudentMajor)
+ if ucdAppointmentDepartmentCode
+ if UcdLookups::DEPT_CODES.keys().include? ucdAppointmentDepartmentCode
+ # This OU should be in the org tree
+ majorDept = nil
+ ou_name = UcdLookups::DEPT_CODES[ucdAppointmentDepartmentCode]['name']
+ ou_manager_name = UcdLookups::DEPT_CODES[ucdAppointmentDepartmentCode]['manager']
+ company_code = UcdLookups::DEPT_CODES[ucdAppointmentDepartmentCode]['company']
+ company_name = UcdLookups::DEPT_CODES[company_code]['name']
+ company_manager_name = UcdLookups::DEPT_CODES[company_code]['manager']
+ elsif ucdStudentMajor
+ # Use major to determine OU (should still be in the org tree)
+ if UcdLookups::MAJORS.keys().include? ucdStudentMajor
+ majorDept = UcdLookups::MAJORS[ucdStudentMajor]
+ ou_name = UcdLookups::DEPT_CODES[majorDept]['name']
+ ou_manager_name = UcdLookups::DEPT_CODES[majorDept]['manager']
+ company_code = UcdLookups::DEPT_CODES[majorDept]['company']
+ company_name = UcdLookups::DEPT_CODES[company_code]['name']
+ company_manager_name = UcdLookups::DEPT_CODES[company_code]['manager']
+ end
+ end
+ elsif ucdStudentMajor
+ # log if the major is not in the lookup table and add them
+ if UcdLookups::MAJORS.keys().include? ucdStudentMajor
+ majorDept = UcdLookups::MAJORS[ucdStudentMajor]
+ ou_name = UcdLookups::DEPT_CODES[majorDept]['name']
+ ou_manager_name = UcdLookups::DEPT_CODES[majorDept]['manager']
+ company_code = UcdLookups::DEPT_CODES[majorDept]['company']
+ company_name = UcdLookups::DEPT_CODES[company_code]['name']
+ company_manager_name = UcdLookups::DEPT_CODES[company_code]['manager']
+ end
+ end
+
+ return majorDept, ou_name, ou_manager_name, company_code, company_name, company_manager_name
+ end
end
View
@@ -73,7 +73,7 @@ namespace :ldap do
ldap.search('(uid=' + person.loginid + ')') do |entry|
p = LdapPersonHelper.create_or_update_person_from_ldap(entry, log)
- save_or_touch(p, log)
+ save_or_touch(p, log) if p
end
unless p

0 comments on commit da853c2

Please sign in to comment.