# Strip leading/ending whitespace; LDAP data can be dirty
# Using email for username deliberately here
- 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.usrname = self._simple_map('mail').lower()
+ self.email = self._simple_map('mail').lower()
+ self.family_name = self._simple_map('sn')
self.ident_type, self.ident_value = self.get_identity()
self.home_ou = self.get_home_ou()
+ self.first_given_name = self._simple_map('givenName')
+ if not self.first_given_name:
+ self.first_given_name('LDAP_NULL')
+ print >> sys.stderr, 'No givenName for %s' % (self.usrname)
+ self.lang_pref = self._simple_map('preferredLanguage')
# Randomized password, assuming user will use "Forgot password"
# function to set their own password
self.passwd = oils.utils.utils.md5sum(os.urandom(10))
- 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)
-
self.profile = self.get_profile()
self.expire_date = self.get_expiry_date()
+ self.barcode = self._simple_map('lulLibraryBarcode')
+
+ def _simple_map(self, ldap_attribute):
+ """
+ Convenience method for mapping a given LDAP attribute
+ """
+
+ if ldap_attribute in self.ldap_atts:
+ return self.ldap_atts[ldap_attribute][0].strip()
+
+ return None
def get_expiry_date(self):
"""
"""
if 'lulStudentLevel' in self.ldap_atts:
- affiliation = self.ldap_atts['lulStudentLevel'][0].strip().lower()
+ affiliation = self._simple_map('lulStudentLevel').lower()
elif 'lulPrimaryAffiliation' in self.ldap_atts:
- affiliation = self.ldap_atts['lulPrimaryAffiliation'][0].strip().lower()
+ affiliation = self._simple_map('lulPrimaryAffiliation').lower()
else:
affiliation = r'\N'
- if affiliation == 'ug' or affiliation == 'student':
- return 13
- elif affiliation == 'gr':
- return 12
- elif affiliation == 'al':
- return 14
- elif affiliation == 'faculty':
- return 11
- elif affiliation == 'staff':
- return 15
- elif affiliation == 'proxy':
- return None
- return None
+ profile_map = {
+ 'ug': 13,
+ 'student': 13,
+ 'gr': 12,
+ 'al': 14,
+ 'faculty': 11,
+ 'staff': 15,
+ 'proxy': None,
+ 'affiliate': 15
+ }
+
+ if affiliation in profile_map:
+ return profile_map[affiliation]
+ else:
+ print >> sys.stderr, "Affiliation '%s' not mapped to a profile " \
+ "for user %s" % (affiliation, self.usrname)
def get_home_ou(self):
"""
if ARGS.dump_ldap:
dump_data(result_data)
if ARGS.create_users:
- barcode = create_evergreen_user(auth, user)
- update_ldap_barcode(con, user, barcode)
+ res = create_evergreen_user(auth, user)
+ update_ldap_barcode(con, user)
if ARGS.push_barcode:
try:
uid = find_evergreen_user(auth, user)
print >> sys.stderr, "User not found in Evergreen: %s" % \
(user.ident_value)
continue
- barcode = get_barcode(auth, uid)
- update_ldap_barcode(con, user, barcode)
+ user.barcode = get_barcode(auth, uid)
+ update_ldap_barcode(con, user)
except ldap.LDAPError, exc:
print >> sys.stderr, exc
limit = 1
sort = None
- search_ou = 1
+ search_depth = 1
include_inactive = True
print("Trying to find user: %s %s %s" %
by_id = osrf_request(
'open-ils.actor', 'open-ils.actor.patron.search.advanced',
auth, {'ident_value': {'value': user.ident_value, 'group': 0}}, limit,
- sort, include_inactive, search_ou
+ sort, include_inactive, search_depth
).send()
if by_id and len(by_id):
by_email = osrf_request(
'open-ils.actor', 'open-ils.actor.patron.search.advanced',
auth, {'email': {'value': user.email}}, limit,
- sort, include_inactive, search_ou
+ sort, include_inactive, search_depth
).send()
if by_email and len(by_email):
by_usrname = osrf_request(
'open-ils.actor', 'open-ils.actor.patron.search.advanced',
auth, {'usrname': {'value': user.usrname}}, limit,
- sort, include_inactive, search_ou
+ sort, include_inactive, search_depth
).send()
if by_usrname and len(by_usrname):
'open-ils.actor', 'open-ils.actor.patron.update', auth, newau
).send()
except Exception, exc:
- print exc
+ print >> sys.stderr, exc
+ return None
evt = oils.event.Event.parse_event(usr)
if evt and not evt.success:
- raise AuthException(evt.text_code)
+ print >> sys.stderr, "Create Evergreen user failed for %s: %s" % (
+ AuthException(evt.text_code), user.usrname)
+ return None
# Generate a barcode for the user
try:
auth, usr.id()
).send()
except Exception, exc:
- print exc
+ print >> sys.stderr, exc
+ return None
evt = oils.event.Event.parse_event(barcode)
if evt and not evt.success:
- raise AuthException(evt.text_code)
+ print >> sys.stderr, "Create barcode failed for %s: %s" % (
+ AuthException(evt.text_code), user.usrname)
+ return None
- barcode = barcode['evergreen.lu_update_barcode']
-
- create_stat_cats(newau, result_data)
+ user.barcode = barcode['evergreen.lu_update_barcode']
- print("Created: %s with barcode %s" % (newau.usrname(), barcode))
+ create_stat_cats(newau, user)
- return barcode
+ print("Created: %s with barcode %s" % (newau.usrname(), user.barcode))
-def update_ldap_barcode(con, user, barcode):
+def update_ldap_barcode(con, user):
"""
Updates the LDAP directory with the new barcode for a given user
"""
try:
- mod_attrs = [(ldap.MOD_REPLACE, 'lulLibraryBarcode', [barcode])]
+ mod_attrs = [(ldap.MOD_REPLACE, 'lulLibraryBarcode', [user.barcode])]
con.modify_s(user.cname, mod_attrs)
except Exception, exc:
print >> sys.stderr, exc
+ return False
return True
-def create_stat_cats(user, result_data):
+def create_stat_cats(eg_user, ldap_user):
"""
Map statistical categories
"""
- # Reasonable default
- lang = 'E'
- if 'preferredLanguage' in result_data:
- lang = result_data['preferredLanguage'][0].strip()
+ if ldap_user.lang_pref:
+ pass
# XXX Now we should generate stat cats eh?
parser.add_argument('--query-id',
help='Search LDAP for a specific user by id attribute'
)
- parser.add_argument('-date', '--query-date',
+ parser.add_argument('--query-date',
help='Search LDAP for users created since (YYYYMMDDHHMMSSZ)'
)
parser.add_argument('-U', '--eg-user', nargs='?',