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()
# 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)
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
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'
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
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
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
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
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
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):