Some improvement over the last path
[mirror/userdir-ldap.git] / UDLdap.py
1 import ldap
2 import time
3 import userdir_ldap
4
5 class Account:
6     array_values = ['keyFingerPrint']
7     int_values = ['shadowExpire']
8     defaults = {
9                  'accountStatus': 'active',
10                  'keyFingerPrint': []
11                }
12
13     @staticmethod
14     def from_search(ldap_connection, base, user):
15         searchresult = ldap_connection.search_s(base, ldap.SCOPE_SUBTREE, 'uid=%s'%(user))
16         if len(searchresult) < 1:
17             sys.stderr.write("No such user: %s\n"%(user))
18             return
19         elif len(searchresult) > 1:
20             sys.stderr.write("More than one hit when getting %s\n"%(user))
21             return
22         else:
23             return Account(searchresult[0][0], searchresult[0][1])
24
25     def __init__(self, dn, attributes):
26         self.dn = dn
27         self.attributes = attributes
28
29     def __getitem__(self, key):
30         if key in self.attributes:
31             if key in self.array_values:
32                 return self.attributes[key]
33             elif key in self.int_values:
34                 return int(self.attributes[key][0])
35             else:
36                 return self.attributes[key][0]
37         elif key in self.defaults:
38             return self.defaults[key]
39         else:
40             raise IndexError
41
42     def __contains__(self, key):
43         return key in self.attributes
44
45     def has_mail(self):
46         if 'mailDisableMessage' in self.attributes:
47             return False
48         return True
49
50     # not locked locked,  just reset to something invalid like {crypt}*SSLRESET* is still active
51     def pw_active(self):
52         if self['userPassword'] == '{crypt}*LK*':
53             return False
54         return True
55
56     # not expired
57     def shadow_active(self):
58         if 'shadowExpire' in self and \
59             self['shadowExpire'] < (time.time() / 3600 / 24):
60             return False
61         return True
62
63     def numkeys(self):
64         return len(self['keyFingerPrint'])
65
66     def latitude_dec(self, anonymized=False):
67         return userdir_ldap.DecDegree(self['latitude'], anonymized)
68     def longitude_dec(self, anonymized=False):
69         return userdir_ldap.DecDegree(self['longitude'], anonymized)
70
71     def verbose_status(self):
72         status = []
73         status.append('mail: %s'  %(['disabled', 'active'][ self.has_mail() ]))
74         status.append('pw: %s'    %(['locked', 'active'][ self.pw_active() ]))
75         status.append('shadow: %s'%(['expired', 'active'][ self.shadow_active() ]))
76         status.append('keys: %d'  %( self.numkeys() ))
77         status.append('status: %s'%( self['accountStatus'] ))
78
79         return '(%s)'%(', '.join(status))
80
81     def get_dn(self):
82         return self.dn
83
84 # vim:set et:
85 # vim:set ts=4:
86 # vim:set shiftwidth=4: