[sysadmin-bin] Allow passing more than just one attribute over to the get_attributes_from_ldap function



commit 000c5511f22cda9f7be4c36f473ca63304f5fc63
Author: Andrea Veri <averi redhat com>
Date:   Fri Oct 13 17:24:46 2017 +0200

    Allow passing more than just one attribute over to the get_attributes_from_ldap function

 gnome_ldap_utils.py                 |   21 +++++++++++++++++----
 membership/foundation-operations.py |   34 ++++++++++++++++++++--------------
 2 files changed, 37 insertions(+), 18 deletions(-)
---
diff --git a/gnome_ldap_utils.py b/gnome_ldap_utils.py
index 67bd4a7..738afdd 100755
--- a/gnome_ldap_utils.py
+++ b/gnome_ldap_utils.py
@@ -38,14 +38,27 @@ class Gnome_ldap_utils:
 
         return members
 
-    def get_attributes_from_ldap(self, uid, attr):
+    def get_attributes_from_ldap(self, uid, attr, *attrs):
         import ldap.filter
 
+        results = [] 
+
         filter = ldap.filter.filter_format('(uid=%s)', (uid, ))
-        results = self.conn.search_s(self.LDAP_USER_BASE, ldap.SCOPE_SUBTREE, filter, ('uid', attr, ))
+        if len(attrs) > 0:
+            attrs = list(attrs)
+            attrs.insert(0, 'uid')
+            attrs.insert(1, attr)
+            _result = self.conn.search_s(self.LDAP_USER_BASE, ldap.SCOPE_SUBTREE, filter, (attrs))
+
+            for arg in attrs:
+                results.append(_result[0][1][arg][0])
+        else:
+            result = self.conn.search_s(self.LDAP_USER_BASE, ldap.SCOPE_SUBTREE, filter, ('uid', attr, ))
 
         if len(results) > 0:
-            return results[0][1][attr][0]
+            return results
+        elif len(result) > 0:
+            return result[0][1][attr][0]
         else:
             return None
 
@@ -70,4 +83,4 @@ class Gnome_ldap_utils:
         import ldap
 
         remove_members = [(ldap.MOD_DELETE, 'member', 'uid=%s,%s' % (userid, self.LDAP_USER_BASE))]
-        self.conn.modify_s('cn=%s,%s' % (group, self.LDAP_GROUP_BASE), remove_members)
+        self.conn.modify_s('cn=%s,%s' % (group, self.LDAP_GROUP_BASE), remove_members)
\ No newline at end of file
diff --git a/membership/foundation-operations.py b/membership/foundation-operations.py
index e2a3df1..b9ba5fb 100755
--- a/membership/foundation-operations.py
+++ b/membership/foundation-operations.py
@@ -52,11 +52,13 @@ foundationmembers = glu.get_uids_from_group('foundation')
 
 def _get_foundation_fields_from_ldap():
     for member in foundationmembers:
-        first_added_attr = glu.get_attributes_from_ldap(member, 'FirstAdded')
-        last_renewed_on_attr = glu.get_attributes_from_ldap(member, 'LastRenewedOn')
-        mail_attr = glu.get_attributes_from_ldap(member, 'mail')
-        common_name_attr = glu.get_attributes_from_ldap(member, 'cn')
-        userid_attr = glu.get_attributes_from_ldap(member, 'uid')
+        ldap_fields = glu.get_attributes_from_ldap(member, 'FirstAdded', 'LastRenewedOn', 'mail', 'cn')
+
+        first_added_attr = ldap_fields[1]
+        last_renewed_on_attr = ldap_fields[2]
+        mail_attr = ldap_fields[3]
+        common_name_attr = ldap_fields[4]
+        userid_attr = ldap_fields[0]
 
         if last_renewed_on_attr == TODAY and first_added_attr == TODAY:
             send_form_letters(new_member_form_letter, mail_attr, common_name_attr, userid_attr)
@@ -80,10 +82,12 @@ def remove_expired_memberships_from_foundation():
     need_renew = {}
 
     for member in foundationmembers:
-        last_renewed_on_attr = glu.get_attributes_from_ldap(member, 'LastRenewedOn')
+        ldap_fields = glu.get_attributes_from_ldap(member, 'LastRenewedOn', 'mail', 'cn')
+
+        last_renewed_on_attr = ldap_fields[1]
         convert_to_unix_time = calendar.timegm(strptime(last_renewed_on_attr, '%Y-%m-%d'))
-        mail_attr = glu.get_attributes_from_ldap(member, 'mail')
-        common_name_attr = glu.get_attributes_from_ldap(member, 'cn')
+        mail_attr = ldap_fields[2]
+        common_name_attr = ldap_fields[3]
 
         if member in foundationmembers and convert_to_unix_time < now - 365 * 2 * 24 * 60 * 60:
             print "Removing %s from the foundation LDAP group as the membership expired on %s" % (member, 
last_renewed_on_attr)
@@ -102,8 +106,9 @@ def generate_membership_list():
     result = []
 
     for member in foundationmembers:
-        common_name_attr = glu.get_attributes_from_ldap(member, 'cn')
-        last_renewed_on_attr = glu.get_attributes_from_ldap(member, 'LastRenewedOn')
+        ldap_fields = glu.get_attributes_from_ldap(member, 'cn', 'LastRenewedOn')
+        common_name_attr = ldap_fields[1]
+        last_renewed_on_attr = ldap_fields[2]
 
         d = { 'common_name' : common_name_attr, 'last_renewed_on' : last_renewed_on_attr }
         result.append(d)
@@ -164,9 +169,10 @@ def subscribe_new_members():
     f = open('/tmp/new_subscribers', 'w')
 
     for member in foundationmembers:
-        first_added_attr = glu.get_attributes_from_ldap(member, 'FirstAdded')
-        last_renewed_on_attr = glu.get_attributes_from_ldap(member, 'LastRenewedOn')
-        mail_attr = glu.get_attributes_from_ldap(member, 'mail')
+        ldap_fields = glu.get_attributes_from_ldap(member, 'FirstAdded', 'LastRenewedOn', 'mail')
+        first_added_attr = ldap_fields[1]
+        last_renewed_on_attr = ldap_fields[2]
+        mail_attr = ldap_fields[3]
 
         if first_added_attr == TODAY:
             f.write(str(mail_attr) + '\n')
@@ -359,4 +365,4 @@ def main():
         generate_membership_list()
 
 if __name__ == "__main__":
-    main()
+    main()
\ No newline at end of file


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]