Skip to content

Commit

Permalink
Merge pull request #310 from hypsug0/dev
Browse files Browse the repository at this point in the history
Merge and improve #288
  • Loading branch information
lpofredc authored Oct 5, 2021
2 parents a0c61c5 + 03fe021 commit df56257
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 67 deletions.
56 changes: 17 additions & 39 deletions backend/gncitizen/core/users/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
from gncitizen.utils.env import MEDIA_DIR
from gncitizen.utils.errors import GeonatureApiError
from gncitizen.utils.jwt import admin_required, get_user_if_exists
from gncitizen.utils.mail_check import confirm_token, confirm_user_email
from gncitizen.utils.mail_check import (
confirm_token,
confirm_user_email,
send_user_email,
)
from server import db

from .models import RevokedTokenModel, UserModel
Expand Down Expand Up @@ -174,7 +178,7 @@ def registration():
try:
if (
current_app.config["CONFIRM_EMAIL"]["USE_CONFIRM_EMAIL"]
== False
is False
):
message = (
"""Félicitations, l'utilisateur "{}" a été créé.""".format(
Expand All @@ -184,7 +188,8 @@ def registration():
confirm_user_email(newuser, with_confirm_link=False)
else:
message = (
"""Félicitations, l'utilisateur "{}" a été créé. \r\n Vous allez recevoir un email pour activer votre compte """.format(
"""Félicitations, l'utilisateur "{}" a été créé. \r\n
Vous allez recevoir un email pour activer votre compte """.format(
newuser.username
),
)
Expand Down Expand Up @@ -543,46 +548,19 @@ def reset_user_password():
passwd = uuid.uuid4().hex[0:6]
passwd_hash = UserModel.generate_hash(passwd)

msg = MIMEMultipart("alternative")
msg["Subject"] = current_app.config["RESET_PASSWD"]["SUBJECT"]
msg["From"] = current_app.config["RESET_PASSWD"]["FROM"]
msg["To"] = user.email

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(
current_app.config["RESET_PASSWD"]["TEXT_TEMPLATE"].format(
passwd=passwd, app_url=current_app.config["URL_APPLICATION"]
),
"plain",
subject = current_app.config["RESET_PASSWD"]["SUBJECT"]
to = user.email
plain_message = current_app.config["RESET_PASSWD"]["TEXT_TEMPLATE"].format(
passwd=passwd, app_url=current_app.config["URL_APPLICATION"]
)
part2 = MIMEText(
current_app.config["RESET_PASSWD"]["HTML_TEMPLATE"].format(
passwd=passwd, app_url=current_app.config["URL_APPLICATION"]
),
"html",
html_message = current_app.config["RESET_PASSWD"]["HTML_TEMPLATE"].format(
passwd=passwd, app_url=current_app.config["URL_APPLICATION"]
)

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

try:
with smtplib.SMTP_SSL(
current_app.config["MAIL"]["MAIL_HOST"],
int(current_app.config["MAIL"]["MAIL_PORT"]),
) as server:
server.login(
str(current_app.config["MAIL"]["MAIL_AUTH_LOGIN"]),
str(current_app.config["MAIL"]["MAIL_AUTH_PASSWD"]),
)
server.sendmail(
current_app.config["MAIL"]["MAIL_AUTH_LOGIN"],
user.email,
msg.as_string(),
)
server.quit()
send_user_email(
subject, to, plain_message=plain_message, html_message=html_message
)
user.password = passwd_hash
db.session.commit()
return (
Expand Down
86 changes: 58 additions & 28 deletions backend/gncitizen/utils/mail_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,32 @@
from itsdangerous import URLSafeTimedSerializer


def confirm_user_email(newuser, with_confirm_link=True):

token = generate_confirmation_token(newuser.email)
def send_user_email(
subject: str, to: str, plain_message: str = None, html_message: str = None
):
msg = MIMEMultipart("alternative")
msg["Subject"] = current_app.config["CONFIRM_EMAIL"]["SUBJECT"]
msg["From"] = current_app.config["CONFIRM_EMAIL"]["FROM"]
msg["To"] = newuser.email

# Check URL_APPLICATION:
url_application = current_app.config["URL_APPLICATION"]
if url_application[-1] == "/":
url_application = url_application
else:
url_application = url_application + "/"
print("url_application", url_application)
activate_url = url_application + "confirmEmail/" + token

# Record the MIME text/html.
template = current_app.config["CONFIRM_EMAIL"]["HTML_TEMPLATE"]
if not with_confirm_link:
template = current_app.config["CONFIRM_EMAIL"][
"NO_VALIDATION_HTML_TEMPLATE"
]
msg_body = MIMEText(
template.format(activate_url=activate_url),
"html",
msg["Subject"] = subject
msg["From"] = current_app.config["MAIL"]["MAIL_AUTH_LOGIN"]
msg["To"] = to
plain_msg = MIMEText(
html_message,
"plain",
)
msg.attach(plain_msg)

if plain_message:
plain_msg = MIMEText(
plain_message,
"html",
)
msg.attach(plain_msg)

msg.attach(msg_body)
if html_message:
html_msg = MIMEText(
html_message,
"html",
)
msg.attach(html_msg)

try:
if current_app.config["MAIL"]["MAIL_USE_SSL"]:
Expand All @@ -56,12 +53,45 @@ def confirm_user_email(newuser, with_confirm_link=True):
str(current_app.config["MAIL"]["MAIL_AUTH_PASSWD"]),
)
server.sendmail(
current_app.config["CONFIRM_EMAIL"]["FROM"],
newuser.email,
current_app.config["MAIL"]["MAIL_AUTH_LOGIN"],
to,
msg.as_string(),
)
server.quit()

except Exception as e:
current_app.logger.warning("send email failled. %s", str(e))
return {"message": """ send email failled: "{}".""".format(str(e))}


def confirm_user_email(newuser, with_confirm_link=True):

token = generate_confirmation_token(newuser.email)
subject = current_app.config["CONFIRM_EMAIL"]["SUBJECT"]
to = newuser.email

# Check URL_APPLICATION:
url_application = current_app.config["URL_APPLICATION"]
if url_application[-1] == "/":
url_application = url_application
else:
url_application = url_application + "/"
print("url_application", url_application)
activate_url = url_application + "confirmEmail/" + token

# Record the MIME text/html.
template = current_app.config["CONFIRM_EMAIL"]["HTML_TEMPLATE"]
if not with_confirm_link:
template = current_app.config["CONFIRM_EMAIL"][
"NO_VALIDATION_HTML_TEMPLATE"
]
try:
send_user_email(
subject,
to,
html_message=template.format(activate_url=activate_url),
)

except Exception as e:
current_app.logger.warning("send confirm_email failled. %s", str(e))
return {
Expand Down

0 comments on commit df56257

Please sign in to comment.