Projet

Général

Profil

0001-backoffice-don-t-display-disabled-emails-texts-optio.patch

Frédéric Péters, 03 avril 2021 17:14

Télécharger (13,5 ko)

Voir les différences:

Subject: [PATCH] backoffice: don't display disabled emails/texts options
 (#52641)

 tests/admin_pages/test_settings.py | 12 ++++++++++
 wcs/admin/users.py                 | 22 +-----------------
 wcs/qommon/admin/emails.py         | 12 ++++++----
 wcs/qommon/admin/texts.py          | 12 ++++++----
 wcs/qommon/ident/password.py       | 37 ++++++++++++++++++++++++++++--
 5 files changed, 63 insertions(+), 32 deletions(-)
tests/admin_pages/test_settings.py
485 485
    resp = resp.forms[0].submit()
486 486
    assert pub.cfg['emails']['email-new-account-approved_subject'] is None
487 487

  
488
    # disable password authentication method
489
    pub.cfg['identification'] = {'methods': []}
490
    pub.write_cfg()
491
    resp = app.get('/backoffice/settings/emails/')
492
    assert 'Approval of new account' not in resp.text
493

  
488 494

  
489 495
def test_settings_texts(pub):
490 496
    create_superuser(pub)
......
503 509
    assert resp.location == 'http://example.net/backoffice/settings/texts/'
504 510
    assert pub.cfg['texts']['text-top-of-login'] is None
505 511

  
512
    # disable password authentication method
513
    pub.cfg['identification'] = {'methods': []}
514
    pub.write_cfg()
515
    resp = app.get('/backoffice/settings/texts/')
516
    assert 'Text on top of the login page' not in resp.text
517

  
506 518

  
507 519
@pytest.mark.skipif('lasso is None')
508 520
def test_settings_auth(pub):
wcs/admin/users.py
19 19
from quixote.html import TemplateIO, htmltext
20 20

  
21 21
import wcs.qommon.storage as st
22
from wcs.qommon import N_, _, errors, force_str, get_cfg, ident, misc
23
from wcs.qommon.admin.emails import EmailsDirectory
22
from wcs.qommon import _, errors, force_str, get_cfg, ident, misc
24 23
from wcs.qommon.admin.menu import error_page
25 24
from wcs.qommon.backoffice.listing import pagination_links
26 25
from wcs.qommon.backoffice.menu import html_top
......
533 532
            return UserPage(component)
534 533
        except KeyError:
535 534
            raise errors.TraversalError()
536

  
537

  
538
EmailsDirectory.register(
539
    'email_with_token',
540
    N_('Identification token'),
541
    N_('Available variables: token, token_url, sitename'),
542
    category=N_('Identification'),
543
    default_subject=N_('Access to [sitename]'),
544
    default_body=N_(
545
        '''\
546
Hello,
547

  
548
An administrator delivered you access to [sitename].
549

  
550
Please visit [token_url] to enable it.
551

  
552
'''
553
    ),
554
)
wcs/qommon/admin/emails.py
38 38
        default_subject=None,
39 39
        default_body=None,
40 40
        category=None,
41
        condition=None,
41 42
    ):
42 43
        if key in cls.emails_dict:
43 44
            return
......
49 50
            'hint': hint,
50 51
            'enabled': enabled,
51 52
            'category': category,
53
            'condition': condition,
52 54
        }
53 55

  
54 56
    @classmethod
......
188 190
        r += htmltext('<li><a href="options">%s</a></li>') % _('General Options')
189 191
        r += htmltext('</ul>')
190 192

  
191
        keys = self.emails_dict.keys()
193
        emails_dict = {
194
            x: y for x, y in self.emails_dict.items() if not y.get('condition') or y['condition']()
195
        }
192 196

  
193 197
        categories = {}
194
        for k, v in self.emails_dict.items():
198
        for k, v in emails_dict.items():
195 199
            if v.get('category'):
196 200
                translated_category = _(v.get('category'))
197 201
            else:
......
205 209
                r += htmltext('<h3>%s</h3>') % category_key
206 210

  
207 211
            keys = categories.get(category_key)
208
            keys.sort(key=lambda x: _(self.emails_dict[x]['description']))
212
            keys.sort(key=lambda x: _(emails_dict[x]['description']))
209 213
            r += htmltext('<ul>')
210 214
            for email_key in keys:
211
                email_values = self.emails_dict[email_key]
215
                email_values = emails_dict[email_key]
212 216
                r += htmltext('<li><a href="%s">%s</a></li>') % (email_key, _(email_values['description']))
213 217
            r += htmltext('</ul>')
