class UcbOrgs::Syncer

Syncs the org units to the LDAP tree

Since ldap is definitive and we don't keep org history, delete any department row that wasn't found in ldap.

Most of this code was lifted wholesale from APBears

Attributes

ldap_org_entries[RW]

Public Class Methods

sync() click to toggle source
# File lib/ucb_orgs/syncer.rb, line 26
def sync
  UcbOrgs::Syncer.new.sync
end

Public Instance Methods

sync() click to toggle source
# File lib/ucb_orgs/syncer.rb, line 31
def sync
  ::UCB::LDAP::Org.root_node  # force load of whole tree
  self.ldap_org_entries = ::UCB::LDAP::Org.flattened_tree

  sync_orgs
  delete_not_found_in_ldap
end

Private Instance Methods

delete_not_found_in_ldap() click to toggle source
# File lib/ucb_orgs/syncer.rb, line 51
def delete_not_found_in_ldap
  codes_found_in_ldap = ldap_org_entries.map(&:code)
  codes_found_in_db = UcbOrgs::OrgUnit.all.map(&:code)
  codes_to_delete = codes_found_in_db - codes_found_in_ldap

  UcbOrgs::OrgUnit
    .where(code: codes_to_delete)
    .delete_all
end
nb_to_string(nb) click to toggle source

Net-Ldap returns Net::BER::BerIdentifiedString. Explicityly convert to String.

# File lib/ucb_orgs/syncer.rb, line 79
def nb_to_string(nb)
  nb.nil? ? nil : nb.to_s
end
sync_org(ldap_org_entry) click to toggle source
# File lib/ucb_orgs/syncer.rb, line 61
def sync_org(ldap_org_entry)
  code = nb_to_string(ldap_org_entry.code)
  UcbOrgs::OrgUnit.find_or_initialize_by(code: code).tap do |org|
    org.update_attributes!(
      code: code,
      name: nb_to_string(ldap_org_entry.name),
      level: ldap_org_entry.level,
      level_2: nb_to_string(ldap_org_entry.level_2_code),
      level_3: nb_to_string(ldap_org_entry.level_3_code),
      level_4: nb_to_string(ldap_org_entry.level_4_code),
      level_5: nb_to_string(ldap_org_entry.level_5_code),
      level_6: nb_to_string(ldap_org_entry.level_6_code)
    )
  end
end
sync_orgs() click to toggle source
# File lib/ucb_orgs/syncer.rb, line 41
def sync_orgs
  ldap_org_entries.each do |ldap_org_entry|
    begin
      sync_org(ldap_org_entry)
    rescue Exception => e
      raise UcbOrgs::SyncError.new(ldap_org_entry.try(:code), e)
    end
  end
end