Repository: modoboa/modoboa (language: python)

Issue:
Alias is marked as internal when it shouldn’t
# Impacted versions

* Modoboa: 2.0.5

# Steps to reproduce

Create two Simple Users. Edit one of the account, go to "Courrier" tab and add an alias there. Now, if you write to this email account, the email is received on both mailboxes. But the alias is not visible anywhere. We can’t change it, we can’t delete it.

# Expected behavior

Please make the alias visible in the interface.




Discussion / hints:
Hi,

Do you think we should allow for an alias to mix with a "SimpleUser" though ? 
Perhaps it would be better to simply lock it and only allow creating : account 1, account 2 and alias1 linked to both account ? 

Fix patch:
diff --git a/modoboa/admin/forms/account.py b/modoboa/admin/forms/account.py
index d08d3fff4..8fd2572b6 100644
--- a/modoboa/admin/forms/account.py
+++ b/modoboa/admin/forms/account.py
@@ -403,6 +403,7 @@ def create_mailbox(self, user, account):
 
     def _update_aliases(self, user, account):
         """Update mailbox aliases."""
+        # 1. We check if existing aliases were removed...
         qset = self.mb.aliasrecipient_set.select_related("alias").filter(
             alias__internal=False)
         for ralias in qset:
@@ -414,22 +415,26 @@ def _update_aliases(self, user, account):
                 alias.delete()
             else:
                 self.aliases.remove(ralias.alias.address)
+        # 2. Nothing left to do, return
         if not self.aliases:
             return
+        # 3. Check limits at all levels
         core_signals.can_create_object.send(
             self.__class__, context=user, klass=models.Alias,
             count=len(self.aliases))
         core_signals.can_create_object.send(
             self.__class__, context=self.mb.domain,
             object_type="mailbox_aliases", count=len(self.aliases))
+        # 4. Create / update aliases
         for alias in self.aliases:
             if self.mb.aliasrecipient_set.select_related("alias").filter(
-                    alias__address=alias).exists():
+                    alias__address=alias, alias__internal=False).exists():
                 continue
             local_part, domname = split_mailbox(alias)
             al, _ = models.Alias.objects.get_or_create(
                 address=alias,
                 domain=models.Domain.objects.get(name=domname),
+                internal=False,
                 defaults={'enabled': account.is_active},
             )
             al.add_recipients([self.mb.full_address])


Test patch:
diff --git a/modoboa/admin/tests/test_account.py b/modoboa/admin/tests/test_account.py
index d47faf17e..6f1c7ab88 100644
--- a/modoboa/admin/tests/test_account.py
+++ b/modoboa/admin/tests/test_account.py
@@ -135,6 +135,26 @@ def test_create_account_with_existing_alias(self):
         self.ajax_post(reverse("admin:account_add"), values)
         self.assertTrue(alias.aliasrecipient_set.count() == 2)
 
+    def test_create_alias_on_existing_account(self):
+        values = {
+            "email": "tester@test.com",
+            "username": "tester@test.com",
+            "role": "SimpleUsers",
+            "random_password": "on",
+            "quota_act": True,
+            "is_active": True,
+            "stepid": "step2",
+            "aliases": "user@test.com",
+        }
+        self.ajax_post(reverse("admin:account_add"), values)
+        # Check that internal alias has not been updated
+        alias = models.Alias.objects.get(
+            address="user@test.com", internal=True)
+        self.assertTrue(alias.aliasrecipient_set.count() == 1)
+        alias = models.Alias.objects.get(
+            address="user@test.com", internal=False)
+        self.assertTrue(alias.aliasrecipient_set.count() == 1)
+
     def test_create_disabled_account(self):
         """Check if account and alias are disabled."""
         values = {