feat: email api add password param

This commit is contained in:
Joe 2024-09-26 14:30:06 +08:00
parent 0cbef254cd
commit 5066233cd4
3 changed files with 25 additions and 10 deletions

View File

@ -31,18 +31,23 @@ class ForgotPasswordSendEmailApi(Resource):
def post(self):
parser = reqparse.RequestParser()
parser.add_argument("email", type=email, required=True, location="json")
parser.add_argument("language", type=str, required=False, location="json")
args = parser.parse_args()
account = Account.query.filter_by(email=args["email"]).first()
token = None
if account is None:
if dify_config.ALLOW_REGISTER:
token = AccountService.send_reset_password_email(email=args["email"])
token = AccountService.send_reset_password_email(
email=args["email"], language=args["language"] or "en-US"
)
else:
raise NotAllowedRegister()
elif account:
try:
token = AccountService.send_reset_password_email(account=account, email=args["email"])
token = AccountService.send_reset_password_email(
account=account, email=args["email"], language=args["language"] or "en-US"
)
except RateLimitExceededError:
logging.warning(f"Rate limit exceeded for email: {args['email']}")
raise PasswordResetRateLimitExceededError()

View File

@ -81,16 +81,19 @@ class ResetPasswordSendEmailApi(Resource):
def post(self):
parser = reqparse.RequestParser()
parser.add_argument("email", type=email, required=True, location="json")
parser.add_argument("language", type=str, required=False, location="json")
args = parser.parse_args()
account = AccountService.get_user_through_email(args["email"])
if account is None:
if dify_config.ALLOW_REGISTER:
token = AccountService.send_reset_password_email(email=args["email"])
token = AccountService.send_reset_password_email(
email=args["email"], language=args["language"] or "en-US"
)
else:
raise NotAllowedRegister()
else:
token = AccountService.send_reset_password_email(account=account)
token = AccountService.send_reset_password_email(account=account, language=args["language"])
return {"result": "success", "data": token}
@ -100,16 +103,19 @@ class EmailCodeLoginSendEmailApi(Resource):
def post(self):
parser = reqparse.RequestParser()
parser.add_argument("email", type=email, required=True, location="json")
parser.add_argument("language", type=str, required=False, location="json")
args = parser.parse_args()
account = AccountService.get_user_through_email(args["email"])
if account is None:
if dify_config.ALLOW_REGISTER:
token = AccountService.send_email_code_login_email(email=args["email"])
token = AccountService.send_email_code_login_email(
email=args["email"], language=args["language"] or "en-US"
)
else:
raise NotAllowedRegister()
else:
token = AccountService.send_email_code_login_email(account=account)
token = AccountService.send_email_code_login_email(account=account, language=args["language"])
return {"result": "success", "data": token}

View File

@ -255,9 +255,11 @@ class AccountService:
return AccountService.load_user(account_id)
@classmethod
def send_reset_password_email(cls, account: Optional[Account] = None, email: Optional[str] = None):
def send_reset_password_email(
cls, account: Optional[Account] = None, email: Optional[str] = None, language: Optional[str] = "en-US"
):
account_email = account.email if account else email
account_language = account.interface_language if account else languages[0]
account_language = account.interface_language if account else language
if cls.reset_password_rate_limiter.is_rate_limited(account_email):
raise RateLimitExceededError(f"Rate limit exceeded for email: {account_email}. Please try again later.")
@ -283,7 +285,9 @@ class AccountService:
return TokenManager.get_token_data(token, "reset_password")
@classmethod
def send_email_code_login_email(cls, account: Optional[Account] = None, email: Optional[str] = None):
def send_email_code_login_email(
cls, account: Optional[Account] = None, email: Optional[str] = None, language: Optional[str] = "en-US"
):
if cls.email_code_login_rate_limiter.is_rate_limited(email):
raise RateLimitExceededError(f"Rate limit exceeded for email: {email}. Please try again later.")
@ -292,7 +296,7 @@ class AccountService:
account=account, email=email, token_type="email_code_login", additional_data={"code": code}
)
send_email_code_login_mail_task.delay(
language=account.interface_language if account else languages[0],
language=account.interface_language if account else language,
to=account.email if account else email,
code=code,
)