214 218

  
wcs/qommon/admin/texts.py
54 54
        return htmltext('<div class="text-%s">%s</div>' % (key, text))
55 55

  
56 56
    @classmethod
57
    def register(cls, key, description, hint=None, default=None, wysiwyg=True, category=None):
57
    def register(cls, key, description, hint=None, default=None, wysiwyg=True, category=None, condition=None):
58 58
        # the wysiwyg is not actually used, it's always considered True, it's
59 59
        # kept for backward compatibility with callers.
60 60
        if key in cls.texts_dict:
......
65 65
            'hint': hint,
66 66
            'default': default,
67 67
            'category': category,
68
            'condition': condition,
68 69
        }
69 70

  
70 71
    def html_top(self, title):
......
75 76
        r = TemplateIO(html=True)
76 77
        r += htmltext('<h2>%s</h2>') % _('Custom Texts')
77 78

  
78
        keys = self.texts_dict.keys()
79
        texts_dict = {x: y for x, y in self.texts_dict.items() if not y.get('condition') or y['condition']()}
80

  
79 81
        categories = {}
80
        for k, v in self.texts_dict.items():
82
        for k, v in texts_dict.items():
81 83
            if v.get('category'):
82 84
                translated_category = _(v.get('category'))
83 85
            else:
......
91 93
                r += htmltext('<h3>%s</h3>') % category_key
92 94

  
93 95
            keys = categories.get(category_key)
94
            keys.sort(key=lambda x: _(self.texts_dict[x]['description']))
96
            keys.sort(key=lambda x: _(texts_dict[x]['description']))
95 97
            r += htmltext('<ul>')
96 98
            for text_key in keys:
97
                text_values = self.texts_dict[text_key]
99
                text_values = texts_dict[text_key]
98 100
                r += htmltext('<li><a href="%s">%s</a></li>') % (text_key, _(text_values['description']))
99 101
            r += htmltext('</ul>')
100 102

  
wcs/qommon/ident/password.py
1067 1067
        }
1068 1068

  
1069 1069

  
1070
def is_password_enabled():
1071
    ident_methods = get_cfg('identification', {}).get('methods', []) or []
1072
    return 'password' in ident_methods
