Repository: modoboa/modoboa (language: python)

Issue:
Creating a disabled account imply an active (internal) alias
Testing against behavior similarly to #2120 it appears that creating a disabled account imply creation of an active (internal) alias.

# Impacted versions

* OS Type: Debian
* OS Version: Buster
* Database Type: PostgreSQL
* Database version: 11
* Modoboa: 1.17.3 (also on master branch)

# Steps to reproduce

* Create an account with Simple user role and disable it at the creation
* Send an email to this account

Here is a (failling) test case: https://github.com/fpoulain/modoboa/commit/05dda3cdcd27f6b4d651cf9622fa62ba03244a74

# Current behavior

```ipython
In [1]: from modoboa.core.models import User
In [2]: from modoboa.admin.models import Alias
In [3]: u = User.objects.get(email='tester@test.com')
In [4]: u.enabled
Out[4]: False
In [5]: a = Alias.objects.get(address=u.email)
In [6]: a.enabled
Out[6]: True
In [7]: a.internal
Out[7]: True
```

# Expected behavior

```ipython
In [1]: from modoboa.core.models import User
In [2]: from modoboa.admin.models import Alias
In [3]: u = User.objects.get(email='tester@test.com')
In [4]: u.enabled
Out[4]: False
In [5]: a = Alias.objects.get(address=u.email)
In [6]: a.enabled
Out[6]: False
In [7]: a.internal
Out[7]: True
```


Discussion / hints:
I may provide a PR if you confirm that the current behavior is a bug.
@fpoulain Does it mean you can send an email to this account anyway? What is the underlying issue here?
> Does it mean you can send an email to this account anyway?

Yes.
Then indeed, it's a bug.The internal alias state should be synchronized with the account one.
Ok, will try to fix it and opening a PR.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.


Fix patch:
diff --git a/modoboa/admin/handlers.py b/modoboa/admin/handlers.py
index 9ccac780a..0b3bef1ab 100644
--- a/modoboa/admin/handlers.py
+++ b/modoboa/admin/handlers.py
@@ -67,8 +67,13 @@ def manage_alias_for_mailbox(sender, instance, **kwargs):
     """Create or update a "self alias" for mailbox (catchall)."""
     if kwargs.get("created"):
         alias, created = models.Alias.objects.get_or_create(
-            address=instance.full_address, domain=instance.domain,
-            internal=True)
+            address=instance.full_address,
+            domain=instance.domain,
+            internal=True,
+            defaults={
+                "enabled": instance.user.enabled
+            }
+        )
         models.AliasRecipient.objects.create(
             address=instance.full_address, alias=alias, r_mailbox=instance)
         return


Test patch:
diff --git a/modoboa/admin/tests/test_account.py b/modoboa/admin/tests/test_account.py
index 963c9b7be..d47faf17e 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_disabled_account(self):
+        """Check if account and alias are disabled."""
+        values = {
+            "username": "tester@test.com",
+            "first_name": "Tester",
+            "last_name": "Toto",
+            "password1": "Toto1234",
+            "password2": "Toto1234",
+            "role": "SimpleUsers",
+            "quota_act": True,
+            "is_active": False,
+            "email": "tester@test.com",
+            "stepid": "step2"
+        }
+        self.ajax_post(reverse("admin:account_add"), values)
+        account = User.objects.get(username=values["username"])
+        self.assertEqual(account.enabled, False)
+        alias = models.Alias.objects.get(address=account.email)
+        self.assertEqual(alias.enabled, False)
+
     def test_aliases_update_on_disable(self):
         """Check if aliases are updated when account is disabled."""
         account = User.objects.get(username="user@test.com")