Projet

Général

Profil

0001-forms-force-authentication-if-anonymous-use-user-tra.patch

Nicolas Roche, 05 décembre 2019 18:08

Télécharger (5,87 ko)

Voir les différences:

Subject: [PATCH] forms: force authentication if anonymous use user tracking
 code (#38239)

 tests/test_form_pages.py    |  7 ++-----
 tests/test_tracking_code.py | 29 +++++++++++++++++++++--------
 wcs/forms/root.py           |  8 +++++++-
 3 files changed, 30 insertions(+), 14 deletions(-)
tests/test_form_pages.py
1559 1559
    resp = resp.forms[0].submit()
1560 1560
    assert formdef.data_class().get(formdata_id).evolution[-1].comment == 'hello world'
1561 1561

  
1562
    # and check we can also get back to it as anonymous
1562
    # check we can't get back to it as anonymous
1563 1563
    app = get_app(pub)
1564 1564
    resp = app.get('/')
1565 1565
    resp.forms[0]['code'] = tracking_code
1566 1566
    resp = resp.forms[0].submit()
1567 1567
    assert resp.location == 'http://example.net/code/%s/load' % tracking_code
1568 1568
    resp = resp.follow()
1569
    assert resp.location == 'http://example.net/test/%s' % formdata_id
1570
    resp = resp.follow()
1571
    resp = resp.follow()
1572
    assert 'form_comment' in resp.text # makes sure user is treated as submitter
1569
    assert resp.location == 'http://example.net/login/?ReturnUrl=http://example.net/test/%s' % formdata_id
1573 1570

  
1574 1571
    # and check a bot is not allowed to get it
1575 1572
    app = get_app(pub)
tests/test_tracking_code.py
160 160

  
161 161
    2- Access using tracking code :
162 162

  
163
    All access is granted,
163
    | sumitter / accesser | anonymous | user1 | user2 | agent1 | agent2 | admin1 |
164
    +---------------------+-----------+-------+-------+--------+--------+--------+
165
    | anonymous           |  allow    | allow | allow | allow  | allow  | allow  |
166
    | agent1 (submiter))  |  allow    | allow | allow | allow  | allow  | allow  |
167
    | user1               |  login    | allow | allow | allow  | allow  | allow  |
168

  
164 169
    On restoring draft, the logged user become the new draft owner,
165 170
    this affect the computed and prefill fields.
166 171
    """
......
261 266
            else:
262 267
                assert expected in ('login', 'forbidden', 'frontoffice',  'backoffice')
263 268

  
264
    def check_tracking_code_access(user, owner=None, new_owner=None):
269
    def check_tracking_code_access(user, owner=None, new_owner=None,
270
                                   expected='allow'):
265 271
        """load the formdata using the tracking code"""
266 272
        pub.session_manager.session_class.wipe()
267 273
        app = get_app(pub)
......
272 278
        resp = resp.forms[0].submit()
273 279
        assert resp.location == 'http://example.net/code/%s/load' % tracking_code
274 280
        resp = resp.follow()
281
        if expected == 'login':
282
            assert resp.location == (
283
                'http://example.net/login/?ReturnUrl='
284
                + 'http://example.net/test/%s') % formdata_id
285
            return
275 286
        assert resp.location == 'http://example.net/test/%s' % formdata_id
276 287
        resp = resp.follow()
277 288
        if is_draft:
......
340 351
            check_direct_access(users[i], expected[i])
341 352

  
342 353
    # access to formdata using the tracking code
354
    expected = ('login', 'allow', 'allow', 'allow', 'allow', 'allow')
343 355
    is_draft = False  # demands
344
    for user in users:
356
    for i in range(len(users)):
345 357
        with submission(anonymous, is_frontoffice=True) as (tracking_code, formdata_id):
346
            check_tracking_code_access(user, owner=anonymous)
358
            check_tracking_code_access(users[i], owner=anonymous)
347 359
        with submission(agent1, is_frontoffice=False) as (tracking_code, formdata_id):
348
            check_tracking_code_access(user, owner=anonymous)
360
            check_tracking_code_access(users[i], owner=anonymous)
349 361
        with submission(user1, is_frontoffice=True) as (tracking_code, formdata_id):
350
            check_tracking_code_access(user, owner=user1)
362
            check_tracking_code_access(users[i], owner=user1, expected=expected[i])
351 363

  
352 364
    is_draft = True  # drafts
353
    for user in users:
365
    for i in range(len(users)):
366
        user = users[i]
354 367
        with submission(anonymous, is_frontoffice=True) as (tracking_code, formdata_id):
355 368
            check_tracking_code_access(user, owner=anonymous, new_owner=user)
356 369
        with submission(agent1, is_frontoffice=False) as (tracking_code, formdata_id):
357 370
            check_tracking_code_access(user, owner=anonymous, new_owner=user)
358 371
        with submission(user1, is_frontoffice=True) as (tracking_code, formdata_id):
359
            check_tracking_code_access(user, owner=user1, new_owner=user)
372
            check_tracking_code_access(user, owner=user1, new_owner=user, expected=expected[i])
wcs/forms/root.py
174 174
            raise errors.TraversalError()
175 175
        if BotFilter.is_bot():
176 176
            raise errors.AccessForbiddenError()
177

  
178
        formdata_url = formdata.get_url().rstrip('/')
179
        if formdata.user_id and not get_request().user:
180
            # anonymous user asked to load a tracking code associated with an user,
181
            # don't load, ask for authentication instead
182
            return redirect('/login/?ReturnUrl=%s' % formdata_url)
177 183
        get_session().mark_anonymous_formdata(formdata)
178
        return redirect(formdata.get_url().rstrip('/'))
184
        return redirect(formdata_url)
179 185

  
180 186

  
181 187
class TrackingCodesDirectory(Directory):
182
-