backoffice: don't display disabled emails/texts options (#52641)

This commit is contained in:
Frédéric Péters 2021-04-03 16:43:02 +02:00
parent a69c78ef73
commit 380b1a1178
5 changed files with 62 additions and 31 deletions

View File

@ -507,6 +507,12 @@ def test_settings_emails(pub):
resp = resp.forms[0].submit()
assert pub.cfg['emails']['email-new-account-approved_subject'] is None
# disable password authentication method
pub.cfg['identification'] = {'methods': []}
pub.write_cfg()
resp = app.get('/backoffice/settings/emails/')
assert 'Approval of new account' not in resp.text
def test_settings_texts(pub):
create_superuser(pub)
@ -525,6 +531,12 @@ def test_settings_texts(pub):
assert resp.location == 'http://example.net/backoffice/settings/texts/'
assert pub.cfg['texts']['text-top-of-login'] is None
# disable password authentication method
pub.cfg['identification'] = {'methods': []}
pub.write_cfg()
resp = app.get('/backoffice/settings/texts/')
assert 'Text on top of the login page' not in resp.text
@pytest.mark.skipif('lasso is None')
def test_settings_auth(pub):

View File

@ -20,7 +20,6 @@ from quixote.html import TemplateIO, htmltext
import wcs.qommon.storage as st
from wcs.qommon import _, errors, force_str, get_cfg, ident, misc
from wcs.qommon.admin.emails import EmailsDirectory
from wcs.qommon.admin.menu import error_page
from wcs.qommon.backoffice.listing import pagination_links
from wcs.qommon.backoffice.menu import html_top
@ -533,22 +532,3 @@ class UsersDirectory(Directory):
return UserPage(component)
except KeyError:
raise errors.TraversalError()
EmailsDirectory.register(
'email_with_token',
_('Identification token'),
_('Available variables: token, token_url, sitename'),
category=_('Identification'),
default_subject=_('Access to [sitename]'),
default_body=_(
'''\
Hello,
An administrator delivered you access to [sitename].
Please visit [token_url] to enable it.
'''
),
)

View File

@ -38,6 +38,7 @@ class EmailsDirectory(Directory):
default_subject=None,
default_body=None,
category=None,
condition=None,
):
if key in cls.emails_dict:
return
@ -49,6 +50,7 @@ class EmailsDirectory(Directory):
'hint': hint,
'enabled': enabled,
'category': category,
'condition': condition,
}
@classmethod
@ -184,10 +186,12 @@ class EmailsDirectory(Directory):
r += htmltext('<li><a href="options">%s</a></li>') % _('General Options')
r += htmltext('</ul>')
keys = self.emails_dict.keys()
emails_dict = {
x: y for x, y in self.emails_dict.items() if not y.get('condition') or y['condition']()
}
categories = {}
for k, v in self.emails_dict.items():
for k, v in emails_dict.items():
if v.get('category'):
translated_category = v.get('category')
else:
@ -201,10 +205,10 @@ class EmailsDirectory(Directory):
r += htmltext('<h3>%s</h3>') % category_key
keys = categories.get(category_key)
keys.sort(key=lambda x: self.emails_dict[x]['description'])
keys.sort(key=lambda x: emails_dict[x]['description'])
r += htmltext('<ul>')
for email_key in keys:
email_values = self.emails_dict[email_key]
email_values = emails_dict[email_key]
r += htmltext('<li><a href="%s">%s</a></li>') % (email_key, email_values['description'])
r += htmltext('</ul>')

View File

@ -54,7 +54,7 @@ class TextsDirectory(Directory):
return htmltext('<div class="text-%s">%s</div>' % (key, text))
@classmethod
def register(cls, key, description, hint=None, default=None, wysiwyg=True, category=None):
def register(cls, key, description, hint=None, default=None, wysiwyg=True, category=None, condition=None):
# the wysiwyg is not actually used, it's always considered True, it's
# kept for backward compatibility with callers.
if key in cls.texts_dict:
@ -65,6 +65,7 @@ class TextsDirectory(Directory):
'hint': hint,
'default': default,
'category': category,
'condition': condition,
}
def html_top(self, title):
@ -75,9 +76,10 @@ class TextsDirectory(Directory):
r = TemplateIO(html=True)
r += htmltext('<h2>%s</h2>') % _('Custom Texts')
keys = self.texts_dict.keys()
texts_dict = {x: y for x, y in self.texts_dict.items() if not y.get('condition') or y['condition']()}
categories = {}
for k, v in self.texts_dict.items():
for k, v in texts_dict.items():
if v.get('category'):
translated_category = v.get('category')
else:
@ -91,10 +93,10 @@ class TextsDirectory(Directory):
r += htmltext('<h3>%s</h3>') % category_key
keys = categories.get(category_key)
keys.sort(key=lambda x: self.texts_dict[x]['description'])
keys.sort(key=lambda x: texts_dict[x]['description'])
r += htmltext('<ul>')
for text_key in keys:
text_values = self.texts_dict[text_key]
text_values = texts_dict[text_key]
r += htmltext('<li><a href="%s">%s</a></li>') % (text_key, text_values['description'])
r += htmltext('</ul>')

