From: Dan Scott Date: Wed, 14 Sep 2011 15:56:09 +0000 (-0400) Subject: Pass User objects rather than raw vars X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=1a4c5afaa6d0cc34771a62c7db3367f63876d1ce;p=contrib%2FConifer.git Pass User objects rather than raw vars Also strip out functions that are not used in this script relating to the creation of barcodes. Signed-off-by: Dan Scott --- diff --git a/tools/patron-load/ldap_osrf_sync b/tools/patron-load/ldap_osrf_sync old mode 100644 new mode 100755 index 3601958277..d069e5ca98 --- a/tools/patron-load/ldap_osrf_sync +++ b/tools/patron-load/ldap_osrf_sync @@ -77,18 +77,19 @@ class User: Easier to replace hard-coded mappings with calls to functions """ - self.raw_ldap = raw_ldap + self.cname = raw_ldap[0] + self.ldap_atts = raw_ldap[1] - if 'mail' not in raw_ldap: - print >> sys.stderr, 'mail not found for %s' % raw_ldap['cn'] + if 'mail' not in self.ldap_atts: + print >> sys.stderr, 'mail not found for %s' % self.cname return None # Strip leading/ending whitespace; LDAP data can be dirty # Using email for username deliberately here - self.usrname = raw_ldap['mail'][0].strip().lower() - self.email = raw_ldap['mail'][0].strip().lower() - self.family_name = raw_ldap['sn'][0].strip() + self.usrname = self.ldap_atts['mail'][0].strip().lower() + self.email = self.ldap_atts['mail'][0].strip().lower() + self.family_name = self.ldap_atts['sn'][0].strip() self.ident_type, self.ident_value = self.get_identity() self.home_ou = self.get_home_ou() @@ -96,8 +97,8 @@ class User: # function to set their own password self.passwd = oils.utils.utils.md5sum(os.urandom(10)) - if 'givenName' in raw_ldap: - self.first_given_name = raw_ldap['givenName'][0].strip() + if 'givenName' in self.ldap_atts: + self.first_given_name = self.ldap_atts['givenName'][0].strip() else: self.first_given_name('LDAP_NULL') print >> sys.stderr, 'No givenName for %s' % (self.usrname) @@ -122,7 +123,7 @@ class User: Map LDAP record to Evergreen identity type and value """ - ident_value = self.raw_ldap['lulColleagueId'][0].strip().lower() + ident_value = self.ldap_atts['lulColleagueId'][0].strip().lower() if len(ident_value) != 7: print >> sys.stderr, 'Datatel number not 7 chars for %s (%s)' % ( self.usrname, ident_value @@ -139,10 +140,10 @@ class User: Map LDAP record to Evergreen profile """ - if 'lulStudentLevel' in self.raw_ldap: - affiliation = self.raw_ldap['lulStudentLevel'][0].strip().lower() - elif 'lulPrimaryAffiliation' in self.raw_ldap: - affiliation = self.raw_ldap['lulPrimaryAffiliation'][0].strip().lower() + if 'lulStudentLevel' in self.ldap_atts: + affiliation = self.ldap_atts['lulStudentLevel'][0].strip().lower() + elif 'lulPrimaryAffiliation' in self.ldap_atts: + affiliation = self.ldap_atts['lulPrimaryAffiliation'][0].strip().lower() else: affiliation = r'\N' @@ -272,62 +273,6 @@ def osrf_request(service, method, *args): req.setPath(credentials.GATEWAY_URL) return req -def datatel_to_barcode(datatel): - """ - Converts a Datatel Colleague ID into a barcode - - Used only for matching legacy barcodes for the purposes of updates. - New users will get a barcode generated for them from a database series. - - >>> datatel_to_barcode('0104923') - '00007001049233' - """ - - barcode = '000070%s' % (datatel) - barcode = '%s%d' % (barcode, mod10_checksum(barcode)) - - return barcode - -def barcode_to_datatel(barcode): - """ - Converts a barcode into a Datatel Colleague ID - - Used to generate the ident_value for legacy users. - - >>> barcode_to_datatel('00007001049233') - '0104923' - """ - - if len(barcode) != 14: - return False - - return barcode[6:13] - -def mod10_checksum(barcode): - """ - Calculates the mod10 checksum for a given string of digits - - This checksum algorithm is used for Code 3 of 9 barcodes. - """ - - total, position = 0, 0 - for digit in barcode: - digit = int(digit) - position += 1 - if (position % 2): - digit *= 2 - if digit < 10: - total += digit - else: - total += digit - 9 - else: - total += digit - - rem = total % 10 - if rem: - return 10 - rem - return rem - def find_ldap_users(con, ldap_filter, attributes, auth): """ Retrieve personnel accounts from LDAP directory and process'em @@ -342,15 +287,14 @@ def find_ldap_users(con, ldap_filter, attributes, auth): if result_data == []: break - cname = result_data[0][0] + user = User(result_data[0]) if ARGS.dump_ldap: dump_data(result_data) if ARGS.create_users: - barcode = create_evergreen_user(auth, result_data[0][1]) - update_ldap_barcode(con, cname, barcode) + barcode = create_evergreen_user(auth, user) + update_ldap_barcode(con, user, barcode) if ARGS.push_barcode: try: - user = User(result_data[0][1]) uid = find_evergreen_user(auth, user) except AttributeError: # stub LDAP account; move on @@ -427,12 +371,11 @@ def find_evergreen_user(auth, user): return None -def create_evergreen_user(auth, result_data): +def create_evergreen_user(auth, user): """ Map LDAP record to Evergreen user """ - user = User(result_data) if not user: return @@ -496,7 +439,7 @@ def create_evergreen_user(auth, result_data): return barcode -def update_ldap_barcode(con, cname, barcode): +def update_ldap_barcode(con, user, barcode): """ Updates the LDAP directory with the new barcode for a given user @@ -506,7 +449,7 @@ def update_ldap_barcode(con, cname, barcode): try: mod_attrs = [(ldap.MOD_REPLACE, 'lulLibraryBarcode', [barcode])] - con.modify_s(cname, mod_attrs) + con.modify_s(user.cname, mod_attrs) except Exception, exc: print >> sys.stderr, exc @@ -540,9 +483,10 @@ def ldap_query(con, auth): Process LDAP users created since a given date """ attributes = [ - 'lulLibraryBarcode', 'createTimestamp', + 'lulLibraryBarcode', 'createTimestamp', 'lulAffiliation', 'lulStudentLevel', 'lulPrimaryAffiliation', 'cn', 'mail', - 'givenName', 'sn', 'lulColleagueId', 'preferredLanguage' + 'givenName', 'sn', 'lulColleagueId', 'preferredLanguage', + 'lulModifyTimestamp' ] if (ARGS.query_date):