From cab6f70ec8e9a162f7aae9e248462c0e22d2196b Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Thu, 29 Sep 2005 13:47:47 +0000 Subject: [PATCH] - 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 --- backend/gvadm/DomainTools.py | 42 +++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/backend/gvadm/DomainTools.py b/backend/gvadm/DomainTools.py index 0235261..45c86f4 100644 --- a/backend/gvadm/DomainTools.py +++ b/backend/gvadm/DomainTools.py @@ -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')