Skip to content

Commit

Permalink
Preserve the flags when creating the user from the partner and do not…
Browse files Browse the repository at this point in the history
… deliver if user is pending deletion
  • Loading branch information
acasajus committed Jan 15, 2025
1 parent 0ccbbd7 commit 305f50a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
10 changes: 7 additions & 3 deletions app/admin_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ def __init__(self):
self.query: str

@staticmethod
def from_email(email: str) -> EmailSearchResult:
def from_request_email(email: str) -> EmailSearchResult:
output = EmailSearchResult()
output.query = email
alias = Alias.get_by(email=email)
Expand All @@ -803,7 +803,11 @@ def from_email(email: str) -> EmailSearchResult:
.all()
)
output.no_match = False
user = User.get_by(email=email)
try:
user_id = int(email)
user = User.get(user_id)
except ValueError:
user = User.get_by(email=email)
if user:
output.user = user
output.user_audit_log = (
Expand Down Expand Up @@ -912,7 +916,7 @@ def index(self):
email = request.args.get("email")
if email is not None and len(email) > 0:
email = email.strip()
search = EmailSearchResult.from_email(email)
search = EmailSearchResult.from_request_email(email)

return self.render(
"admin/email_search.html",
Expand Down
8 changes: 4 additions & 4 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class Fido(Base, ModelMixin):
class User(Base, ModelMixin, UserMixin, PasswordOracle):
__tablename__ = "users"

FLAG_DISABLE_CREATE_CONTACTS = 1 << 0
FLAG_FREE_DISABLE_CREATE_CONTACTS = 1 << 0
FLAG_CREATED_FROM_PARTNER = 1 << 1
FLAG_FREE_OLD_ALIAS_LIMIT = 1 << 2
FLAG_CREATED_ALIAS_FROM_PARTNER = 1 << 3
Expand Down Expand Up @@ -550,7 +550,7 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle):
# bitwise flags. Allow for future expansion
flags = sa.Column(
sa.BigInteger,
default=FLAG_DISABLE_CREATE_CONTACTS,
default=FLAG_FREE_DISABLE_CREATE_CONTACTS,
server_default="0",
nullable=False,
)
Expand Down Expand Up @@ -640,7 +640,7 @@ def create(cls, email, name="", password=None, from_partner=False, **kwargs):
# If the user is created from partner, do not notify
# nor give a trial
if from_partner:
user.flags = User.FLAG_CREATED_FROM_PARTNER
user.flags = user.flags | User.FLAG_CREATED_FROM_PARTNER
user.notification = False
user.trial_end = None
Job.create(
Expand Down Expand Up @@ -1189,7 +1189,7 @@ def get_random_alias_suffix(self, custom_domain: Optional["CustomDomain"] = None
def can_create_contacts(self) -> bool:
if self.is_premium():
return True
if self.flags & User.FLAG_DISABLE_CREATE_CONTACTS == 0:
if self.flags & User.FLAG_FREE_DISABLE_CREATE_CONTACTS == 0:
return True
return not config.DISABLE_CREATE_CONTACTS_FOR_FREE_USERS

Expand Down
11 changes: 11 additions & 0 deletions email_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,17 @@ def handle_forward(envelope, msg: Message, rcpt_to: str) -> List[Tuple[bool, str
else:
reply_to_contact = get_or_create_reply_to_contact(reply_to, alias, msg)

if alias.user.delete_on is not None:
LOG.d(f"user {user} is pending to be deleted. Do not forward")
EmailLog.create(
contact_id=contact.id,
user_id=contact.user_id,
blocked=True,
alias_id=contact.alias_id,
commit=True,
)
return [(True, status.E502)]

if not alias.enabled or contact.block_forward:
LOG.d("%s is disabled, do not forward", alias)
EmailLog.create(
Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ def test_create_contact_route_free_users(flask_client):
assert r.status_code == 201

# End trial and disallow for new free users. Config should allow it
user.flags = User.FLAG_DISABLE_CREATE_CONTACTS
user.flags = User.FLAG_FREE_DISABLE_CREATE_CONTACTS
Session.commit()
r = flask_client.post(
url_for("api.create_contact_route", alias_id=alias.id),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_contact_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_create_contact_free_user():
assert result.contact is not None
assert not result.contact.automatic_created
# Free users with the flag should be able to still create automatic emails
user.flags = User.FLAG_DISABLE_CREATE_CONTACTS
user.flags = User.FLAG_FREE_DISABLE_CREATE_CONTACTS
Session.flush()
result = create_contact(random_email(), alias, automatic_created=True)
assert result.error is None
Expand Down
13 changes: 13 additions & 0 deletions tests/test_email_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from email.message import EmailMessage
from typing import List

import arrow
import pytest
from aiosmtpd.smtp import Envelope

Expand Down Expand Up @@ -387,3 +388,15 @@ def test_preserve_headers(flask_client):
msg = sent_mails[0].msg
for header in headers_to_keep:
assert msg[header] == header + "keep"


def test_not_send_to_pending_to_delete_users(flask_client):
user = create_new_user()
alias = Alias.create_new_random(user)
user.delete_on = arrow.utcnow()
envelope = Envelope()
envelope.mail_from = "[email protected]"
envelope.rcpt_tos = [alias.email]
msg = EmailMessage()
result = email_handler.handle(envelope, msg)
assert result == status.E200

0 comments on commit 305f50a

Please sign in to comment.