Projet

Général

Profil

0001-authenticators-keep-readable-journal-log-even-if-del.patch

Valentin Deniaud, 17 août 2022 17:27

Télécharger (5,55 ko)

Voir les différences:

Subject: [PATCH] authenticators: keep readable journal log even if deleted
 (#68184)

 .../authenticators/journal_event_types.py     | 41 +++++++++----------
 tests/test_manager_journal.py                 |  9 ++++
 2 files changed, 28 insertions(+), 22 deletions(-)
src/authentic2/apps/authenticators/journal_event_types.py
22 22
from .models import BaseAuthenticator
23 23

  
24 24

  
25
class AuthenticatorCreation(EventTypeDefinition):
25
class AuthenticatorEvents(EventTypeDefinition):
26
    @classmethod
27
    def record(cls, *, user, session, authenticator, data=None):
28
        data = data or {}
29
        data.update({'authenticator_name': str(authenticator), 'authenticator_uuid': str(authenticator.uuid)})
30
        super().record(user=user, session=session, references=[authenticator], data=data)
31

  
32

  
33
class AuthenticatorCreation(AuthenticatorEvents):
26 34
    name = 'authenticator.creation'
27 35
    label = _('authenticator creation')
28 36

  
29
    @classmethod
30
    def record(cls, *, user, session, authenticator):
31
        super().record(user=user, session=session, references=[authenticator])
32

  
33 37
    @classmethod
34 38
    def get_message(cls, event, context):
35 39
        (authenticator,) = event.get_typed_references(BaseAuthenticator)
40
        authenticator = authenticator or event.get_data('authenticator_name')
36 41
        if context != authenticator:
37 42
            return _('creation of authenticator "%s"') % authenticator
38 43
        else:
39 44
            return _('creation')
40 45

  
41 46

  
42
class AuthenticatorEdit(EventTypeDefinition):
47
class AuthenticatorEdit(AuthenticatorEvents):
43 48
    name = 'authenticator.edit'
44 49
    label = _('authenticator edit')
45 50

  
46 51
    @classmethod
47 52
    def record(cls, *, user, session, form):
48
        super().record(user=user, session=session, references=[form.instance], data=form_to_old_new(form))
53
        super().record(user=user, session=session, authenticator=form.instance, data=form_to_old_new(form))
49 54

  
50 55
    @classmethod
51 56
    def get_message(cls, event, context):
52 57
        (authenticator,) = event.get_typed_references(BaseAuthenticator)
58
        authenticator = authenticator or event.get_data('authenticator_name')
53 59
        new = event.get_data('new') or {}
54 60
        edited_attributes = ', '.join(new) or ''
55 61
        if context != authenticator:
......
60 66
            return _('edit ({change})').format(change=edited_attributes)
61 67

  
62 68

  
63
class AuthenticatorEnable(EventTypeDefinition):
69
class AuthenticatorEnable(AuthenticatorEvents):
64 70
    name = 'authenticator.enable'
65 71
    label = _('authenticator enable')
66 72

  
67
    @classmethod
68
    def record(cls, *, user, session, authenticator):
69
        super().record(user=user, session=session, references=[authenticator])
70

  
71 73
    @classmethod
72 74
    def get_message(cls, event, context):
73 75
        (authenticator,) = event.get_typed_references(BaseAuthenticator)
76
        authenticator = authenticator or event.get_data('authenticator_name')
74 77
        if context != authenticator:
75 78
            return _('enable of authenticator "%s"') % authenticator
76 79
        else:
77 80
            return _('enable')
78 81

  
79 82

  
80
class AuthenticatorDisable(EventTypeDefinition):
83
class AuthenticatorDisable(AuthenticatorEvents):
81 84
    name = 'authenticator.disable'
82 85
    label = _('authenticator disable')
83 86

  
84
    @classmethod
85
    def record(cls, *, user, session, authenticator):
86
        super().record(user=user, session=session, references=[authenticator])
87

  
88 87
    @classmethod
89 88
    def get_message(cls, event, context):
90 89
        (authenticator,) = event.get_typed_references(BaseAuthenticator)
90
        authenticator = authenticator or event.get_data('authenticator_name')
91 91
        if context != authenticator:
92 92
            return _('disable of authenticator "%s"') % authenticator
93 93
        else:
94 94
            return _('disable')
95 95

  
96 96

  
97
class AuthenticatorDeletion(EventTypeDefinition):
97
class AuthenticatorDeletion(AuthenticatorEvents):
98 98
    name = 'authenticator.deletion'
99 99
    label = _('authenticator deletion')
100 100

  
101
    @classmethod
102
    def record(cls, *, user, session, authenticator):
103
        super().record(user=user, session=session, references=[authenticator])
104

  
105 101
    @classmethod
106 102
    def get_message(cls, event, context):
107 103
        (authenticator,) = event.get_typed_references(BaseAuthenticator)
104
        authenticator = authenticator or event.get_data('authenticator_name')
108 105
        return _('deletion of authenticator "%s"') % authenticator
tests/test_manager_journal.py
1318 1318

  
1319 1319
    for e in Event.objects.all():
1320 1320
        assert '%s (%s)' % (e.type.name, e.type.definition.label) in response.text
1321

  
1322

  
1323
def test_delete_authenticator(app, superuser, events):
1324
    events['authenticator'].delete()
1325
    response = login(app, superuser, path="/manage/journal/")
1326
    response.form.set('search', 'event:authenticator.creation')
1327
    response = response.form.submit()
1328
    assert len(response.pyquery('tbody tr')) == 1
1329
    assert 'creation of authenticator "Password"' in response.pyquery('tbody tr').text()
1321
-