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
).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',
).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',
).send()
if by_usrname and len(by_usrname):
- return True
+ return by_usrname[0]
+
+ return None
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
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
Process LDAP users created since a given date
"""
attributes = [
- 'lulLibraryBarcode',
+ 'lulLibraryBarcode', 'createTimestamp',
'lulStudentLevel', 'lulPrimaryAffiliation', 'cn', 'mail',
'givenName', 'sn', 'lulColleagueId', 'preferredLanguage'
]
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():
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'
)