- start development of methods for updating POP3/IMAP passwords
git-svn-id: file:///home/www/usr01/svn/gnuviechadmin/gnuviech.info/gnuviechadmin/trunk@125 a67ec6bc-e5d5-0310-a910-815c51eb3124
This commit is contained in:
parent
cdbb1a8e2b
commit
cab6f70ec8
1 changed files with 41 additions and 1 deletions
|
@ -20,6 +20,15 @@ class NoSysuserForDomain(Exception):
|
|||
def __str__(self):
|
||||
return repr("No system user for domain %s" % (self.domain))
|
||||
|
||||
class InvalidPopUser(Exception):
|
||||
"""This exception is raised if an invalid POP3/IMAP user has been specified."""
|
||||
def __init__(self, domain, username):
|
||||
self.domain = domain
|
||||
self.username = username
|
||||
|
||||
def __str__(self):
|
||||
return "Invalid POP3/IMAP user %s in domain %s." % (self.username, self.domain)
|
||||
|
||||
class Domain:
|
||||
"""A Domain representation object with service methods."""
|
||||
def __init__(self, domain):
|
||||
|
@ -43,7 +52,7 @@ class Domain:
|
|||
raise InvalidDomain(self)
|
||||
|
||||
def __str__(self):
|
||||
return repr(self.domain)
|
||||
return str(self.domain)
|
||||
|
||||
def getSysuser(self):
|
||||
"""Gets the system user id of the domain."""
|
||||
|
@ -120,3 +129,34 @@ Password: %(password)s""" % {'domain': self.domain,
|
|||
s.connect()
|
||||
s.sendmail(Settings.mailsender, [Settings.mailreceiver], themail.as_string())
|
||||
s.close()
|
||||
|
||||
def listPopUsers(self):
|
||||
sysuser = self.getSysuser()
|
||||
|
||||
cr = self.cnx.cursor()
|
||||
cr.execute("SELECT id FROM mailpasswd WHERE id LIKE %(user)s" % {
|
||||
'user': psycopg.QuotedString(sysuser + '%')})
|
||||
self.cnx.commit()
|
||||
|
||||
result = cr.fetchall()
|
||||
return [line[0] for line in result]
|
||||
|
||||
def hasPopUser(self, username):
|
||||
"""Checks whether the specified POP3/IMAP user exists in the domain."""
|
||||
return ([user for user in domain.listPopUsers() if (user == username)])
|
||||
|
||||
def updatePopPassword(self, username, password=PasswordTools.generate_password()):
|
||||
"""Updates the password of the given POP3/IMAP user."""
|
||||
if self.hasPopUser(username):
|
||||
print("will update password of user %s to %s" % (username, password))
|
||||
else:
|
||||
raise InvalidPopUser(self, username)
|
||||
|
||||
if __name__ == '__main__':
|
||||
domain = Domain('efs-sohland.de')
|
||||
# check for not existing user
|
||||
try:
|
||||
domain.updatePopPassword('usr03p2', 'test')
|
||||
except InvalidPopUser, ipu:
|
||||
print ipu
|
||||
domain.updatePopPassword('usr05p2')
|
||||
|
|
Loading…
Reference in a new issue