forked from HHS/TANF-app
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into 3.7.5-release-notes
- Loading branch information
Showing
9 changed files
with
201 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tdrs-backend/tdpservice/email/templates/system-admin-role-changed.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{% extends 'base.html' %} | ||
{% block content %} | ||
<!-- Body copy --> | ||
<p style="color: #000000;"> | ||
|
||
{% if action == "added" %} | ||
<p>The following user account for the TANF Data Portal (TDP) has been assigned to OFA System Admin group:</p> | ||
|
||
{% elif action == "removed" %} | ||
<p>The following user account for the TANF Data Portal (TDP) has been removed from OFA System Admin group:</p> | ||
|
||
{% elif action == "is_staff_assigned" %} | ||
<p>The following user account for the TANF Data Portal (TDP) has been assigned to OFA Staff group:</p> | ||
|
||
{% elif action == "is_staff_removed" %} | ||
<p>The following user account for the TANF Data Portal (TDP) has been removed from OFA Staff group:</p> | ||
|
||
{% elif action == "is_superuser_assigned" %} | ||
<p>The following user account for the TANF Data Portal (TDP) has been assigned to OFA Superuser group:</p> | ||
|
||
{% elif action == "is_superuser_removed" %} | ||
<p>The following user account for the TANF Data Portal (TDP) has been removed from OFA Superuser group:</p> | ||
|
||
{% endif %} | ||
|
||
</p> | ||
<p>Account Information:</p> | ||
<ul> | ||
<li>Name: {{ user.first_name }}</li> | ||
<li>Last name: {{ user.last_name }}</li> | ||
<li>Email: {{ user.email }}</li> | ||
</ul> | ||
|
||
<p>Thank you,</p> | ||
TDP Team | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""Signals for the users app.""" | ||
from django.db.models.signals import m2m_changed, pre_save, post_save | ||
from django.dispatch import receiver | ||
from tdpservice.users.models import User | ||
from django.contrib.auth.models import Group | ||
from tdpservice.email.helpers.admin_notifications import email_system_owner_system_admin_role_change | ||
|
||
import logging | ||
logger = logging.getLogger() | ||
|
||
@receiver(m2m_changed, sender=User.groups.through) | ||
def user_group_changed(sender, instance, action, pk_set, **kwargs): | ||
"""Send an email to the System Owner when a user is assigned or removed from the System Admin role.""" | ||
ACTIONS = { | ||
'PRE_REMOVE': 'pre_remove', | ||
'PRE_ADD': 'pre_add', | ||
'PRE_CLEAR': 'pre_clear' | ||
} | ||
if pk_set: | ||
ADMIN_GROUP_PK = Group.objects.get(name="OFA System Admin").pk | ||
group_change_list = [pk for pk in pk_set] | ||
if ADMIN_GROUP_PK in group_change_list and action == ACTIONS['PRE_ADD']: | ||
# EMAIL ADMIN GROUP ADDED to OFA ADMIN | ||
email_system_owner_system_admin_role_change(instance, "added") | ||
elif ADMIN_GROUP_PK in group_change_list and action == ACTIONS['PRE_REMOVE']: | ||
# EMAIL ADMIN GROUP REMOVED from OFA ADMIN | ||
email_system_owner_system_admin_role_change(instance, "removed") | ||
elif pk_set is None and action == ACTIONS['PRE_CLEAR']: | ||
# EMAIL ADMIN GROUP REMOVED from OFA ADMIN | ||
email_system_owner_system_admin_role_change(instance, "removed") | ||
|
||
@receiver(pre_save, sender=User) | ||
def user_is_staff_superuser_changed(sender, instance, **kwargs): | ||
"""Send an email to the System Owner when a user is assigned or removed from the System Admin role.""" | ||
# first get instance from db for existing state | ||
try: | ||
current_user_state = User.objects.get(pk=instance.pk) | ||
except User.DoesNotExist: | ||
return | ||
|
||
# check if is_staff is assigned | ||
if instance.is_staff and not current_user_state.is_staff: | ||
email_system_owner_system_admin_role_change(instance, "is_staff_assigned") | ||
# check if is_staff is removed | ||
elif not instance.is_staff and current_user_state.is_staff: | ||
email_system_owner_system_admin_role_change(instance, "is_staff_removed") | ||
# check if is_superuser is assigned | ||
if instance.is_superuser and not current_user_state.is_superuser: | ||
email_system_owner_system_admin_role_change(instance, "is_superuser_assigned") | ||
# check if is_superuser is removed | ||
elif not instance.is_superuser and current_user_state.is_superuser: | ||
email_system_owner_system_admin_role_change(instance, "is_superuser_removed") | ||
|
||
|
||
@receiver(post_save, sender=User) | ||
def user_is_staff_superuser_created(sender, instance, created, **kwargs): | ||
"""Send an email to the System Owner when a user is assigned or removed from the System Admin role.""" | ||
if created: | ||
if instance.is_staff: | ||
email_system_owner_system_admin_role_change(instance, "is_staff_assigned") | ||
if instance.is_superuser: | ||
email_system_owner_system_admin_role_change(instance, "is_superuser_assigned") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Test signals.""" | ||
import pytest | ||
from unittest.mock import patch, call | ||
from tdpservice.users.models import User | ||
from tdpservice.users.test.factories import AdminUserFactory | ||
from django.contrib.auth.models import Group | ||
import logging | ||
import django | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_my_signal_receiver(mocker): | ||
"""Test my_signal_receiver.""" | ||
with patch("django.db.models.signals.m2m_changed.send") as mock_receiver: | ||
instance = AdminUserFactory.create() | ||
instance.groups.add(Group.objects.get(name="OFA System Admin")) | ||
|
||
mock_receiver.assert_called_with( | ||
sender=User.groups.through, | ||
instance=instance, | ||
action="post_add", | ||
pk_set={Group.objects.get(name="OFA System Admin").pk}, | ||
reverse=False, | ||
using="default", | ||
model=django.contrib.auth.models.Group, | ||
) | ||
mock_receiver.call_count = 2 # pre_save and post_save | ||
|
||
with patch( | ||
"tdpservice.users.signals.email_system_owner_system_admin_role_change" | ||
) as mock_email_system_owner_system_admin_role_change: | ||
instance = AdminUserFactory.create() | ||
instance.groups.add(Group.objects.get(name="OFA System Admin")) | ||
mock_email_system_owner_system_admin_role_change.assert_has_calls([ | ||
call(instance, 'is_staff_assigned'), | ||
call(instance, 'is_superuser_assigned'), | ||
call(instance, "added") | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters