plugin misc account: fix send_email calls + admin email flag:
The `send_email` calls in `send_emails` passed the plugin `EmailSettings` as the `config` argument, raising `AttributeError` on account creation and leading to the profile deletion. They now pass the main config via a new `email_settings`. `credentials_xmpp_connect` takes a new `send_email` flag to let frontends skip the admin notification email (notably useful in web local mode).
This commit is contained in:
parent
1003cd2c6d
commit
e8b3948ad0
2 changed files with 24 additions and 13 deletions
|
|
@ -145,7 +145,7 @@ class MiscAccount(object):
|
|||
host.bridge.add_method(
|
||||
"credentials_xmpp_connect",
|
||||
".plugin",
|
||||
in_sign="ss",
|
||||
in_sign="ssb",
|
||||
out_sign="b",
|
||||
method=self.credentials_xmpp_connect,
|
||||
async_=True,
|
||||
|
|
@ -293,11 +293,11 @@ class MiscAccount(object):
|
|||
# there is no email when an existing XMPP account is used
|
||||
body = f"New account created on {domain}: {profile} [{email or '<no email>'}]"
|
||||
d_admin = sat_email.send_email(
|
||||
self.get_email_conf(),
|
||||
self.host.memory.config,
|
||||
admins_emails,
|
||||
subject,
|
||||
body,
|
||||
email_settings=self.get_email_conf(),
|
||||
)
|
||||
|
||||
admins_emails_txt = ", ".join(["<" + addr + ">" for addr in admins_emails])
|
||||
|
|
@ -326,7 +326,13 @@ class MiscAccount(object):
|
|||
# XXX: this will not fail when the email address doesn't exist
|
||||
# FIXME: check email reception to validate email given by the user
|
||||
# FIXME: delete the profile if the email could not been sent?
|
||||
d_user = sat_email.send_email(self.get_email_conf, [email], subject, body)
|
||||
d_user = sat_email.send_email(
|
||||
self.host.memory.config,
|
||||
[email],
|
||||
subject,
|
||||
body,
|
||||
email_settings=self.get_email_conf(),
|
||||
)
|
||||
d_user.addCallbacks(
|
||||
lambda __: log.debug(
|
||||
"Account creation confirmation sent to <{}>".format(email)
|
||||
|
|
@ -689,12 +695,13 @@ class MiscAccount(object):
|
|||
d.addCallbacks(deleted, errback)
|
||||
return d
|
||||
|
||||
def credentials_xmpp_connect(self, jid_s, password):
|
||||
def credentials_xmpp_connect(self, jid_s, password, send_email=True):
|
||||
"""Create and connect a new SàT profile using the given XMPP credentials.
|
||||
|
||||
Re-use given JID and XMPP password for the profile name and profile password.
|
||||
@param jid_s (unicode): JID
|
||||
@param password (unicode): XMPP password
|
||||
@param send_email (bool): send the administrator notification email
|
||||
@return Deferred(bool)
|
||||
@raise exceptions.PasswordError, exceptions.ConflictError
|
||||
"""
|
||||
|
|
@ -716,7 +723,8 @@ class MiscAccount(object):
|
|||
)
|
||||
|
||||
def connected(result):
|
||||
self.send_emails(None, profile=jid_s)
|
||||
if send_email:
|
||||
self.send_emails(None, profile=jid_s)
|
||||
return result
|
||||
|
||||
def remove_profile(
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ def send_email(
|
|||
subject: str = "",
|
||||
body: str = "",
|
||||
from_email: str | None = None,
|
||||
email_settings: tools_config.EmailSettings | None = None,
|
||||
) -> defer.Deferred:
|
||||
"""Send an email using Libervia configuration
|
||||
|
||||
|
|
@ -43,12 +44,14 @@ def send_email(
|
|||
@param subject: subject of the message
|
||||
@param body: body of the message
|
||||
@param from_email: address of the sender
|
||||
@param email_settings: ``EmailSettings`` to use, default to ``config.email``
|
||||
@return:Same as smtp.sendmail.
|
||||
"""
|
||||
if isinstance(to_emails, str):
|
||||
to_emails = to_emails.split()
|
||||
email_host = config.email.server
|
||||
email_from = from_email or config.email.from_
|
||||
settings = email_settings or config.email
|
||||
email_host = settings.server
|
||||
email_from = from_email or settings.from_
|
||||
|
||||
# we suppose that email domain and XMPP domain are identical
|
||||
domain = config.common.xmpp_domain
|
||||
|
|
@ -60,12 +63,12 @@ def send_email(
|
|||
|
||||
if email_from is None:
|
||||
email_from = "no_reply@" + domain
|
||||
email_sender_domain = config.email.sender_domain or domain
|
||||
email_port = config.email.port
|
||||
email_username = config.email.username
|
||||
email_password = config.email.password
|
||||
email_auth = config.email.auth
|
||||
email_starttls = config.email.starttls
|
||||
email_sender_domain = settings.sender_domain or domain
|
||||
email_port = settings.port
|
||||
email_username = settings.username
|
||||
email_password = settings.password
|
||||
email_auth = settings.auth
|
||||
email_starttls = settings.starttls
|
||||
|
||||
msg = MIMEText(body, "plain", "UTF-8")
|
||||
msg["Subject"] = subject
|
||||
|
|
|
|||
Loading…
Reference in a new issue