Projet

Général

Profil

0001-user-detail-display-planned-alert-deletion-dates-744.patch

Paul Marillonnet, 13 février 2023 16:10

Télécharger (7,13 ko)

Voir les différences:

Subject: [PATCH] user-detail: display planned alert & deletion dates (#74464)

 .../authentic2/manager/user_detail.html       | 17 +++++++
 src/authentic2/manager/user_views.py          | 34 ++++++++++++++
 tests/test_user_manager.py                    | 46 ++++++++++++++++++-
 3 files changed, 96 insertions(+), 1 deletion(-)
src/authentic2/manager/templates/authentic2/manager/user_detail.html
85 85
      </p>
86 86
    {% endif %}
87 87

  
88
    {% if alert_date and deletion_date %}
89
      <p class="a2-manager-user-date-alert">
90
        {% if alert_date > now %}
91
          {% blocktrans %}Deletion alert email planned for {{ alert_date }}.{% endblocktrans %}
92
        {% else %}
93
          {% blocktrans %}Deletion alert email sent on {{ alert_date }}.{% endblocktrans %}
94
        {% endif %}
95
      </p>
96
      <p class="a2-manager-user-date-deletion">
97
        {% if deletion_date > now %}
98
          {% blocktrans %}Account deletion planned for {{ deletion_date }}.{% endblocktrans %}
99
        {% else %}
100
          {% blocktrans %}Account deletion performed on {{ deletion_date }}.{% endblocktrans %}
101
        {% endif %}
102
      </p>
103
    {% endif %}
104

  
88 105
    {% for data in user_data %}
89 106
      {{ data }}
90 107
    {% endfor %}
src/authentic2/manager/user_views.py
16 16

  
17 17
import base64
18 18
import collections
19
import datetime
19 20
import operator
20 21

  
21 22
from django.contrib import messages
......
30 31
from django.utils import timezone
31 32
from django.utils.functional import cached_property
32 33
from django.utils.html import format_html
34
from django.utils.timezone import now
33 35
from django.utils.translation import gettext
34 36
from django.utils.translation import gettext_lazy as _
35 37
from django.utils.translation import pgettext_lazy
......
40 42
from authentic2.a2_rbac.models import OrganizationalUnit, Role, RoleParenting
41 43
from authentic2.a2_rbac.utils import get_default_ou
42 44
from authentic2.apps.journal.views import JournalViewWithContext
45
from authentic2.backends.ldap_backend import LDAPBackend
43 46
from authentic2.models import Attribute, PasswordReset
44 47
from authentic2.utils import hooks, spooler, switch_user
45 48
from authentic2.utils.misc import is_ajax, make_url, redirect, select_next_url, send_password_reset_mail
......
437 440
            data for datas in hooks.call_hooks('manager_user_data', self, self.object) for data in datas
438 441
        ]
439 442
        kwargs['user_data'] = user_data
443

  
444
        realms = [block['realm'] for block in LDAPBackend.get_config() if block.get('realm')]
445
        # user is active and belongs to an OU that defines deletion delays
446
        if (
447
            self.object.is_active
448
            and self.object.email
449
            and self.object.ou
450
            and self.object.ou.clean_unused_accounts_alert
451
            and self.object.ou.clean_unused_accounts_deletion
452
        ):
453
            # user does not have any external identifier that would prohibit automated deletion
454
            if not (
455
                getattr(self.object, 'oidc_account', None)
456
                or (
457
                    getattr(self.object, 'userexternalid', None)
458
                    and getattr(self.object.userexternalid, 'source', None)
459
                    and self.object.userexternalid.source in realms
460
                )
461
            ):
462
                # base value for computing alert & deletion is creation date or last login
463
                start = self.object.date_joined or self.object.last_login
464
                # but the keepalive alert date is more relevant if more recent
465
                if self.object.keepalive and self.object.keepalive > start:
466
                    start = self.object.keepalive
467
                kwargs['now'] = now().date()
468
                kwargs['alert_date'] = start.date() + datetime.timedelta(
469
                    days=self.object.ou.clean_unused_accounts_alert
470
                )
471
                kwargs['deletion_date'] = start.date() + datetime.timedelta(
472
                    days=self.object.ou.clean_unused_accounts_deletion
473
                )
440 474
        ctx = super().get_context_data(**kwargs)
441 475
        return ctx
442 476

  
tests/test_user_manager.py
756 756
    assert et.attributes.values['phone'].content == '+3281123456'
757 757

  
758 758

  
759
def test_detail_view(app, admin, simple_user):
759
def test_detail_view(app, admin, simple_user, freezer, user_ou1, ou1):
760 760
    url = f'/manage/users/{simple_user.pk}/'
761 761
    resp = login(app, admin, url)
762 762
    assert str(simple_user.uuid) in resp.text
......
766 766
    simple_user.save()
767 767
    resp = app.get(url)
768 768
    assert "Last activity on Feb. 1, 2023" in resp.pyquery('.a2-manager-user-last-activity')[0].text
769
    logout(app)
770

  
771
    ou1.clean_unused_accounts_alert = 700
772
    ou1.clean_unused_accounts_deletion = 730
773
    ou1.save()
774
    user_ou1.date_joined = user_ou1.last_login = datetime.datetime(2023, 1, 1, 3)
775
    user_ou1.save()
776

  
777
    url = f'/manage/users/{user_ou1.pk}/'
778

  
779
    freezer.move_to('2023-01-01')
780
    resp = login(app, admin, url)
781
    assert (
782
        "Deletion alert email planned for Dec. 1, 2024."
783
        in resp.pyquery('.a2-manager-user-date-alert')[0].text
784
    )
785
    assert (
786
        "Account deletion planned for Dec. 31, 2024."
787
        in resp.pyquery('.a2-manager-user-date-deletion')[0].text
788
    )
789
    logout(app)
790

  
791
    freezer.move_to('2024-12-10')
792
    resp = login(app, admin, url)
793
    assert "Deletion alert email sent on Dec. 1, 2024." in resp.pyquery('.a2-manager-user-date-alert')[0].text
794
    assert (
795
        "Account deletion planned for Dec. 31, 2024."
796
        in resp.pyquery('.a2-manager-user-date-deletion')[0].text
797
    )
798
    logout(app)
799

  
800
    freezer.move_to('2025-01-01')
801
    resp = login(app, admin, url)
802
    assert "Deletion alert email sent on Dec. 1, 2024." in resp.pyquery('.a2-manager-user-date-alert')[0].text
803
    assert (
804
        "Account deletion performed on Dec. 31, 2024."
805
        in resp.pyquery('.a2-manager-user-date-deletion')[0].text
806
    )
807

  
808
    ou1.clean_unused_accounts_alert = ou1.clean_unused_accounts_deletion = None
809
    ou1.save()
810
    resp = app.get(url)
811
    assert not resp.pyquery('.a2-manager-user-date-alert')
812
    assert not resp.pyquery('.a2-manager-user-date-deletion')
769 813

  
770 814

  
771 815
def test_detail_view_user_deleted(app, admin, simple_user):
772
-