Let Account have a constructor that is more useful in generate
[mirror/userdir-ldap.git] / UDLdap.py
1
2 class Account:
3     def __init__(self, user):
4         searchresult = lc.search_s(BaseDn,ldap.SCOPE_SUBTREE, 'uid=%s'%(user))
5         if len(searchresult) < 1:
6             sys.stderr.write("No such user: %s\n"%(user))
7             return
8         elif len(searchresult) > 1:
9             sys.stderr.write("More than one hit when getting %s\n"%(user))
10             return
11
12         self.dn, self.attributes = searchresult[0]
13
14
15     def has_mail(self):
16         if 'mailDisableMessage' in self.attributes:
17             return False
18         return True
19
20     # not locked locked,  just reset to something invalid like {crypt}*SSLRESET* is still active
21     def pw_active(self):
22         if self.attributes['userPassword'][0] == '{crypt}*LK*':
23             return False
24         return True
25
26     # not expired
27     def shadow_active(self):
28         if 'shadowExpire' in self.attributes and \
29             int(self.attributes['shadowExpire'][0]) < (time.time() / 3600 / 24):
30             return False
31         return True
32
33     def numkeys(self):
34         if 'keyFingerPrint' in self.attributes:
35             return len(self.attributes['keyFingerPrint'])
36         return 0
37
38     def account_status(self):
39         if 'accountStatus' in self.attributes:
40             return self.attributes['accountStatus'][0]
41         return 'active'
42
43
44     def verbose_status(self):
45         status = []
46         status.append('mail: %s'  %(['disabled', 'active'][ self.has_mail() ]))
47         status.append('pw: %s'    %(['locked', 'active'][ self.pw_active() ]))
48         status.append('shadow: %s'%(['expired', 'active'][ self.shadow_active() ]))
49         status.append('keys: %d'  %( self.numkeys() ))
50         status.append('status: %s'%( self.account_status() ))
51
52         return '(%s)'%(', '.join(status))
53
54     def get_dn(self):
55         return self.dn
56
57 # vim:set et:
58 # vim:set ts=4:
59 # vim:set shiftwidth=4: