mirror of
https://github.com/simple-login/app.git
synced 2025-02-25 16:23:16 +08:00
replace get_suffix() by User.get_random_alias_suffix()
This commit is contained in:
parent
09d00df363
commit
01815b9153
3 changed files with 17 additions and 21 deletions
|
@ -23,7 +23,6 @@ from app.models import (
|
|||
AliasMailbox,
|
||||
DomainDeletedAlias,
|
||||
)
|
||||
from app.utils import get_suffix
|
||||
|
||||
signer = TimestampSigner(CUSTOM_ALIAS_SECRET)
|
||||
|
||||
|
@ -54,7 +53,7 @@ def get_available_suffixes(user: User) -> [SuffixInfo]:
|
|||
# for each user domain, generate both the domain and a random suffix version
|
||||
for custom_domain in user_custom_domains:
|
||||
if custom_domain.random_prefix_generation:
|
||||
suffix = "." + get_suffix(user) + "@" + custom_domain.domain
|
||||
suffix = "." + user.get_random_alias_suffix() + "@" + custom_domain.domain
|
||||
suffix_info = SuffixInfo(True, suffix, signer.sign(suffix).decode(), False)
|
||||
if user.default_alias_custom_domain_id == custom_domain.id:
|
||||
suffixes.insert(0, suffix_info)
|
||||
|
@ -77,7 +76,7 @@ def get_available_suffixes(user: User) -> [SuffixInfo]:
|
|||
# then SimpleLogin domain
|
||||
for sl_domain in user.get_sl_domains():
|
||||
suffix = (
|
||||
("" if DISABLE_ALIAS_SUFFIX else "." + get_suffix(user))
|
||||
("" if DISABLE_ALIAS_SUFFIX else "." + user.get_random_alias_suffix())
|
||||
+ "@"
|
||||
+ sl_domain.domain
|
||||
)
|
||||
|
|
|
@ -24,6 +24,7 @@ from app.config import (
|
|||
FIRST_ALIAS_DOMAIN,
|
||||
DISABLE_ONBOARDING,
|
||||
UNSUBSCRIBER,
|
||||
ALIAS_RANDOM_SUFFIX_LENGTH,
|
||||
)
|
||||
from app.errors import AliasInTrashError
|
||||
from app.extensions import db
|
||||
|
@ -35,7 +36,7 @@ from app.utils import (
|
|||
random_string,
|
||||
random_words,
|
||||
sanitize_email,
|
||||
get_suffix,
|
||||
random_word,
|
||||
)
|
||||
|
||||
|
||||
|
@ -758,6 +759,17 @@ class User(db.Model, ModelMixin, UserMixin, PasswordOracle):
|
|||
> 0
|
||||
)
|
||||
|
||||
def get_random_alias_suffix(self):
|
||||
"""Get random suffix for an alias based on user's preference.
|
||||
|
||||
|
||||
Returns:
|
||||
str: the random suffix generated
|
||||
"""
|
||||
if self.random_alias_suffix == AliasSuffixEnum.random_string.value:
|
||||
return random_string(ALIAS_RANDOM_SUFFIX_LENGTH, include_digits=True)
|
||||
return random_word()
|
||||
|
||||
def __repr__(self):
|
||||
return f"<User {self.id} {self.name} {self.email}>"
|
||||
|
||||
|
@ -1147,7 +1159,7 @@ class Alias(db.Model, ModelMixin):
|
|||
|
||||
# find the right suffix - avoid infinite loop by running this at max 1000 times
|
||||
for i in range(1000):
|
||||
suffix = get_suffix(user)
|
||||
suffix = user.get_random_alias_suffix()
|
||||
email = f"{prefix}.{suffix}@{FIRST_ALIAS_DOMAIN}"
|
||||
|
||||
if not cls.get_by(email=email) and not DeletedAlias.get_by(email=email):
|
||||
|
|
17
app/utils.py
17
app/utils.py
|
@ -4,9 +4,8 @@ import urllib.parse
|
|||
|
||||
from unidecode import unidecode
|
||||
|
||||
from .config import WORDS_FILE_PATH, ALIAS_RANDOM_SUFFIX_LENGTH
|
||||
from .config import WORDS_FILE_PATH
|
||||
from .log import LOG
|
||||
from .models import User, AliasSuffixEnum
|
||||
|
||||
with open(WORDS_FILE_PATH) as f:
|
||||
LOG.d("load words file: %s", WORDS_FILE_PATH)
|
||||
|
@ -17,20 +16,6 @@ def random_word():
|
|||
return random.choice(_words)
|
||||
|
||||
|
||||
def get_suffix(user: User) -> str:
|
||||
"""Get random suffix for an alias based on user's preference.
|
||||
|
||||
Args:
|
||||
user (User): the user who is trying to create an alias
|
||||
|
||||
Returns:
|
||||
str: the random suffix generated
|
||||
"""
|
||||
if user.random_alias_suffix == AliasSuffixEnum.random_string.value:
|
||||
return random_string(ALIAS_RANDOM_SUFFIX_LENGTH, include_digits=True)
|
||||
return random_word()
|
||||
|
||||
|
||||
def word_exist(word):
|
||||
return word in _words
|
||||
|
||||
|
|
Loading…
Reference in a new issue