VMM/{,cli/}handler: user_add: Check earlier if the account exists. v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Wed, 04 Aug 2010 18:41:46 +0000
branchv0.6.x
changeset 337 150ddcc8b315
parent 336 d24c2ea39710
child 338 45834dcc280e
VMM/{,cli/}handler: user_add: Check earlier if the account exists.
VirtualMailManager/cli/handler.py
VirtualMailManager/handler.py
--- a/VirtualMailManager/cli/handler.py	Tue Aug 03 23:33:46 2010 +0000
+++ b/VirtualMailManager/cli/handler.py	Wed Aug 04 18:41:46 2010 +0000
@@ -14,7 +14,7 @@
 from VirtualMailManager.handler import Handler
 from VirtualMailManager.cli import read_pass
 from VirtualMailManager.cli.config import CliConfig as Cfg
-from VirtualMailManager.constants import INVALID_SECTION
+from VirtualMailManager.constants import ACCOUNT_EXISTS, INVALID_SECTION
 
 _ = lambda msg: msg
 
@@ -68,11 +68,17 @@
                            INVALID_SECTION)
 
     def user_add(self, emailaddress, password=None):
-        """Prefix the parent user_add() with the interactive password
+        """Override the parent user_add() - add the interactive password
         dialog."""
+        acc = self._get_account(emailaddress)
+        if acc:
+            raise VMMError(_(u"The account '%s' already exists.") %
+                           acc.address, ACCOUNT_EXISTS)
         if password is None:
             password = read_pass()
-        super(CliHandler, self).user_add(emailaddress, password)
+        acc.set_password(password)
+        acc.save()
+        self._make_account_dirs(acc)
 
     def user_password(self, emailaddress, password=None):
         """Prefix the parent user_password() with the interactive password
--- a/VirtualMailManager/handler.py	Tue Aug 03 23:33:46 2010 +0000
+++ b/VirtualMailManager/handler.py	Wed Aug 04 18:41:46 2010 +0000
@@ -271,6 +271,21 @@
         os.mkdir('%s' % uid, self._cfg.dget('account.directory_mode'))
         os.chown('%s' % uid, uid, account.gid)
 
+    def _make_account_dirs(self, account):
+        """Create all necessary directories for the account."""
+        oldpwd = os.getcwd()
+        self._make_home(account)
+        mailbox = new_mailbox(account)
+        mailbox.create()
+        folders = self._cfg.dget('mailbox.folders').split(':')
+        if any(folders):
+            bad = mailbox.add_boxes(folders,
+                                    self._cfg.dget('mailbox.subscribe'))
+            if bad:
+                self._warnings.append(_(u"Skipped mailbox folders:") +
+                                      '\n\t- ' + '\n\t- '.join(bad))
+        os.chdir(oldpwd)
+
     def _delete_home(self, domdir, uid, gid):
         """Delete a user's home directory.
 
@@ -494,20 +509,12 @@
     def user_add(self, emailaddress, password):
         """Wrapper around Account.set_password() and Account.save()."""
         acc = self._get_account(emailaddress)
+        if acc:
+            raise VMMError(_(u"The account '%s' already exists.") %
+                           acc.address, ACCOUNT_EXISTS)
         acc.set_password(password)
         acc.save()
-        oldpwd = os.getcwd()
-        self._make_home(acc)
-        mailbox = new_mailbox(acc)
-        mailbox.create()
-        folders = self._cfg.dget('mailbox.folders').split(':')
-        if any(folders):
-            bad = mailbox.add_boxes(folders,
-                                    self._cfg.dget('mailbox.subscribe'))
-            if bad:
-                self._warnings.append(_(u"Skipped mailbox folders:") +
-                                      '\n\t- ' + '\n\t- '.join(bad))
-        os.chdir(oldpwd)
+        self._make_account_dirs(acc)
 
     def alias_add(self, aliasaddress, *targetaddresses):
         """Creates a new `Alias` entry for the given *aliasaddress* with