1073

  
1074

  
1070 1075
EmailsDirectory.register(
1071 1076
    'password-subscription-notification',
1072 1077
    N_('Subscription notification for password account'),
1073 1078
    N_('Available variables: email, website, token_url, token, admin_email, username, password'),
1074 1079
    category=N_('Identification'),
1080
    condition=is_password_enabled,
1075 1081
    default_subject=N_('Subscription Confirmation'),
1076 1082
    default_body=N_(
1077 1083
        '''\
......
1096 1102
    N_('Request for password change'),
1097 1103
    N_('Available variables: change_url, cancel_url, token, time'),
1098 1104
    category=N_('Identification'),
1105
    condition=is_password_enabled,
1099 1106
    default_subject=N_('Change Password Request'),
1100 1107
    default_body=N_(
1101 1108
        """\
......
1121 1128
    N_('New generated password'),
1122 1129
    N_('Available variables: username, password, hostname'),
1123 1130
    category=N_('Identification'),
1131
    condition=is_password_enabled,
1124 1132
    default_subject=N_('Your new password'),
1125 1133
    default_body=N_(
1126 1134
        '''\
......
1141 1149
    N_('Approval of new account'),
1142 1150
    N_('Available variables: username, password'),
1143 1151
    category=N_('Identification'),
1152
    condition=is_password_enabled,
1144 1153
    default_subject=N_('Your account has been approved'),
1145 1154
    default_body=N_(
1146 1155
        '''\
......
1159 1168
    N_('Warning about unusued account'),
1160 1169
    N_('Available variables: username'),
1161 1170
    category=N_('Identification'),
1171
    condition=is_password_enabled,
1162 1172
    default_subject=N_('Your account is unused'),
1163 1173
    default_body=N_(
1164 1174
        '''\
......
1172 1182
    N_('Notification of removal of unused account'),
1173 1183
    N_('Available variables: username'),
1174 1184
    category=N_('Identification'),
1185
    condition=is_password_enabled,
1175 1186
    default_subject=N_('Your account has been removed'),
1176 1187
    default_body=N_(
1177 1188
        '''\
......
1185 1196
    N_('Notification of new registration to administrators'),
1186 1197
    N_('Available variables: hostname, email_as_username, username'),
1187 1198
    category=N_('Identification'),
1199
    condition=is_password_enabled,
1188 1200
    default_subject=N_('New Registration'),
1189 1201
    default_body=N_(
1190 1202
        '''\
......
1203 1215
    N_('Welcome email, with generated password'),
1204 1216
    N_('Available variables: hostname, username, password, email_as_username'),
1205 1217
    category=N_('Identification'),
1218
    condition=is_password_enabled,
1206 1219
    default_subject=N_('Welcome to [hostname]'),
1207 1220
    default_body=N_(
1208 1221
        '''\
......
1218 1231
    N_('Email with a new password for the user'),
1219 1232
    N_('Available variables: hostname, name, username, password'),
1220 1233
    category=N_('Identification'),
1234
    condition=is_password_enabled,
1221 1235
    default_subject=N_('Your new password for [hostname]'),
1222 1236
    default_body=N_(
1223 1237
        '''\
......
1233 1247
    N_('Email with current password for the user'),
1234 1248
    N_('Available variables: hostname, name, username, password'),
1235 1249
    category=N_('Identification'),
1250
    condition=is_password_enabled,
1236 1251
    default_subject=N_('Your password for [hostname]'),
1237 1252
    default_body=N_(
1238 1253
        '''\
......
1248 1263
    'account-created',
1249 1264
    N_('Text when account confirmed by user'),
1250 1265
    category=N_('Identification'),
1266
    condition=is_password_enabled,
1251 1267
    default=N_(
1252 1268
        '''<p>
1253 1269
Your account has been created.
......
1259 1275
    'password-forgotten-token-sent',
1260 1276
    N_('Text when an email with a change password token has been sent'),
1261 1277
    category=N_('Identification'),
1278
    condition=is_password_enabled,
1262 1279
    default=N_(
1263 1280
        '''<p>
1264 1281
A token for changing your password has been emailed to you. Follow the instructions in that email to change your password.
......
1273 1290
    'new-password-sent-by-email',
1274 1291
    N_('Text when new password has been sent'),
1275 1292
    category=N_('Identification'),
1293
    condition=is_password_enabled,
1276 1294
    default=N_(
1277 1295
        '''<p>
1278 1296
Your new password has been sent to you by email.
......
1283 1301
    ),
1284 1302
)
1285 1303

  
1286
TextsDirectory.register('new-account', N_('Text on top of registration form'), category=N_('Identification'))
1304
TextsDirectory.register(
1305
    'new-account',
1306
    N_('Text on top of registration form'),
1307
    category=N_('Identification'),
1308
    condition=is_password_enabled,
1309
)
1287 1310

  
1288 1311
TextsDirectory.register(
1289 1312
    'password-forgotten-link',
1290 1313
    N_('Text on login page, linking to the forgotten password request page'),
1291 1314
    category=N_('Identification'),
1315
    condition=is_password_enabled,
1292 1316
    default=N_(
1293 1317
        '''<p>
1294 1318
If you have an account, but have forgotten your password, you should go
......
1302 1326
    'password-forgotten-enter-username',
1303 1327
    N_('Text on forgotten password request page'),
1304 1328
    category=N_('Identification'),
1329
    condition=is_password_enabled,
1305 1330
    default=N_(
1306 1331
        '''<p>
1307 1332
If you have an account, but have forgotten your password, enter your user name
......
1315 1340
    N_('Text linking the login page to the account creation page'),
1316 1341
    hint=N_('Available variable: register_url'),
1317 1342
    category=N_('Identification'),
1343
    condition=is_password_enabled,
1318 1344
    default=N_(
1319 1345
        '''<p>
1320 1346
If you do not have an account, you should go to the <a href="[register_url]">
......
1327 1353
    'invalid-password-token',
1328 1354
    N_('Text when an invalid password token is used'),
1329 1355
    category=N_('Identification'),
1356
    condition=is_password_enabled,
1330 1357
    default=N_(
1331 1358
        '''<p>
1332 1359
Sorry, the token you used is invalid, or has already been used.
......
1334 1361
    ),
1335 1362
)
1336 1363

  
1337
TextsDirectory.register('top-of-login', N_('Text on top of the login page'), category=N_('Identification'))
1364
TextsDirectory.register(
1365
    'top-of-login',
1366
    N_('Text on top of the login page'),
1367
    category=N_('Identification'),
1368
    condition=is_password_enabled,
1369
)
1338 1370

  
1339 1371
TextsDirectory.register(
1340 1372
    'email-sent-confirm-creation',
1341 1373
    N_('Text when a mail for confirmation of an account creation has been sent'),
1342 1374
    category=N_('Identification'),
1375
    condition=is_password_enabled,
1343 1376
    default=N_('An email has been sent to you so you can confirm your account creation.'),
1344 1377
)
1345 1378

  
1346
-