API - disable emails sending when EMAIL_URL is not initialized
This commit is contained in:
@ -40,25 +40,27 @@ from .utils.token import decode_user_token
|
||||
auth_blueprint = Blueprint('auth', __name__)
|
||||
|
||||
HEX_COLOR_REGEX = regex = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"
|
||||
NOT_FOUND_MESSAGE = 'the requested URL was not found on the server'
|
||||
|
||||
|
||||
def send_account_confirmation_email(user: User) -> None:
|
||||
ui_url = current_app.config['UI_URL']
|
||||
email_data = {
|
||||
'username': user.username,
|
||||
'fittrackee_url': ui_url,
|
||||
'operating_system': request.user_agent.platform, # type: ignore # noqa
|
||||
'browser_name': request.user_agent.browser, # type: ignore
|
||||
'account_confirmation_url': (
|
||||
f'{ui_url}/account-confirmation'
|
||||
f'?token={user.confirmation_token}'
|
||||
),
|
||||
}
|
||||
user_data = {
|
||||
'language': 'en',
|
||||
'email': user.email,
|
||||
}
|
||||
account_confirmation_email.send(user_data, email_data)
|
||||
if current_app.config['CAN_SEND_EMAILS']:
|
||||
ui_url = current_app.config['UI_URL']
|
||||
email_data = {
|
||||
'username': user.username,
|
||||
'fittrackee_url': ui_url,
|
||||
'operating_system': request.user_agent.platform, # type: ignore # noqa
|
||||
'browser_name': request.user_agent.browser, # type: ignore
|
||||
'account_confirmation_url': (
|
||||
f'{ui_url}/account-confirmation'
|
||||
f'?token={user.confirmation_token}'
|
||||
),
|
||||
}
|
||||
user_data = {
|
||||
'language': 'en',
|
||||
'email': user.email,
|
||||
}
|
||||
account_confirmation_email.send(user_data, email_data)
|
||||
|
||||
|
||||
@auth_blueprint.route('/auth/register', methods=['POST'])
|
||||
@ -505,7 +507,7 @@ def update_user_account(auth_user: User) -> Union[Dict, HttpResponse]:
|
||||
"""
|
||||
update authenticated user email and password
|
||||
|
||||
It sends emails:
|
||||
It sends emails if sending is enabled:
|
||||
|
||||
- Password change
|
||||
- Email change:
|
||||
@ -634,8 +636,12 @@ def update_user_account(auth_user: User) -> Union[Dict, HttpResponse]:
|
||||
try:
|
||||
if email_to_confirm != auth_user.email:
|
||||
if is_valid_email(email_to_confirm):
|
||||
auth_user.email_to_confirm = email_to_confirm
|
||||
auth_user.confirmation_token = secrets.token_urlsafe(30)
|
||||
if current_app.config['CAN_SEND_EMAILS']:
|
||||
auth_user.email_to_confirm = email_to_confirm
|
||||
auth_user.confirmation_token = secrets.token_urlsafe(30)
|
||||
else:
|
||||
auth_user.email = email_to_confirm
|
||||
auth_user.confirmation_token = None
|
||||
else:
|
||||
error_messages = 'email: valid email must be provided\n'
|
||||
|
||||
@ -652,44 +658,48 @@ def update_user_account(auth_user: User) -> Union[Dict, HttpResponse]:
|
||||
|
||||
db.session.commit()
|
||||
|
||||
ui_url = current_app.config['UI_URL']
|
||||
user_data = {
|
||||
'language': (
|
||||
'en' if auth_user.language is None else auth_user.language
|
||||
),
|
||||
'email': auth_user.email,
|
||||
}
|
||||
data = {
|
||||
'username': auth_user.username,
|
||||
'fittrackee_url': ui_url,
|
||||
'operating_system': request.user_agent.platform,
|
||||
'browser_name': request.user_agent.browser,
|
||||
}
|
||||
|
||||
if new_password is not None:
|
||||
password_change_email.send(user_data, data)
|
||||
|
||||
if (
|
||||
auth_user.email_to_confirm is not None
|
||||
and auth_user.email_to_confirm != auth_user.email
|
||||
):
|
||||
email_data = {
|
||||
**data,
|
||||
**{'new_email_address': email_to_confirm},
|
||||
if current_app.config['CAN_SEND_EMAILS']:
|
||||
ui_url = current_app.config['UI_URL']
|
||||
user_data = {
|
||||
'language': (
|
||||
'en' if auth_user.language is None else auth_user.language
|
||||
),
|
||||
'email': auth_user.email,
|
||||
}
|
||||
email_updated_to_current_address.send(user_data, email_data)
|
||||
|
||||
email_data = {
|
||||
**data,
|
||||
**{
|
||||
'email_confirmation_url': (
|
||||
f'{ui_url}/email-update'
|
||||
f'?token={auth_user.confirmation_token}'
|
||||
)
|
||||
},
|
||||
data = {
|
||||
'username': auth_user.username,
|
||||
'fittrackee_url': ui_url,
|
||||
'operating_system': request.user_agent.platform,
|
||||
'browser_name': request.user_agent.browser,
|
||||
}
|
||||
user_data = {**user_data, **{'email': auth_user.email_to_confirm}}
|
||||
email_updated_to_new_address.send(user_data, email_data)
|
||||
|
||||
if new_password is not None:
|
||||
password_change_email.send(user_data, data)
|
||||
|
||||
if (
|
||||
auth_user.email_to_confirm is not None
|
||||
and auth_user.email_to_confirm != auth_user.email
|
||||
):
|
||||
email_data = {
|
||||
**data,
|
||||
**{'new_email_address': email_to_confirm},
|
||||
}
|
||||
email_updated_to_current_address.send(user_data, email_data)
|
||||
|
||||
email_data = {
|
||||
**data,
|
||||
**{
|
||||
'email_confirmation_url': (
|
||||
f'{ui_url}/email-update'
|
||||
f'?token={auth_user.confirmation_token}'
|
||||
)
|
||||
},
|
||||
}
|
||||
user_data = {
|
||||
**user_data,
|
||||
**{'email': auth_user.email_to_confirm},
|
||||
}
|
||||
email_updated_to_new_address.send(user_data, email_data)
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
@ -1139,6 +1149,8 @@ def request_password_reset() -> Union[Dict, HttpResponse]:
|
||||
"""
|
||||
handle password reset request
|
||||
|
||||
If email sending is disabled, this endpoint is not available
|
||||
|
||||
**Example request**:
|
||||
|
||||
.. sourcecode:: http
|
||||
@ -1162,8 +1174,12 @@ def request_password_reset() -> Union[Dict, HttpResponse]:
|
||||
|
||||
:statuscode 200: password reset request processed
|
||||
:statuscode 400: invalid payload
|
||||
:statuscode 404: the requested URL was not found on the server
|
||||
|
||||
"""
|
||||
if not current_app.config['CAN_SEND_EMAILS']:
|
||||
return NotFoundErrorResponse(NOT_FOUND_MESSAGE)
|
||||
|
||||
post_data = request.get_json()
|
||||
if not post_data or post_data.get('email') is None:
|
||||
return InvalidPayloadErrorResponse()
|
||||
@ -1203,6 +1219,8 @@ def update_password() -> Union[Dict, HttpResponse]:
|
||||
"""
|
||||
update user password after password reset request
|
||||
|
||||
It sends emails if sending is enabled
|
||||
|
||||
**Example request**:
|
||||
|
||||
.. sourcecode:: http
|
||||
@ -1259,18 +1277,21 @@ def update_password() -> Union[Dict, HttpResponse]:
|
||||
).decode()
|
||||
db.session.commit()
|
||||
|
||||
password_change_email.send(
|
||||
{
|
||||
'language': ('en' if user.language is None else user.language),
|
||||
'email': user.email,
|
||||
},
|
||||
{
|
||||
'username': user.username,
|
||||
'fittrackee_url': current_app.config['UI_URL'],
|
||||
'operating_system': request.user_agent.platform,
|
||||
'browser_name': request.user_agent.browser,
|
||||
},
|
||||
)
|
||||
if current_app.config['CAN_SEND_EMAILS']:
|
||||
password_change_email.send(
|
||||
{
|
||||
'language': (
|
||||
'en' if user.language is None else user.language
|
||||
),
|
||||
'email': user.email,
|
||||
},
|
||||
{
|
||||
'username': user.username,
|
||||
'fittrackee_url': current_app.config['UI_URL'],
|
||||
'operating_system': request.user_agent.platform,
|
||||
'browser_name': request.user_agent.browser,
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
@ -1406,6 +1427,8 @@ def resend_account_confirmation_email() -> Union[Dict, HttpResponse]:
|
||||
"""
|
||||
resend email with instructions to confirm account
|
||||
|
||||
If email sending is disabled, this endpoint is not available
|
||||
|
||||
**Example request**:
|
||||
|
||||
.. sourcecode:: http
|
||||
@ -1429,9 +1452,13 @@ def resend_account_confirmation_email() -> Union[Dict, HttpResponse]:
|
||||
|
||||
:statuscode 200: confirmation email resent
|
||||
:statuscode 400: invalid payload
|
||||
:statuscode 404: the requested URL was not found on the server
|
||||
:statuscode 500: error, please try again or contact the administrator
|
||||
|
||||
"""
|
||||
if not current_app.config['CAN_SEND_EMAILS']:
|
||||
return NotFoundErrorResponse(NOT_FOUND_MESSAGE)
|
||||
|
||||
post_data = request.get_json()
|
||||
if not post_data or post_data.get('email') is None:
|
||||
return InvalidPayloadErrorResponse()
|
||||
|
@ -400,8 +400,9 @@ def update_user(auth_user: User, user_name: str) -> Union[Dict, HttpResponse]:
|
||||
Update user account
|
||||
|
||||
- add/remove admin rights (regardless user account status)
|
||||
- reset password (and send email to update user password)
|
||||
- update user email (and send email to update user password)
|
||||
- reset password (and send email to update user password,
|
||||
if sending enabled)
|
||||
- update user email (and send email to new user email, if sending enabled)
|
||||
- activate account for an inactive user
|
||||
|
||||
Only user with admin rights can modify another user
|
||||
@ -527,52 +528,56 @@ def update_user(auth_user: User, user_name: str) -> Union[Dict, HttpResponse]:
|
||||
new_email=new_email,
|
||||
)
|
||||
|
||||
user_language = 'en' if user.language is None else user.language
|
||||
ui_url = current_app.config['UI_URL']
|
||||
if reset_password:
|
||||
user_data = {
|
||||
'language': user_language,
|
||||
'email': user.email,
|
||||
}
|
||||
password_change_email.send(
|
||||
user_data,
|
||||
{
|
||||
'username': user.username,
|
||||
'fittrackee_url': ui_url,
|
||||
},
|
||||
)
|
||||
password_reset_token = user.encode_password_reset_token(user.id)
|
||||
reset_password_email.send(
|
||||
user_data,
|
||||
{
|
||||
'expiration_delay': get_readable_duration(
|
||||
current_app.config[
|
||||
'PASSWORD_TOKEN_EXPIRATION_SECONDS'
|
||||
],
|
||||
user_language,
|
||||
),
|
||||
'username': user.username,
|
||||
'password_reset_url': (
|
||||
f'{ui_url}/password-reset?token={password_reset_token}'
|
||||
),
|
||||
'fittrackee_url': ui_url,
|
||||
},
|
||||
)
|
||||
if current_app.config['CAN_SEND_EMAILS']:
|
||||
user_language = 'en' if user.language is None else user.language
|
||||
ui_url = current_app.config['UI_URL']
|
||||
if reset_password:
|
||||
user_data = {
|
||||
'language': user_language,
|
||||
'email': user.email,
|
||||
}
|
||||
password_change_email.send(
|
||||
user_data,
|
||||
{
|
||||
'username': user.username,
|
||||
'fittrackee_url': ui_url,
|
||||
},
|
||||
)
|
||||
password_reset_token = user.encode_password_reset_token(
|
||||
user.id
|
||||
)
|
||||
reset_password_email.send(
|
||||
user_data,
|
||||
{
|
||||
'expiration_delay': get_readable_duration(
|
||||
current_app.config[
|
||||
'PASSWORD_TOKEN_EXPIRATION_SECONDS'
|
||||
],
|
||||
user_language,
|
||||
),
|
||||
'username': user.username,
|
||||
'password_reset_url': (
|
||||
f'{ui_url}/password-reset?'
|
||||
f'token={password_reset_token}'
|
||||
),
|
||||
'fittrackee_url': ui_url,
|
||||
},
|
||||
)
|
||||
|
||||
if new_email:
|
||||
user_data = {
|
||||
'language': user_language,
|
||||
'email': user.email_to_confirm,
|
||||
}
|
||||
email_data = {
|
||||
'username': user.username,
|
||||
'fittrackee_url': ui_url,
|
||||
'email_confirmation_url': (
|
||||
f'{ui_url}/email-update'
|
||||
f'?token={user.confirmation_token}'
|
||||
),
|
||||
}
|
||||
email_updated_to_new_address.send(user_data, email_data)
|
||||
if new_email:
|
||||
user_data = {
|
||||
'language': user_language,
|
||||
'email': user.email_to_confirm,
|
||||
}
|
||||
email_data = {
|
||||
'username': user.username,
|
||||
'fittrackee_url': ui_url,
|
||||
'email_confirmation_url': (
|
||||
f'{ui_url}/email-update'
|
||||
f'?token={user.confirmation_token}'
|
||||
),
|
||||
}
|
||||
email_updated_to_new_address.send(user_data, email_data)
|
||||
|
||||
return {
|
||||
'status': 'success',
|
||||
|
Reference in New Issue
Block a user