View File

@ -1063,11 +1063,17 @@ class PasswordAuthMethod(AuthMethod):
}
def is_password_enabled():
ident_methods = get_cfg('identification', {}).get('methods', []) or []
return 'password' in ident_methods
EmailsDirectory.register(
'password-subscription-notification',
_('Subscription notification for password account'),
_('Available variables: email, website, token_url, token, admin_email, username, password'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('Subscription Confirmation'),
default_body=_(
'''\
@ -1092,6 +1098,7 @@ EmailsDirectory.register(
_('Request for password change'),
_('Available variables: change_url, cancel_url, token, time'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('Change Password Request'),
default_body=_(
"""\
@ -1117,6 +1124,7 @@ EmailsDirectory.register(
_('New generated password'),
_('Available variables: username, password, hostname'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('Your new password'),
default_body=_(
'''\
@ -1137,6 +1145,7 @@ EmailsDirectory.register(
_('Approval of new account'),
_('Available variables: username, password'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('Your account has been approved'),
default_body=_(
'''\
@ -1155,6 +1164,7 @@ EmailsDirectory.register(
_('Warning about unusued account'),
_('Available variables: username'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('Your account is unused'),
default_body=_(
'''\
@ -1168,6 +1178,7 @@ EmailsDirectory.register(
_('Notification of removal of unused account'),
_('Available variables: username'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('Your account has been removed'),
default_body=_(
'''\
@ -1181,6 +1192,7 @@ EmailsDirectory.register(
_('Notification of new registration to administrators'),
_('Available variables: hostname, email_as_username, username'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('New Registration'),
default_body=_(
'''\
@ -1199,6 +1211,7 @@ EmailsDirectory.register(
_('Welcome email, with generated password'),
_('Available variables: hostname, username, password, email_as_username'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('Welcome to [hostname]'),
default_body=_(
'''\
@ -1214,6 +1227,7 @@ EmailsDirectory.register(
_('Email with a new password for the user'),
_('Available variables: hostname, name, username, password'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('Your new password for [hostname]'),
default_body=_(
'''\
@ -1229,6 +1243,7 @@ EmailsDirectory.register(
_('Email with current password for the user'),
_('Available variables: hostname, name, username, password'),
category=_('Identification'),
condition=is_password_enabled,
default_subject=_('Your password for [hostname]'),
default_body=_(
'''\
@ -1244,6 +1259,7 @@ TextsDirectory.register(
'account-created',
_('Text when account confirmed by user'),
category=_('Identification'),
condition=is_password_enabled,
default=_(
'''<p>
Your account has been created.
@ -1255,6 +1271,7 @@ TextsDirectory.register(
'password-forgotten-token-sent',
_('Text when an email with a change password token has been sent'),
category=_('Identification'),
condition=is_password_enabled,
default=_(
'''<p>
A token for changing your password has been emailed to you. Follow the instructions in that email to change your password.
@ -1269,6 +1286,7 @@ TextsDirectory.register(
'new-password-sent-by-email',
_('Text when new password has been sent'),
category=_('Identification'),
condition=is_password_enabled,
default=_(
'''<p>
Your new password has been sent to you by email.
@ -1279,12 +1297,18 @@ Your new password has been sent to you by email.
),
)
TextsDirectory.register('new-account', _('Text on top of registration form'), category=_('Identification'))
TextsDirectory.register(
'new-account',
_('Text on top of registration form'),
category=_('Identification'),
condition=is_password_enabled,
)
TextsDirectory.register(
'password-forgotten-link',
_('Text on login page, linking to the forgotten password request page'),
category=_('Identification'),
condition=is_password_enabled,
default=_(
'''<p>
If you have an account, but have forgotten your password, you should go
@ -1298,6 +1322,7 @@ TextsDirectory.register(
'password-forgotten-enter-username',
_('Text on forgotten password request page'),
category=_('Identification'),
condition=is_password_enabled,
default=_(
'''<p>
If you have an account, but have forgotten your password, enter your user name
@ -1311,6 +1336,7 @@ TextsDirectory.register(
_('Text linking the login page to the account creation page'),
hint=_('Available variable: register_url'),
category=_('Identification'),
condition=is_password_enabled,
default=_(
'''<p>
If you do not have an account, you should go to the <a href="[register_url]">
@ -1323,6 +1349,7 @@ TextsDirectory.register(
'invalid-password-token',
_('Text when an invalid password token is used'),
category=_('Identification'),
condition=is_password_enabled,
default=_(
'''<p>
Sorry, the token you used is invalid, or has already been used.
@ -1330,12 +1357,18 @@ Sorry, the token you used is invalid, or has already been used.
),
)
TextsDirectory.register('top-of-login', _('Text on top of the login page'), category=_('Identification'))
TextsDirectory.register(
'top-of-login',
_('Text on top of the login page'),
category=_('Identification'),
condition=is_password_enabled,
)
TextsDirectory.register(
'email-sent-confirm-creation',
_('Text when a mail for confirmation of an account creation has been sent'),
category=_('Identification'),
condition=is_password_enabled,
default=_('An email has been sent to you so you can confirm your account creation.'),
)