From 06c1c7f2f762393ae885e8f5066c347b15674144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Casaj=C3=BAs?= Date: Wed, 20 Jul 2022 11:09:22 +0200 Subject: [PATCH] Restrict the number of free alias for new free users (#1155) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Restrict the number of free alias for new free users * Fix test * Make flag reverse Co-authored-by: Adrià Casajús --- app/api/views/user_info.py | 9 ++++++++- app/config.py | 2 ++ app/models.py | 13 ++++++++++++- docs/api.md | 3 ++- tests/api/test_user_info.py | 2 ++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/api/views/user_info.py b/app/api/views/user_info.py index 1a2a5053..3a8ce34c 100644 --- a/app/api/views/user_info.py +++ b/app/api/views/user_info.py @@ -18,6 +18,7 @@ def user_to_dict(user: User) -> dict: "is_premium": user.is_premium(), "email": user.email, "in_trial": user.in_trial(), + "max_alias_free_plan": user.max_alias_for_free_account(), } if user.profile_picture_id: @@ -33,6 +34,13 @@ def user_to_dict(user: User) -> dict: def user_info(): """ Return user info given the api-key + + Output as json + - name + - is_premium + - email + - in_trial + - max_alias_free """ user = g.user @@ -46,7 +54,6 @@ def update_user_info(): Input - profile_picture (optional): base64 of the profile picture. Set to null to remove the profile picture - name (optional) - """ user = g.user data = request.get_json() or {} diff --git a/app/config.py b/app/config.py index d7e29d7d..43e2b7a0 100644 --- a/app/config.py +++ b/app/config.py @@ -97,6 +97,8 @@ except Exception: print("MAX_NB_EMAIL_FREE_PLAN is not set, use 5 as default value") MAX_NB_EMAIL_FREE_PLAN = 5 +MAX_NB_EMAIL_OLD_FREE_PLAN = int(os.environ.get("MAX_NB_EMAIL_OLD_FREE_PLAN", 15)) + # maximum number of directory a premium user can create MAX_NB_DIRECTORY = 50 MAX_NB_SUBDOMAIN = 5 diff --git a/app/models.py b/app/models.py index f3ec160d..d7716d95 100644 --- a/app/models.py +++ b/app/models.py @@ -315,6 +315,7 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle): FLAG_FREE_DISABLE_CREATE_ALIAS = 1 << 0 FLAG_CREATED_FROM_PARTNER = 1 << 1 + FLAG_FREE_OLD_ALIAS_LIMIT = 1 << 2 email = sa.Column(sa.String(256), unique=True, nullable=False) @@ -748,6 +749,15 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle): # endregion + def max_alias_for_free_account(self) -> int: + if ( + self.FLAG_FREE_OLD_ALIAS_LIMIT + == self.flags & self.FLAG_FREE_OLD_ALIAS_LIMIT + ): + return config.MAX_NB_EMAIL_OLD_FREE_PLAN + else: + return config.MAX_NB_EMAIL_FREE_PLAN + def can_create_new_alias(self) -> bool: """ Whether user can create a new alias. User can't create a new alias if @@ -760,7 +770,8 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle): return True else: return ( - Alias.filter_by(user_id=self.id).count() < config.MAX_NB_EMAIL_FREE_PLAN + Alias.filter_by(user_id=self.id).count() + < self.max_alias_for_free_account() ) def profile_picture_url(self): diff --git a/docs/api.md b/docs/api.md index 995c8b1d..a501123a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -207,7 +207,8 @@ Output: if api key is correct, return a json with user name and whether user is "is_premium": false, "email": "john@wick.com", "in_trial": true, - "profile_picture_url": "https://profile.png" + "profile_picture_url": "https://profile.png", + "max_alias_free_plan": 5, } ``` diff --git a/tests/api/test_user_info.py b/tests/api/test_user_info.py index f5ebbaa6..f7728af1 100644 --- a/tests/api/test_user_info.py +++ b/tests/api/test_user_info.py @@ -1,5 +1,6 @@ from flask import url_for +from app import config from app.models import User from tests.api.utils import get_new_user_and_api_key from tests.utils import login @@ -19,6 +20,7 @@ def test_user_in_trial(flask_client): "email": user.email, "in_trial": True, "profile_picture_url": None, + "max_alias_free_plan": config.MAX_NB_EMAIL_FREE_PLAN, }