Skip to content

Commit edeaac7

Browse files
2561 Deactivating inactive user automatically (#3145)
* Added logic and test for deactivating the user automatically * 2561- linting * added admin notification * linting * [2561] added sending email to admin * Update tdrs-backend/tdpservice/email/helpers/admin_notifications.py Co-authored-by: Alex P. <63075587+ADPennington@users.noreply.github.com> --------- Co-authored-by: Alex P. <63075587+ADPennington@users.noreply.github.com>
1 parent abd8609 commit edeaac7

7 files changed

Lines changed: 103 additions & 3 deletions

File tree

tdrs-backend/tdpservice/email/email_enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ class EmailType(Enum):
1313
REQUEST_DENIED = 'request-denied.html'
1414
DEACTIVATION_WARNING = 'account-deactivation-warning.html'
1515
ACCOUNT_DEACTIVATED = 'account-deactivated.html'
16+
ACCOUNT_DEACTIVATED_ADMIN = 'account-deactivated-admin.html'
1617
UPCOMING_SUBMISSION_DEADLINE = 'upcoming-submission-deadline.html'

tdrs-backend/tdpservice/email/helpers/account_deactivation_warning.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
from tdpservice.email.email import automated_email
44
from datetime import datetime, timedelta, timezone
55
from django.conf import settings
6-
6+
from tdpservice.users.models import User
77

88
def send_deactivation_warning_email(users, days):
99
"""Send an email to users that are about to be deactivated."""
10-
from tdpservice.users.models import User
11-
1210
template_path = EmailType.DEACTIVATION_WARNING.value
1311
text_message = f'Your account will be deactivated in {days} days.'
1412
subject = f'Account Deactivation Warning: {days} days remaining'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""helper functions to administer user accounts."""
2+
3+
def email_admin_deactivated_user(user):
4+
"""Send an email to OFA Admins when a user is deactivated."""
5+
from tdpservice.users.models import User
6+
from tdpservice.email.email_enums import EmailType
7+
from tdpservice.email.email import automated_email, log
8+
from tdpservice.email.tasks import get_ofa_admin_user_emails
9+
10+
recipient_emails = get_ofa_admin_user_emails()
11+
logger_context = {
12+
'user_id': user.id,
13+
'object_id': user.id,
14+
'object_repr': user.username,
15+
'content_type': User,
16+
}
17+
18+
template_path = EmailType.ACCOUNT_DEACTIVATED_ADMIN.value
19+
text_message = 'A user account has been deactivated.'
20+
subject = ' TDP User Account Deactivated due to Inactivity'
21+
context = {
22+
'user': user,
23+
}
24+
25+
log(f"Preparing email to OFA Admins for deactivated user {user.username}", logger_context=logger_context)
26+
27+
for recipient_email in recipient_emails:
28+
automated_email(
29+
email_path=template_path,
30+
recipient_email=recipient_email,
31+
subject=subject,
32+
email_context=context,
33+
text_message=text_message,
34+
logger_context=logger_context
35+
)

tdrs-backend/tdpservice/email/tasks.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,35 @@
1616
from tdpservice.email.email import automated_email, log
1717
from tdpservice.email.email_enums import EmailType
1818
from tdpservice.parsers.util import calendar_to_fiscal
19+
from tdpservice.email.helpers.admin_notifications import email_admin_deactivated_user
1920

2021

2122
logger = logging.getLogger(__name__)
2223

24+
@shared_task
25+
def deactivate_users():
26+
"""Deactivate users that have not logged in in the last 180 days."""
27+
users_to_deactivate = User.objects.filter(
28+
last_login__lte=datetime.now(tz=timezone.utc) - timedelta(days=180),
29+
account_approval_status=AccountApprovalStatusChoices.APPROVED,
30+
)
31+
32+
for user in users_to_deactivate:
33+
user.account_approval_status = AccountApprovalStatusChoices.DEACTIVATED
34+
user.groups.clear()
35+
user.save()
36+
37+
logger_context = {
38+
'user_id': user.id,
39+
'object_id': user.id,
40+
'object_repr': user.username,
41+
}
42+
email_admin_deactivated_user(user)
43+
log(
44+
f"Deactivated user {user.username} for inactivity.",
45+
logger_context=logger_context if not settings.DEBUG else None
46+
)
47+
2348

2449
@shared_task
2550
def check_for_accounts_needing_deactivation_warning():
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% extends 'base.html' %}
2+
{% block content %}
3+
<!-- Body copy -->
4+
<p style="color: #000000;">
5+
6+
<p>The following user account for the TANF Data Portal (TDP) has been deactivated.</p>
7+
8+
<p>Account Information:</p>
9+
<ul>
10+
<li>Name: {{ user.first_name }}</li>
11+
<li>Last name: {{ user.last_name }}</li>
12+
<li>Email: {{ user.email }}</li>
13+
</ul>
14+
15+
<p>Thank you,</p>
16+
TDP Team
17+
</p>
18+
{% endblock %}

tdrs-backend/tdpservice/scheduling/test/test_user_deactivation_warning.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ def test_deactivation_email_10_days(user, mocker):
3030
assert tdpservice.email.helpers.account_deactivation_warning.send_deactivation_warning_email.called_once_with(
3131
users=[user], days=10)
3232

33+
@pytest.mark.django_db
34+
def test_deactivate_users(user, mocker):
35+
"""Test that the deactivate_users task runs."""
36+
mocker.patch(
37+
'tdpservice.email.helpers.admin_notifications.email_admin_deactivated_user',
38+
return_value=None
39+
)
40+
user.groups.add()
41+
user.last_login = datetime.now(tz=timezone.utc) - timedelta(days=181)
42+
user.account_approval_status = AccountApprovalStatusChoices.APPROVED
43+
user.save()
44+
tdpservice.email.tasks.deactivate_users()
45+
assert user.groups.count() == 0
46+
assert tdpservice.email.helpers.admin_notifications.email_admin_deactivated_user.called_once_with(user)
47+
48+
3349
@pytest.mark.django_db
3450
def test_deactivation_email_3_days(user, mocker):
3551
"""Test that the check_for_accounts_needing_deactivation_warning task runs."""

tdrs-backend/tdpservice/settings/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,13 @@ class Common(Configuration):
475475
'expires': 15.0,
476476
},
477477
},
478+
'Deactivate Users': {
479+
'task': 'tdpservice.email.tasks.deactivate_users',
480+
'schedule': crontab(day_of_week='*', hour='13', minute='0'), # Every day at 1pm UTC (9am EST)
481+
'options': {
482+
'expires': 15.0,
483+
},
484+
},
478485
'Email Admin Number of Access Requests' : {
479486
'task': 'tdpservice.email.tasks.email_admin_num_access_requests',
480487
'schedule': crontab(minute='0', hour='1', day_of_week='*', day_of_month='*', month_of_year='*'), # Every day at 1am UTC (9pm EST)

0 commit comments

Comments
 (0)