X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=UDLdap.py;h=f19703fd94a14cecbd3191e9507c9050421583a7;hb=f8181b0dfa59390433a8329e61f0cfbc2f720644;hp=97fd2fbdcb665800a3746b3f6c79f32a7d10bb2e;hpb=ab9b6db10f47351659f37b0d75acfad2d40d2493;p=mirror%2Fuserdir-ldap.git diff --git a/UDLdap.py b/UDLdap.py index 97fd2fb..f19703f 100644 --- a/UDLdap.py +++ b/UDLdap.py @@ -27,23 +27,27 @@ class Account: def __init__(self, dn, attributes): self.dn = dn self.attributes = attributes + self.cache = {} def __getitem__(self, key): + if key in self.cache: + return self.cache[key] + if key in self.attributes: if key in self.array_values: - return self.attributes[key] - - if not len(self.attributes[key]) == 1: + self.cache[key] = self.attributes[key] + elif not len(self.attributes[key]) == 1: raise ValueError, 'non-array value has not exactly one value' - - if key in self.int_values: - return int(self.attributes[key][0]) + elif key in self.int_values: + self.cache[key] = int(self.attributes[key][0]) else: - return self.attributes[key][0] + self.cache[key] = self.attributes[key][0] elif key in self.defaults: - return self.defaults[key] + self.cache[key] = self.defaults[key] else: - raise IndexError + raise IndexError, "No such key: %s (dn: %s)"%(key, self.dn) + + return self.cache[key] def __contains__(self, key): return key in self.attributes @@ -55,15 +59,17 @@ class Account: # not locked locked, just reset to something invalid like {crypt}*SSLRESET* is still active def pw_active(self): - if self['userPassword'] == '{crypt}*LK*': + if not 'userPassword' in self: + return False + if self['userPassword'].upper() == '{CRYPT}*LK*': return False - if self['userPassword'].startswith("{crypt}!"): + if self['userPassword'].upper().startswith("{CRYPT}!"): return False return True def get_password(self): p = self['userPassword'] - if not p.startswith('{crypt}') or len(p) > 50: + if not p.upper().startswith('{CRYPT}') or len(p) > 50: return p else: return p[7:] @@ -81,6 +87,9 @@ class Account: def is_active_user(self): return self['accountStatus'] == 'active' and self.numkeys() != 0 + def is_guest_account(self): + return self['gidNumber'] == 60000 + def latitude_dec(self, anonymized=False): return userdir_ldap.DecDegree(self['latitude'], anonymized) def longitude_dec(self, anonymized=False):