Skip to content

Commit 06c1c7f

Browse files
authored
Restrict the number of free alias for new free users (simple-login#1155)
* Restrict the number of free alias for new free users * Fix test * Make flag reverse Co-authored-by: Adrià Casajús <[email protected]>
1 parent 8773ed1 commit 06c1c7f

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

app/api/views/user_info.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def user_to_dict(user: User) -> dict:
1818
"is_premium": user.is_premium(),
1919
"email": user.email,
2020
"in_trial": user.in_trial(),
21+
"max_alias_free_plan": user.max_alias_for_free_account(),
2122
}
2223

2324
if user.profile_picture_id:
@@ -33,6 +34,13 @@ def user_to_dict(user: User) -> dict:
3334
def user_info():
3435
"""
3536
Return user info given the api-key
37+
38+
Output as json
39+
- name
40+
- is_premium
41+
- email
42+
- in_trial
43+
- max_alias_free
3644
"""
3745
user = g.user
3846

@@ -46,7 +54,6 @@ def update_user_info():
4654
Input
4755
- profile_picture (optional): base64 of the profile picture. Set to null to remove the profile picture
4856
- name (optional)
49-
5057
"""
5158
user = g.user
5259
data = request.get_json() or {}

app/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ def sl_getenv(env_var: str, default_factory: Callable = None):
9797
print("MAX_NB_EMAIL_FREE_PLAN is not set, use 5 as default value")
9898
MAX_NB_EMAIL_FREE_PLAN = 5
9999

100+
MAX_NB_EMAIL_OLD_FREE_PLAN = int(os.environ.get("MAX_NB_EMAIL_OLD_FREE_PLAN", 15))
101+
100102
# maximum number of directory a premium user can create
101103
MAX_NB_DIRECTORY = 50
102104
MAX_NB_SUBDOMAIN = 5

app/models.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle):
315315

316316
FLAG_FREE_DISABLE_CREATE_ALIAS = 1 << 0
317317
FLAG_CREATED_FROM_PARTNER = 1 << 1
318+
FLAG_FREE_OLD_ALIAS_LIMIT = 1 << 2
318319

319320
email = sa.Column(sa.String(256), unique=True, nullable=False)
320321

@@ -748,6 +749,15 @@ def upgrade_channel(self) -> str:
748749

749750
# endregion
750751

752+
def max_alias_for_free_account(self) -> int:
753+
if (
754+
self.FLAG_FREE_OLD_ALIAS_LIMIT
755+
== self.flags & self.FLAG_FREE_OLD_ALIAS_LIMIT
756+
):
757+
return config.MAX_NB_EMAIL_OLD_FREE_PLAN
758+
else:
759+
return config.MAX_NB_EMAIL_FREE_PLAN
760+
751761
def can_create_new_alias(self) -> bool:
752762
"""
753763
Whether user can create a new alias. User can't create a new alias if
@@ -760,7 +770,8 @@ def can_create_new_alias(self) -> bool:
760770
return True
761771
else:
762772
return (
763-
Alias.filter_by(user_id=self.id).count() < config.MAX_NB_EMAIL_FREE_PLAN
773+
Alias.filter_by(user_id=self.id).count()
774+
< self.max_alias_for_free_account()
764775
)
765776

766777
def profile_picture_url(self):

docs/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ Output: if api key is correct, return a json with user name and whether user is
207207
"is_premium": false,
208208
"email": "[email protected]",
209209
"in_trial": true,
210-
"profile_picture_url": "https://profile.png"
210+
"profile_picture_url": "https://profile.png",
211+
"max_alias_free_plan": 5,
211212
}
212213
```
213214

tests/api/test_user_info.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from flask import url_for
22

3+
from app import config
34
from app.models import User
45
from tests.api.utils import get_new_user_and_api_key
56
from tests.utils import login
@@ -19,6 +20,7 @@ def test_user_in_trial(flask_client):
1920
"email": user.email,
2021
"in_trial": True,
2122
"profile_picture_url": None,
23+
"max_alias_free_plan": config.MAX_NB_EMAIL_FREE_PLAN,
2224
}
2325

2426

0 commit comments

Comments
 (0)