Update changelog
[mirror/userdir-ldap.git] / UDLdap.py
index 2e45092..97fd2fb 100644 (file)
--- a/UDLdap.py
+++ b/UDLdap.py
@@ -1,6 +1,8 @@
 import ldap
 import time
+import datetime
 import userdir_ldap
+import sys
 
 class Account:
     array_values = ['objectClass', 'keyFingerPrint', 'mailWhitelist', 'mailRBL',
@@ -16,11 +18,9 @@ class Account:
     def from_search(ldap_connection, base, user):
         searchresult = ldap_connection.search_s(base, ldap.SCOPE_SUBTREE, 'uid=%s'%(user))
         if len(searchresult) < 1:
-            sys.stderr.write("No such user: %s\n"%(user))
-            return
+            raise IndexError, "No such user: %s\n"%(user)
         elif len(searchresult) > 1:
-            sys.stderr.write("More than one hit when getting %s\n"%(user))
-            return
+            raise IndexError, "More than one hit when getting %s\n"%(user)
         else:
             return Account(searchresult[0][0], searchresult[0][1])
 
@@ -110,6 +110,24 @@ class Account:
         tokens.append(mailbox)
         return ' '.join(tokens)
 
+    def is_allowed_by_hostacl(self, host):
+        if not 'allowedHost' in self: return False
+        if host in self['allowedHost']: return True
+        # or maybe it's a date limited ACL
+        for entry in self['allowedHost']:
+            list = entry.split(None,1)
+            if len(list) == 1: continue
+            (h, expire) = list
+            if host != h: continue
+            try:
+                parsed = datetime.datetime.strptime(expire, '%Y%m%d')
+            except ValueError:
+                print >>sys.stderr, "Cannot parse expiry date in '%s' in hostACL entry for %s."%(entry, self['uid'])
+                return False
+            return parsed >= datetime.datetime.now()
+        return False
+
+
 # vim:set et:
 # vim:set ts=4:
 # vim:set shiftwidth=4: