From 91a190cda703e463ac4fb3ee59f153e17bfe4eb2 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 24 Mar 2021 12:04:36 +0100 Subject: [PATCH] manager: easier journal filtering by event types (#50054) --- src/authentic2/manager/journal_views.py | 43 +++++++++++++++++++++++++ tests/test_manager_journal.py | 33 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/src/authentic2/manager/journal_views.py b/src/authentic2/manager/journal_views.py index e764db2e..bada503f 100644 --- a/src/authentic2/manager/journal_views.py +++ b/src/authentic2/manager/journal_views.py @@ -14,8 +14,10 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import functools import uuid +from django import forms from django.contrib.auth import get_user_model from django.core.exceptions import PermissionDenied, ValidationError from django.core.validators import EmailValidator @@ -23,6 +25,7 @@ from django.db.models import Q from django.utils.translation import ugettext_lazy as _ from authentic2.apps.journal.forms import JournalForm +from authentic2.apps.journal.models import EventType from authentic2.apps.journal.search_engine import JournalSearchEngine from authentic2.apps.journal.views import JournalView @@ -69,9 +72,49 @@ to user whose UUID is 1234.''' return self.query_for_users(users) +EVENT_TYPE_CHOICES = ( + ('', _('All')), + ( + _('Users'), + ( + ('login,user.creation,user.registration,sso,password', _('Connection & SSO')), + ('password', _('Password')), + ( + 'password,manager.user.((de)?activation|password|profile|email),^user.deletion,^user.profile', + _('Profile changes'), + ), + ), + ), + ( + _('Backoffice'), + ( + ('manager', _('All')), + ('manager.user', _('User management')), + ('manager.role', _('Role management')), + ), + ), +) + + class JournalForm(JournalForm): search_engine_class = JournalSearchEngine + event_type = forms.ChoiceField(required=False, choices=EVENT_TYPE_CHOICES) + + def clean_event_type(self): + patterns = self.cleaned_data['event_type'].split(',') + qs_filter = functools.reduce(Q.__or__, (Q(name__regex=pattern) for pattern in patterns)) + return EventType.objects.filter(qs_filter) + + def get_queryset(self, **kwargs): + qs = super().get_queryset(**kwargs) + + event_type = self.cleaned_data.get('event_type') + if event_type: + qs = qs.filter(type__in=event_type) + + return qs + class BaseJournalView(views.TitleMixin, views.MediaMixin, views.MultipleOUMixin, JournalView): template_name = 'authentic2/manager/journal.html' diff --git a/tests/test_manager_journal.py b/tests/test_manager_journal.py index 9c992691..c19ef4c0 100644 --- a/tests/test_manager_journal.py +++ b/tests/test_manager_journal.py @@ -931,3 +931,36 @@ def test_search(app, superuser, events): list(map(text_content, p)) for p in zip(pq('tbody td.journal-list--user-column'), pq('tbody td.journal-list--message-column')) ] == [['Johnny doe', 'login using password']] + + response.form.set('search', '') + response.form['event_type'].select(text='Profile changes') + response = response.form.submit() + + table_content = [text_content(p) for p in response.pyquery('tbody td.journal-list--message-column')] + assert table_content == [ + 'deactivation of user "Johnny doe"', + 'activation of user "Johnny doe"', + 'mandatory password change at next login unset for user "Johnny doe"', + 'mandatory password change at next login set for user "Johnny doe"', + 'password reset request of "Johnny doe" sent to "user@example.com"', + 'password change of user "Johnny doe" and notification by mail', + 'password change of user "Johnny doe"', + 'email change of user "Johnny doe" for email address "jane@example.com"', + 'edit of user "Johnny doe" (first name)', + 'password reset', + 'password reset failure with email "USER@example.com"', + 'password reset request with email "user@example.com"', + 'user deletion', + 'profile edit (first name)', + 'password change', + ] + + response.form['event_type'].select(text='Role management') + response = response.form.submit() + + table_content = [text_content(p) for p in response.pyquery('tbody td.journal-list--message-column')] + assert table_content[:3] == [ + 'removal of user "user (111111)" as administrator of role "role1"', + 'addition of user "user (111111)" as administrator of role "role1"', + 'removal of role "role2" as administrator of role "role1"', + ] -- 2.20.1