Skip to content

Commit 7886432

Browse files
author
Luigi Pellecchia
committed
Add email notification in case of user role change. Add a method in api_utils to add a link to the BASIL instance in the email body in case the app_url setting is valid. Added a footer message to invite users to join the Matrix chat room.
Signed-off-by: Luigi Pellecchia <lpellecc@redhat.com>
1 parent b5123af commit 7886432

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

api/api.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from sqlalchemy import and_, or_, update
2222
from sqlalchemy.orm.exc import NoResultFound
2323
from api_utils import (
24+
add_html_link_to_email_body,
2425
async_email_notification,
2526
get_api_specification,
2627
get_html_email_body_from_template,
@@ -44,6 +45,9 @@
4445
TESTRUN_PRESET_FILEPATH = os.path.join(currentdir, CONFIGS_FOLDER, "testrun_plugin_presets.yaml")
4546
SETTINGS_FILEPATH = os.path.join(currentdir, CONFIGS_FOLDER, "settings.yaml")
4647
EMAIL_TEMPLATE_PATH = os.path.join(currentdir, CONFIGS_FOLDER, "email_template.html")
48+
EMAIL_MATRIX_FOOTER_MESSAGE = "<p>Join our <a href='" \
49+
"https://matrix.to/#/!RoPWKbVtTKUKNouZCV:matrix.org?via=matrix.org" \
50+
"'>BASIL Matrix chat room</a> to discuss about the tool usage and development!</p>"
4751
TEST_RUNS_BASE_DIR = os.getenv("TEST_RUNS_BASE_DIR", "/var/test-runs")
4852
USER_FILES_BASE_DIR = os.path.join(currentdir, "user-files") # forced under api to ensure tmt tree validity
4953
PYPROJECT_FILEPATH = os.path.join(os.path.dirname(currentdir), "pyproject.toml")
@@ -5489,7 +5493,7 @@ def post(self):
54895493
# Send email notifications to admins
54905494
email_subject = "BASIL - New User"
54915495
email_body = f"{username} joined us on BASIL!"
5492-
email_footer = ""
5496+
email_footer = EMAIL_MATRIX_FOOTER_MESSAGE
54935497

54945498
admins = dbi.session.query(UserModel).filter(
54955499
UserModel.role == "ADMIN").filter(
@@ -5903,13 +5907,18 @@ def get(self):
59035907
dbi.session.add(target_user)
59045908
dbi.session.commit()
59055909
dbi.engine.dispose()
5910+
5911+
# Notification
5912+
settings = load_settings()
59065913
email_subject = "BASIL - Confirm password reset"
5907-
email_footer = ""
5914+
email_footer = EMAIL_MATRIX_FOOTER_MESSAGE
5915+
email_body = "<p>Your password has been reset</p>"
5916+
email_body = add_html_link_to_email_body(settings=settings, body=email_body)
59085917
email_body = get_html_email_body_from_template(EMAIL_TEMPLATE_PATH,
59095918
email_subject,
5910-
"Your password has been reset",
5919+
email_body,
59115920
email_footer)
5912-
email_notifier = EmailNotifier(settings=load_settings())
5921+
email_notifier = EmailNotifier(settings=settings)
59135922
ret = email_notifier.send_email(email, email_subject, email_body, True)
59145923
if ret:
59155924
if "redirect" in request_data.keys():
@@ -5940,15 +5949,16 @@ def post(self):
59405949

59415950
# generate reset_token and reset_pwd
59425951
email_subject = "BASIL - Password reset"
5943-
email_footer = ""
5952+
email_footer = EMAIL_MATRIX_FOOTER_MESSAGE
59445953

59455954
reset_pwd = secrets.token_urlsafe(10)
59465955
encoded_reset_pwd = base64.b64encode(reset_pwd.encode("utf-8")).decode("utf-8")
59475956

59485957
reset_token = secrets.token_urlsafe(90)
59495958
reset_url = f"{request.base_url}?email={email}&reset_token={reset_token}"
59505959
if "app_url" in settings.keys():
5951-
reset_url += f"&redirect={settings['app_url']}/login?from=reset-password"
5960+
if str(settings["app_url"].trim()):
5961+
reset_url += f"&redirect={settings['app_url']}/login?from=reset-password"
59525962

59535963
target_user.reset_pwd = encoded_reset_pwd
59545964
target_user.reset_token = reset_token
@@ -6037,6 +6047,23 @@ def put(self):
60376047
target_user.role = request_data["role"]
60386048
dbi.session.commit()
60396049

6050+
# Notification
6051+
settings = load_settings()
6052+
email_subject = "BASIL user role changed"
6053+
email_footer = EMAIL_MATRIX_FOOTER_MESSAGE
6054+
email_body = f"<p>Your BASIL user role changed to <b>{target_user.role}</b></p>"
6055+
email_body += "<p>See the <a href='" \
6056+
"https://basil-the-fusa-spice.readthedocs.io/en/latest/user_management.html#roles" \
6057+
"'>BASIL documentation</a> for a description of each role.</p>"
6058+
email_body = add_html_link_to_email_body(settings=settings, body=email_body)
6059+
email_body = get_html_email_body_from_template(EMAIL_TEMPLATE_PATH,
6060+
email_subject,
6061+
email_body,
6062+
email_footer)
6063+
6064+
email_notifier = EmailNotifier(settings=settings)
6065+
email_notifier.send_email(target_user.email, email_subject, email_body, True)
6066+
60406067
return {"email": request_data["email"]}
60416068

60426069

api/api_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
from urllib.error import HTTPError, URLError
66

77

8+
def add_html_link_to_email_body(settings, body):
9+
"""Append a link to BASIL instance if the app_url setting is populated"""
10+
if "app_url" in settings.keys():
11+
if str(settings["app_url"].trim()):
12+
body += f"<p><a href='{settings['app_url']}'>Link to BASIL website</a></p>"
13+
return body
14+
15+
816
def get_html_email_body_from_template(template_path, subject, body, footer):
917
"""Generate the HTML email body from a template file using
1018
custom values for subject, body and footer

0 commit comments

Comments
 (0)