From: Dan Scott Date: Fri, 9 Sep 2011 04:45:30 +0000 (-0400) Subject: New --push-barcode option updates LDAP with Evergreen barcodes X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=8b7336aa253680a84bf35b86883dc2ae592ac088;p=contrib%2FConifer.git New --push-barcode option updates LDAP with Evergreen barcodes By default, the --create-users option will update LDAP with the new barcode that was generated for the new users. One can also use a --query of some type and pass --push-barcode to retrieve the barcodes for all of the LDAP users from Evergreen and then push those barcodes into their respective LDAP accounts. Signed-off-by: Dan Scott --- diff --git a/tools/patron-load/ldap_osrf_sync b/tools/patron-load/ldap_osrf_sync index f305d14e4e..420936a35a 100644 --- a/tools/patron-load/ldap_osrf_sync +++ b/tools/patron-load/ldap_osrf_sync @@ -339,21 +339,51 @@ def find_ldap_users(con, ldap_filter, attributes, auth): if result_data == []: break + cname = result_data[0][0] if ARGS.dump_ldap: dump_data(result_data) if ARGS.create_users: - (ident, code) = create_evergreen_user(auth, result_data[0][1]) - # Push the barcode into LDAP - update_ldap_barcode(con, ident, code) + barcode = create_evergreen_user(auth, result_data[0][1]) + update_ldap_barcode(con, cname, 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 + continue + if not uid: + print >> sys.stderr, "User not found in Evergreen: %s" % \ + (user.ident_value) + continue + barcode = get_barcode(auth, uid) + update_ldap_barcode(con, cname, barcode) except ldap.LDAPError, exc: print >> sys.stderr, exc +def get_barcode(auth, uid): + """ + Retrieve the barcode for a user from Evergreen based on their user ID + """ + user = osrf_request( + 'open-ils.actor', 'open-ils.actor.user.fleshed.retrieve', + auth, int(uid), ['card'] + ).send() + evt = oils.event.Event.parse_event(user) + if evt and not evt.success: + raise AuthException(evt.text_code) + + if not user: + return None + + return user.card().barcode() + def find_evergreen_user(auth, user): """ Search for an existing user in Evergreen - Returns True if found, False if not + Returns user ID if found, None if not """ limit = 1 @@ -372,7 +402,7 @@ def find_evergreen_user(auth, user): ).send() if by_id and len(by_id): - return True + return by_id[0] by_email = osrf_request( 'open-ils.actor', 'open-ils.actor.patron.search.advanced', @@ -381,7 +411,7 @@ def find_evergreen_user(auth, user): ).send() if by_email and len(by_email): - return True + return by_email[0] by_usrname = osrf_request( 'open-ils.actor', 'open-ils.actor.patron.search.advanced', @@ -390,7 +420,9 @@ def find_evergreen_user(auth, user): ).send() if by_usrname and len(by_usrname): - return True + return by_usrname[0] + + return None def create_evergreen_user(auth, result_data): """ @@ -455,9 +487,9 @@ def create_evergreen_user(auth, result_data): print("Created: %s with barcode %s" % (newau.usrname(), barcode)) - return (newau.ident_value(), barcode) + return barcode -def update_ldap_barcode(con, ident, barcode): +def update_ldap_barcode(con, cname, barcode): """ Updates the LDAP directory with the new barcode for a given user @@ -465,25 +497,9 @@ def update_ldap_barcode(con, ident, barcode): first, so take the first match we get. Reckless, I know. """ - base_dn = 'o=lul' - search_scope = ldap.SCOPE_SUBTREE - try: - ldap_filter = '(&%s(lulColleagueId=%s))' % ( - '(objectclass=lulEduPerson)', ident - ) - result_id = con.search(base_dn, search_scope, ldap_filter) - while 1: - result_type, result_data = con.result(result_id, 0) - if result_data == []: - break - - if 'lulColleagueId' not in result_data[0][1]: - continue - - cname = result_data[0][1]['cn'][0] - mod_attrs = [(ldap.MOD_REPLACE, 'lulLibraryBarcode', barcode)] - con.modify(cname, mod_attrs) + mod_attrs = [(ldap.MOD_REPLACE, 'lulLibraryBarcode', [barcode])] + con.modify_s(cname, mod_attrs) except Exception, exc: print >> sys.stderr, exc @@ -517,7 +533,7 @@ def ldap_query(con, auth): Process LDAP users created since a given date """ attributes = [ - 'lulLibraryBarcode', + 'lulLibraryBarcode', 'createTimestamp', 'lulStudentLevel', 'lulPrimaryAffiliation', 'cn', 'mail', 'givenName', 'sn', 'lulColleagueId', 'preferredLanguage' ] @@ -538,7 +554,8 @@ def ldap_query(con, auth): ldap_filter = '(&%s(lulColleagueId=%s))' % ( '(objectclass=lulEduPerson)', ARGS.query_id ) - + if not ldap_filter: + return find_ldap_users(con, ldap_filter, attributes, auth) def parse_args(): @@ -552,6 +569,9 @@ def parse_args(): parser.add_argument('-c', '--create-users', action='store_true', help='Create new users in Evergreen' ) + parser.add_argument('-b', '--push-barcode', action='store_true', + help='Push barcode to LDAP' + ) parser.add_argument('--query-cn', help='Search LDAP for a specific user by cn attribute' )